As software developers version control plays an important role in our day to day work life. In this article we will discuss 5 git tips and tricks that will enhance your productivity, better your workflow and make you an overall git ninja. Let’s dive in:
1. Remove all your local git branches but keep master
You are often working on many features and every feature requires you to create a separate branch. At some point you will have lots of dangling local branches that you don’t need. As a developer I have this problem all the time. I want to get rid of all branches except master/main. This following command will do the trick.
git branch | grep -v “master” | xargs git branch -D
2. How do I undo the most recent local commits in Git?
This happens to be one of the most asked questions on stack overflow. Let’s say you committed something by mistake and now you have to undo this.
Here’s a git commit that I made recently that I want to undo
git commit -m “this was a mistake”
I can reset to any previous commit by running git reset — hard but this will override my local changes (the changes I made in the local files). We can do better.
We can undo only the latest commit without changing the working tree (files that we made changes to on the disk) with the command below.
git reset HEAD~
After running this we can run our git add and git commit commands like we usually do.
git add .
git commit -m “some message
3. A better git log visualization on terminal
You have most definitely used the git log
command before. It prints out all the version control history in your terminal.
git log output
As you can see in the above output we can see the commit history. We can make this more intuitive with the following git command
git log — graph — pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ — abbrev-commit
Now this will print the following:
git log 2
As you can see this way we have much more information logged out in the terminal. You can also observe recent line changes with the associated commit. Just pass in a -p flag at the end of previous command.
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -p
4. How to delete git branch locally and from remote ?
This one is very self explanatory. To remove a git branch locally we can run the following command
git branch -d <name of your branch>
If you would like to delete the branch without checking merge status use -D.
Now to delete branch from remote you can run the following
git push origin --delete <your remote branch name>
- How to cherry pick from another repository
Let’s say we want to apply some changes from another repository. We can do this by running the following command.
git fetch <remote-git-url> <branch> && git cherry-pick SHA1
Conclusion
I hope you enjoyed learning about these git tricks. That’s all for today, until next time!
References:
https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git
https://coderwall.com/p/sgpksw/git-cherry-pick-from-another-repository
Top comments (6)
git branch | grep -v “master” | xargs git branch -D
will delete branches that haven't been merged, which is potentially dangerous. I'd use-d
instead, as you suggest in your fourth point, and if you end up with a couple of branches left over, you can delete them one at a time after you're sure you don't need them any more.git switch --detach origin/master
git branch --merged | xargs git branch -d
That would be another good choice. It has some of my additions.
It will also keep branches that include the word master in it. If for some reason you have branches named like "not-master", then it is better to stay sharp! 😅
Good point 👍🤠
If you want to redo the most recent local commit, why not use
git commit -a --amend
instead the long sequence of commands described in the article, namely:versus
Also, you should only rarely use
git commit -m "some message"
in real life; most often than not the commit message should be longer than just a single line.Hi,
nice examples. Unfortunatelly your first git log example is horribly broken.
Here is a correct version. Something got messed up with the formatting.