15 Git Hacks to Save your Life as a Developer

15 Git Hacks to Save your Life as a Developer

If you have any experience in Software Development projects for some time, then you already know how important Git is. 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 few very useful commands that you didn?t know of before but you will need very often.

Image for post

Here is our pick

15 Git Hacks to save your life

1. Autocorrect:

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 have 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.

2. Add all of the change files to a commit:

Use this command to add all of your change files to a commit.

git add ?A


git add .

3. Switching back and forth between two branches:

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.

Similar Link: Why Should You Opt For GitShip? What Makes It Different?

4. Check Commit History:

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 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 for help.

5. See where all your branches are tracking from

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 very important to know which branches are pointing where.

Run the following command to see where all your branches are tracking from:

git branch -vv

6. Pinpoint exactly when in the history a commit was made:

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

7. Add & commit in a single command:

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’

8. Undo a git addIf you have accidentally added a wrong file to the repo, or have added a file in a 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~1git reset /path/filenamegit rm /path/filenamegit commit

This will undo the commit, remove the particular file, delete the file from the repo and add a new commit in its place.

So, this removes one particular file, what if you want to undo 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

9. Add a file that you missed to add in your last commit

Another very common mistake 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 filenamegit commit ? amend

10. Revert repo to a previous commit

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.

11. Undo a Git Merge

Sometimes you mess up badly while trying to merge few branches, and want to undo the merge.

Here?s how to do it.

Because merge is a commit which 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, using 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.

12. Delete a Git branch locally and remotely

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 in its place you need to checkout to a new branch in order to work on it –

git checkout -b <branchname>

13. Change branch name:

If you have typed a branch name wrong you can fix it too later on.

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 neamgit push origin name

14. Autostash:

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 do is just enable autostash, which automatically stashes your work and then applies the stash after rebase is done. To enable autotash, type this into your terminal –

git config –global rebase.autoStash true

15. git reflog

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. So,

git reflog

is the command to use when things go extremely bad.

So, here you have it. All are the quite useful little known commands or Git hacks (if you want to call it so), 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? We would love to know, even add in our list if we find it cool.


No Responses

Write a response