5 Git Tricks Every Developer Should Know

Shadid Haque - Aug 5 '21 - - Dev Community

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .