Hello Community,
It is very much likely that you already use standard git commands like git add, git commit, git push, git pull in day-to-day life.
But learning a few more commands will improve your efficiency, and even can be lifesaving sometimes.
Well, I have to admit that, probably not all in this list are “hacks”, just some fairly used git commands by experienced programmers but I hope you will find at least a few very useful commands that you didn’t know of before but you will need very often.
Did you know that you have an autocorrect feature in Git?
If you make a lot of typos while entering Git commands, then you should consider enabling Git autocorrect. To enable autocorrect run this command in your terminal.
git config --global help.autocorrect 1
Though you have already guessed, it autocorrects the Git commands that you have typed wrong. However, if you type something too crazy which is not obviously a Git Command, you will just get an error message.
Use this command to add all your change files to a commit.
git add –A or git add .
Do you know how to switch back and forth between two branches? Use the following command
git checkout -
It makes wandering around branches so much quicker.
If you or your peers have created several commits or have cloned a repository with an existing history of commits several commits, you may want to see what changes and updates are made to the repo, time to time. You can do so by running this command.
git log
This will show you all the commit messages, authors' name, and email, as well as the date and time they were committed. If you want to see only the diff introduced in each commit, use the following command.
git log –p
There is one thing that frustrates many beginners about using git log or git diff they can’t figure out a way to exit commit log. To exit git log, type “q” or “z”. Or type “h” to seek help.
To understand where a code that you push will land, or to know where exactly a piece is coming from when you perform a git pull, it is important to know which branches are pointing where. Run the following command to see where all your branches are tracking from:
git branch -vv
Let’s say you have just discovered a bug, and you want to change the code which is causing it. You got to find out the point in history a commit was made so that you can fix it in that commit. Here’s a command that helps you quickly achieve that
git bisect
We generally add some files and after then commit them. However, this can be done using a single command only when there is not any new file.
git commit -a -m 'commit message'
If you have accidentally added a wrong file to the repo, or have added a file in the wrong directory, you can undo it.
If you haven’t committed yet
git reset /path/filename
If you have already committed the code, still worry not
git reset --soft HEAD~1
git reset /path/filename
git rm /path/filename
git commit
This will undo the commit, remove the file, delete the file from the repo and add a new commit in its place. So, this removes one file, what if you want to undo the whole uncommitted branch that you have just added?
git reset
This is the command to reset the whole branch to as it was before running git add.
Another quite common mistake that happens quite a lot is you forget to add a file. So if you forget to add a file in your last then –amend is your buddy
Add the missed file and run - amend
git add filename
git commit - amend
If you have messed up completely in your last one or more commits, and one to go back to a previous commit, you can do so easily, in fact, ultimately this is the main purpose of Git. Use the following command
git checkout <SHA>
SHA is the Hash or unique ID of the commit you want to revert to. To get the hash of the commit you want to run a git log as mentioned before.
Once you are on that commit you want you simply run any command you want.
And to go back to the present state, you can check out the branch you were on previously.
Sometimes you mess up badly while trying to merge a few branches and want to undo the merge. Here’s how to do it. Because merge is a commit that points the HEAD to a specific commit, we can undo the merge commit running the command.
git revert HEAD
If you want to specify the exact merge commit that you want to undo, use the following command.
git revert -m 1 SHA
SHA is the Hash of the merge you want to undo, and “-m 1” indicates we want to keep the parent of the merge.
Sometimes you just mess up so bad that you want to delete the entire branch and start everything from a clean branch. To delete a branch on your local repo:
git branch — delete <branchname> or git branch --delete --force <branchname>
To delete a branch remotely and to create a new branch to work on
git push origin — delete <branchname>
After deleting the old branch and creating a new one in its place you need to checkout to a new branch to work on it –
git checkout -b <branchname>
If you have typed a branch name wrong, you can fix it too later.
If you haven’t pushed it yet, run this:
git branch -m neam name
where “neam” is the wrongly typed name, “name” is the new name of the branch.
To change a remote branch name you need to first delete the branch and then push the new one.
git push origin --delete neam
git push origin name
Let’s say you are working on a branch and you just find out your colleague has pushed some changes to the same branch, but if you are not ready to pull those changes yet, then you have to stash whatever you are working on, then fetch, then rebase and then apply for a stash. You can automate it. What you have to do is just enable autostash, which automatically stashes your work and then applies the stash after rebasing is done. To enable autotash, type this into your terminal
git config --global rebase.autoStash true
Reflog is like a trump card. It is useful when you have messed up to a point that none of the above commands works. Reflog shows you a list of all the things you have done in a project so far. Then you can go to any point in the past and fix the problem.
git reflog
the above command is used when things go extremely bad.
So, here you have it. All are quite useful little-known commands or Git hacks, which can be lifesaving when the time comes.
Let us know which one you think will be most useful for you. Do you know any other Git Hacks not mentioned in the list?
Thanks,
Pramodh
Pramodh M
DevSecOps Consultant
DevTools
Bengaluru
661 accepted answers
0 comments