8 underrated Git commands every programmer should know (not the usual pull, push, add, commit)โ€‚by@milindsoorya

8 underrated Git commands every programmer should know (not the usual pull, push, add, commit)

image
Milind Soorya HackerNoon profile picture

Milind Soorya

Hey ๐Ÿ‘‹ I am Milind Soorya. I'm interested in Data Science and web development.

I will be adding more useful Git tips, if you find this helpful I suggest bookmarking this page. ๐Ÿ”–

1. Rename a local branch

Useful when you mess up the branch name with some typos.

// Note: no need to include < >, separate words with -
git branch -m <new_name>
eg:- git branch -m new-new-branch

2. Change the upstream branch

Use this to push the local branch to a new remote branch.

git push origin -u <new_name>

3. Makes local branch same as remote

Sometimes we may make many changes to our local branch and end up with something that is worse than what we started with. Don't worry everyone has been there ๐Ÿ˜…. This command will be helpful in those scenarios.

// replace staging with the branch you want to reset to
git reset --hard origin/staging

4. Delete the most recent commit, keeping the work youโ€™ve done

I am amazed thatย not too many people know of this command. This will help to recover from those stupid typos that creep into our commits.

git reset --soft HEAD~1

5. Delete the most recent commit, destroying the work youโ€™ve done

Use this command when you know you really messed up. Don't fret, itโ€™s part of the journey.๐ŸŽฏ

git reset --hard HEAD~1

6. Stash your work

Stash command is used to temporarily work on another branch without committing our current work.

git stash

7. Recover stash by going into that branch and

git stash apply

Please note thatย 

git stash apply
ย won't delete the stashed item from the stash list. If you want to recover stash and drop it from the stash list useย 
git stash pop
.

8. Go back to a previous commit, undo a rebase

It is quite natural to mess up a rebase, these commands will hopefully save you. Use reflog command to find the head number of the required commit.

// Find the head at that point
git reflog 

// Replace 5 with the head number, please be extra careful not to 
// give wrong wrong head number
git reset --hard "[email protected]{5}"

Thank you very much for reading, liking, and commenting on my articles. As you know, writing quality articles takes time and effort. Therefore, if you have enjoyed my article or if it was helpful please support me by
buying me a coffeeโ˜•ย ๐Ÿ˜‡.

๐Ÿ‘‰๐Ÿผ checkout my website,ย milindsoorya.siteย for more updates and to get in touch. Cheers.

Milind Soorya HackerNoon profile picture
by Milind Soorya @milindsoorya.Hey ๐Ÿ‘‹ I am Milind Soorya. I'm interested in Data Science and web development.
Portfolio

Comments

Signup or Login to Join the Discussion

Tags

Related Stories