5 Git Tricks Every Developer Should Know
Tags: git
Published: 2021-08-05 12:26:05

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 <commit-hash>** 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.


![](https://paper-attachments.dropbox.com/s_6D89DABFC9940A59D8BBC120EAC178B1EC503D67F9319BA08C657A9890F714AA_1628166090239_1_oSQgVsUCDJDPpgRXJU6hgQ.png)


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:

![](https://paper-attachments.dropbox.com/s_6D89DABFC9940A59D8BBC120EAC178B1EC503D67F9319BA08C657A9890F714AA_1628166130460_1_CJMLXsaXem3MNry8SKLOhQ.png)



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>


## 5. 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