Git tricks you should know
- 2021年8月09日
- 技術情報
We use version control system to manage and gather our source codes, right. I am pretty sure most of us are using Git for version control system nowadays. Today, I will share with you some git commands that will help you to improve your git knowledge.
Remove all your local git branches except master
You may have many local branches while you are on developments. But one day, you may want to get rid of these local branches and keep only main/master branch. This command will help you.
git branch | grep -v "main" | xargs git branch -D
Undo last commit
If you want to undo your most recent commit. Follow these steps.
git commit -m "undo message"
Then reset our changes
Pretty git log format
git reset HEAD~
Normally we use to see our git log activity using git log
. But we can see better with pretty formatting using this.
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
Deleting git branch
I wrote deleting local branches except main/master in above. In here, I will talk about deleting a specific branch both on local and remote.
Deleting on local
git branch -d <branch_name>
Deleting on remote
git push <remote> --delete <branch_name>
Cherry picking from another repository
We can apply the changes from another repository to us also.
git fetch <remote-url> <branch_name> && git cherry-pick SHA1
I’m pretty sure there are other useful tricks as well. That’s all for now.
Yuuma
yuuma at 2021年08月09日 10:30:19