DEV Community

Cover image for 5 Git Tricks Every Developer Should Know
Shadid Haque
Shadid Haque

Posted on

5 Git Tricks Every Developer Should Know

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
Enter fullscreen mode Exit fullscreen mode

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”
Enter fullscreen mode Exit fullscreen mode

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~
Enter fullscreen mode Exit fullscreen mode

After running this we can run our git add and git commit commands like we usually do.

git add .
git commit -m “some message
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode



  1. 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
Enter fullscreen mode Exit fullscreen mode




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

Discussion (6)

Collapse
moopet profile image
Ben Sinclair

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.

Collapse
jessekphillips profile image
Jesse Phillips

git switch --detach origin/master
git branch --merged | xargs git branch -d

That would be another good choice. It has some of my additions.

  • delete your local master, don't put unneeded work to manage it
  • utilize detached head, it is not scary
Collapse
lucassperez profile image
Lucas Perez • Edited on

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! 😅

Collapse
shadid12 profile image
Shadid Haque Author

Good point 👍🤠

Collapse
jnareb profile image
Jakub Narębski
  1. How do I undo/redo the most recent local commits in Git?

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:

git reset HEAD~
git add .
git commit -m "some message"
Enter fullscreen mode Exit fullscreen mode

versus

git commit -a --amend -m "some message"
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
cblte profile image
cblte

Hi,
nice examples. Unfortunatelly your first git log example is horribly broken.
Here is a correct version. Something got messed up with the formatting.

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Enter fullscreen mode Exit fullscreen mode