Git is a fantastic distributed version control system that is used by millions of developers around the world. It is a powerful tool that allows you to track changes in your codebase, collaborate with other developers, and manage your codebase effectively. In this article, I will share some of the most useful git commands that I use on a daily basis.
Track file and folder case changes
By default, git treats folder and file names as case insensitive so if you rename something from App.tsx to app.tsx then git does not detect any changes, unfortunately typescript imports are case sensitive and it would cause an error in the app. disabling ignore case would allow git to properly rename the file or folder to be used by the rest of the team
git config --global core.ignorecase false
Undo a commit
Undo the last commit and keep the changes in the working directory
git reset --soft HEAD~1
Disable showing branches in VIM
Disable showing branches in VIM when you run git branch -a
or git branch -r
by running the following command
git config --global pager.branch false
Amend a commit
Best used when you want to add more changes to the last commit without creating a new commit. I often use this when I forget to add a file to the last commit. Caveat: this will change the commit hash so be careful when using it on a commit that has been pushed to the remote repository. If you already pushed the commit to the remote repository then you will need to force push the changes to the remote repository using git push -f
git commit --amend --no-edit
Prune branches
Pruning branches will remove any branches that have been deleted from the remote repository. This is useful when you have a lot of branches in your local repository and you want to clean them up.
git remote update origin --prune
I typically create an alias for this command to make it easier to run by adding the following line to my .bashrc
or .zshrc
file
alias gprune="git remote update origin --prune"
Delete remotely deleted branches
This command will delete all branches that have been deleted from the remote repository. This is useful when you have a lot of branches in your local repository and you want to clean them up.
git branch | egrep -v \"(^\*|main|dev)\" | xargs git branch -D
I typically create an alias for this command to make it easier to run by adding the following line to my .bashrc
or .zshrc
file
alias gclean="git branch | egrep -v \"(^\*|main|dev)\" | xargs git branch -D"
Conclusion
Git has lots of powerful commands that can help you manage your codebase effectively. I hope you found these commands useful and that you will add them to your daily workflow to get ahead of your peers. If you have any other useful git commands that you would like to share, please email us at e@akadenia.com