1. Life-Saving git tips
We have two branches (dev and master) and one file in both branches with one commit for implementation of below scenarios
git init
touch file.txt
write something like- these are life-saving commands
git status
git add file.txt
git commit -m “First Commit”
git log
git checkout dev
git log
Now both branches have similar commits and in exact same condition, now we learn in this document the following commands
- git diff
- git checkout
- git commit --amend
- git cherry-pick
- git reset (Soft, Mix, and Hard)
- git clean
- git reflog
- git revert
- print branch's hashcode
2. Gibberish fix in the file before commit check diff
- add something in file write let say- asdfdsf adsdsd ssfdsf
- Check the differences
git diff
- To fix this
checkout file.txt
Note- Where you use this if you mistakenly unzip a file in the working directory and want to remove all these files
3.Fix wrong commit message
- Now we write next change in file.txt and add a new line - yes these tips are really helpful
git status
git add
git commit -m "wrong message"
git log
you see the wrong commit message, now fix we will fix this
git commit --amend -m "right commit message"
git log
- Now, you can see right commit message from which you have replaced, basically, it is a new commit, as you can see a new hash code for this commit message. It changes git history, it is a bad practice.
4. You accidentally added a file which should be not committed
Let try this,
touch not-to-be-commit.text
git status
git add not-to-be-commit.text
git commit --amend
lets write :wqgit log
- and boom it disappears
5. When you commit an in the wrong branch
git branch
there are two branches
- master
- dev
- We committed in the master branch and move that commit in dev, Also we will be returning the master in the same status it was before this commit. The Answer is cherry-pick
Cherry-pick doesn't delete any commit, it creates a new commit based on dependent commit.
Let’s implement it
git log
Pick the hash which you want to cherry-pick and copy it
git checkout dev
git log
You will see only a commit.
git cherry-pick --copied hash--
git log
Note
- You brought the commit in the dev branch, however, it will not delete that commit from the master branch. to answer this we have git reset.
There are three types of git reset:
- git reset soft
- git reset mix
- git reset hard
Let understand this with examples
- Git Reset Soft
git checkout master
git log
- copy the hash which one you want to have last change
git reset --soft --copied hash--
git log
- You can see you no longer have that commit, which is good that we don't want it longer.
git status
- Now, you can see that there are some files in the stage area, by this command, you will not lose the work.
- Git Mix
git reset -copied hash-
git log
- You can see you no longer have that commit, which is good that we don't want it longer.
git status
- Now, you can see that there are some files in the un-stage area, it’s on you, whatever you want to do- can add changes or delete*
- Git Hard
git reset --hard -copied hashcode-
git log
git status
Boom!! Nothing is left and you are on your previous commit. New changes totally deleted!!
Bonus: You want to get rid of the leftover- untracked file
git clean -df
- d- directory
- f-files
#### 6. When you want to track what you have done
git reflog
7. What if mistakenly run git hard and badly want the code back
git reflog
- grab and copy the hash before the reset
git checkout -copied hash-
- You are in the copied hash, remember everything is hash in git (branch,commit and anything), run git log in this hash
git log
- You clearly can see there are the commit we have reset earlier.
git branch backup
git branch
git checkout master
git branch
git checkout backup
Isn't reflog a lifesaver?
8. You wanna undo the commit but other people pull that changes
git revert- Git revert not delete or modify any commit, it simply creates a new commit top of those that completely undo the changes, so that history remains intact.
git log
- copy the hash which you want to revert
git revert -copied hash-
- Write- :wq
git log
You can see there three commits- two are untouched and we have new commit with revert. if you see the file it undid the previous changes. To see these changes run diff command-
git diff -old copied hash- -reverted has(new commit)
Remember, I said everything(branch, commit) in the git has hashcode
Lets, try this- we have three branches. You already know you have git in this directory by writing a command **git init. Now, we will be doing print the hashcodes these branches. Try and run following commands
ls -al .git
ls -la .git/refs
ls -la .git/refs/heads
cat .git/refs/heads/master
cat .git/refs/heads/dev
cat .git/refs/heads/backup
All these branches have different hashcodes
Top comments (11)
4 is not correct.
git commit --amend
will try to amend the last commit by adding not-to-be-commit.txt to it. So after:wq
you will have effectively squashed not-to-be-commit.txt to the last commit on the branch.To undo a file accidentally added to the index, simply do
git restore --staged not-to-be-commit.txt
.There are many ways to do that you can simply write this :
git rm --cached not-to-be-commit.txt
And Please try this one too (The —amend command), it always works for me. I practiced each and every command and wrote here. Let me know if you have other suggestions. Thanks !!
I use --amend everyday to squash changes into the last commit. If you look into the last commit on the branch after doing
git --amend
, you'll see that the file you didn't want to add has become part of the commit. :-)git rm --cached
would indeed work too, however it is the "old way" of doing it. If you have a recent git version, when you rungit status
, it would suggest to usegit restore --staged
.A personal favourite is:
git reset HEAD^
… to undo the last commit, or:
git reset HEAD~n
… where n is the number of commits to undo.
It becomes a bit problematic if you've done a push beforehand!
Useful Content
Keep it up
Very useful content n kudos to you!!
Thanks Abhishek for your informative articles. Git is always life saver for a developer.
Great! Sir
Great sir ....
Thank you so much. I wanted to learn this especially when I am editing remote code using terminal.