31 Advanced Git Tips
(Boost your Productivity)
Git is a powerful tool.
If you use Git best practices, you can manage your code more efficiently. And with these Git tips, you will be productive.
In addition to productivity, these Git power tips will help you to save your time.
This post will take you to another level with Important Git tricks. And here, I'm sharing a list of 31 advanced git tips and tricks with you.
These Git tips will help you to learn :
- Few hidden secrets of Git
- How you can do more in less time
- Cool things you can do with Git
- Lots more
Let's dive in
Download Git Tips
Git Tips is a long post.
And if you need a pdf summary of all these Git tips for your reference, it is available to download free of cost a pdf.
Download Git Tips
Download 31 Advanced Git Tips &
Boost Your Productivity
#1. Know Your .git folder
The .git folder is created automatically on Git init or git clone command.
It contains all the required information about commits, hooks and remote repo, etc.
And if you delete this folder, your project will forget to live in a repo.
The .git is unique and a hidden folder. In case you want to see the content of the folder, you can view your hidden files.
Technically, in Git, every branch is stored as a text file. And its content contains the commit hash.
#2. Parallel Programming in Git
You can check out two branches at the same time in Git for Parallel Programming. It's possible with worktrees.
For example - You can fix a bug in a repo (in a branch), and at the same time, you can develop new features in the same repo (in another branch)
By default, you have the Main worktree, but you can create additional worktrees known as Linked worktree.
Here are three crucial git commands for Git worktrees.
(i) View the list of worktrees
git worktree list
(ii) Add a new worktree
git worktree add <your new work tree name>
(iii) Remove a worktree
git worktree remove <your worktree name>
#3. Delete Unwanted branches
You might have a few stale branches in your local repository. There is a possibility you have unwanted local branches that no longer exist in the remote.
And it is vital to stay up to date with your branches. So with the following command, you can see all your local branches that do not merge back to HEAD.
To delete branches on each fetch/pull, you can set up the following command configuration.
git config --global fetch.prune true
And you can view your config at any time with the following command.
git config --list
#4. Use .gitignore
It's a special file, and with its help, you can tell to Git all those files you want to ignore. So ignored files will not be a part of your repository.
You can avoid following types of files
Files with sensitive information like configuration files, production passwords, etc.
Files with temporary information like log files
Compiler code such as obj and dll files etc
I always use a gitignore file. The .gitignore file is helpful to filter out unwanted files & directories.
#5. Compress Your Code
You can compress your Git repository code from Git.
You might export specific changes or individual commits.
Also, you can zip your entire branch. In the following example, you will get a zip file for your master branch.
git archive --format zip --output master_branch.zip master
And the Zip will be available in your repo folder.
#6. Undo Shared history
Sometimes you commit something wrong or broken. And you already pushed your changes to a shared repository.
In that case, you can undo your commit with git revert. It will create a new commit. Its usage is simple as follow :
Git revert <your commit hash>
#7. Find Broken Commits
To err is human. Any developer can introduce a bug (by mistake) and commit it to the repository. It might be hard to find that commit.
And the best way to find the faulty commit with Git bisect command.
In fact, with the Git bisect command, you can find out which commit has fixed a bug or introduce a bug.
There is a detailed tutorial about Git bisect on Git Commands.
#8. Visualizing Your Commit
You can visualize your commit at any time with the git bash tool. You can use the --graph option to view that graph.
There are few more Git tips about the git log command and this is the best one.
You will get the following view after running this command..
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
#9. Git plugins
Git is a free and open-source distributed version control system. So it's easy for you to extend its functionality by writing custom scripts. One of my favorite plugins is git-extras.
Here are my favorite two :
git local-commits : git local-commits: List all those commits on your local branch, which are not pushed to the remote.
Git effort: It will show stats regarding the number of commits per file with active days.
Here is the screenshot of the git-extra plugin. It is showing commits per file with active days.
#10. Git Autocompletion
You can save a few keystrokes with git auto-completion. Let's say, for example, you want to type Git fetch.
You can start typing the git f and hit tab. The autocomplete feature will show you all the possible commands starting from f, and as soon as you write "fet" and hit tab, it will complete the word for you.
And you can use auto-completion for anything - for long branch names, checkout branches, etc.
#11. Rewrite Last commit
It is convenient to rewrite, especially when you commit something wrong or a typo in your commit message.
You can use -v option.
git commit -v --amend
#12. Removed Untracked changes
Untracked files are those files that are not in your staging area. (with the Git add command, you add files to your staging area)
And sometimes, you do not want to add those files for many good reasons. In that case, you can get rid of those files easily with the git clean command.
git clean -f -d
#13. Common Git Guides
You can read all the available common git guides with the git help command.
git help -g
#14. Search in Git
You want to search for a few lines of code that you wrote. And your code is committed in the repo. How will you explore?
One of the easy ways to list all commits and then within each commit, search your string
git rev-list --all | xargs git grep -F '<Your search string>'
For example, Here I am searching a css element “ font-size: 52px;”
#15. Remove a file from the Git History
Use a . gitignore file for Git best practice.
The .gitignore file helps us in many ways, and one of the advantages is it ignores checking in sensitive files like your local app settings with API keys and their secrets.
And many times, developers checked in sensitive information.
And no need to say that it's not a good idea to expose sensitive information to a shared public branch.
Here is an easy fix.
You can remove the file with sensitive information by searching in the entire git history with the following commands.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch <your file name>' --prune-empty --tag-name-filter cat -- --all
#16. Optimize your Repo
Git garbage collector, as its name describes, helps to reclaim the memory occupied by objects that are no longer in use.
Many git commands may automatically run git gc. And you can run this on-demand with the Git gc command.
git gc --prune=now --aggressive
You can use this if your team and you are heavily using the git pull/git push command,
#17. Copy a commit
Merge is used for multiple commits and cherry-pick commands you can use for selective commits.
With Cherry-pick, you can copy a commit from one branch to another branch. And you have to use the commit hash.
It is a handy command; its syntax is straight forward.
git cherry-pick <commit-sha>
Cherry-picking a commit is not always a best practice; chances are you might duplicate a commit.
But in a few scenarios, it will be helpful, especially for bug fixing and in restoring lost commits.
#18. Backup untracked files
With the Git, it's easy, If you need the backup of your untrack files.
You can use the git command to create a zip archive. It will place the archive file in your home directory.
This command will take all of the untracked files (but not ignored files) in your directory:
git ls-files --others --exclude-standard -z | xargs -0 tar rvf ~/backup-untracked.zip
#19. Get Repository Name
Sometimes it might not be very clear when you are working with multiple projects and in various repositories.
The following command will help you to know the repo name from your Git bash.
basename `git rev-parse --show-toplevel`
#20. Counts your commits
There is a git command to count the number of commits in a branch.
git rev-list --count <branch-name>
For example, in the following screenshot, the dev branch has 34 commits, and the master has only 32 commits.
Sometimes it's good to know how many commits you or your coworker have done.
#21. Open all conflicts
During the merge, you might get conflicts. And it's handy to see all the conflicted files at once in an editor.
git diff --name-only | uniq | xargs $EDITOR
#22. Autocorrect typos
Sometimes I make mistakes while typing. For example, most of the time, I use git stats ( instead of git status)
But Git is smart. It gives a clue about the most similar command.
You can set the autocorrect flag to the right, so instead of telling you to use the correct command, it will use the closest match to autocorrect.
git config --global help.autocorrect 1
#23. Sort branches by date
You can sort the branches by their commit date. It is helpful when you are away from the project for some time. It is not a fancy thing, but it can help you on different occasions.
The syntax of the command is simple.
git branch --sort=-committerdate
#24. Git Message Template
The Git tips about message template is really important. As good commit message is also Git best practices.
Writing a useful commit message is a good practice. It always helps the team to understand what is the purpose of each commit.
You can set up a commit message structure. Its two-step process
- Update your git config to use a commit template
git config commit.template ./gitmessage
Create a git message template file (below is an example)Why:
# 50-character subject line
And when you commit next time, you can use this template to commit.
#25. Update from Remote
You might need to overwrite your local changes at some time. It's easy to update from remote.
git fetch origin && git reset --hard origin/master && git clean -f -d
#26. Reset Author
If you are using your nickname in git config and Git commits, you want to change it. It is super easy.
Let's update the author in the gitconfig file.
git config --global user.name "<name>"
And now, you reset the author in the commit.
git commit --amend --reset-author --no-edit
#27. Stash ( And No commit)
A stash is a temporary area where you can use it when you want to store the current state of the index temporarily.
I have also mentioned few Git tips in my another blog - Git commands. Here is another brief.
And the idea is you want to use it later on (without committing your changes)
(a) Create a stash
git stash <your stash name>
(b) Lish all stash
Git stash list
(c) View the content of your stash
Git stash show
(d) Apply the last stash
Git stash pop
#28. Setting Up a Merge Tool
You can use the merge tool to set up your favorite editor.
Setting up a merge tool is easy.
git config --global merge.tool <mergetool_path>
#29. Search Branches
You can search a commit within all branches. Here is an easy way to explore with a commit hash.
git branch --contains <commit>
For example, the following commit is available in two branches dev and master.
#30. Git Alias
This feature will make your git experience customized and straightforward.
So if you do not want to write the complete command, you can set up your preferred alias and then use it.
The syntax is straightforward.
git config --global alias.<handle> <command>
In the following command, we are setting us git status command alias.
git config --global alias.s status
#31. Clone a Branch ( And not Repo)
With the clone command, you are copying the entire remote repo. But what about if you want to clone just a specific branch from a remote repository.
The alternative is git remote add.
git remote add -t <remoteBranchName> -f origin <remoteRepoUrlPath>
git checkout <localBranchName>
These powerful Git tips, will help you to boost your productivity.
Download Git Tips
Download 31 Advanced Git Tips &
Boost Your Productivity
I have used these git tips not only to boost productivity but also these helped me to save many hours.
Let me know your thoughts about these Git tips.