Life-saving git commands
Tags: github, git, devops, linux
Published: 2020-07-18 14:26:30

#### 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 :wq*

> git 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:**

1. git reset soft
2. git reset mix
3. git reset hard 

*Let understand this with examples*

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

2. 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*

3. 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**