# Powerful Git commands that would save your life!

Since I've been developing an app in a team, I realized there are very useful git commands that I often use. <br> Here're some of them.

## 1. Git reset --hard & --mixed
```git reset``` is a powerful command when you accidentally add, commit changes or even pull the wrong branch from the remote.<br>

**Case1. Add some changes but you want to undo them.**<br>
1 - ```$ git add . ``` to add all changed files. <br>
2 - Opps😲. I want to undo the add.<br>
3- ```$ git reset --mixed <commit #> ```<br>
*You can find the commit number by hitting ```$ git log``` <br>
You should see something like below.
```
commit 23c627d4ea9349c5d2499ad43ee57b17a7e9d93b (HEAD -> master, origin/master)
// Commit # is 23c627d4ea9349c5d2499ad43ee57b17a7e9d93b
```
or <br>
```$ git reset -- hard HEAD@{head#}``` <br>
*You can find the head number by hitting ```$ git reflog``` <br>
```
23c627d (HEAD -> master, origin/master) HEAD@{0}: commit: something
// Head # is 0
```

**Case 2. Commit some changes but you want to undo it.** <br>
1 - ```$ git add . ``` to add all changed files. <br>
2 - ```$ git commit ``` to commit the change.<br> 
3 - Opps😲. I want to undo the commit.<br>
4 - ```$ git reset --mixed <commit #> ``` if you still want to need the changes. <br>
 or <br>
```$ git reset --hard <commit #> ``` if you don't need the changes anymore.

**Case 3. Pull from your remote repository but you want to undo.**<br>
1 - ```$ git pull origin <branch name>```  <br>
2 - Opps😲. I want to undo the pull.<br>
3 - ```$ git reset --hard HEAD@{head #}``` to undo the pull.

## 2. git stash save (-u) & git stash apply or pop
This command will save you when you realize you are working on the wrong branch you intend to work on but wish to save changes.

1 - Making changes on your branch.  <br>
2 - Opps😲. I'm working on the wrong branch.<br>
3 - ```$ git stash save "comment"``` <br> 
*If you wish to save untracked files, you can do ```$ git stash save -u``` <br>
4 - ```$ git checkout <branch name>``` to move to a branch you want to work on. <br>
5 - ```$ git stash pop stash@{0} ``` <br> 
*If you still want to keep the stash, you can hit ```$ git stash apply stash@{0} ```<br>

## 3. Git commit --amend
Let's say that you've committed some changes to your branch but forgot to add, modify or remove codes.<br>

1 - ```$ git add . ``` to add all changed files. <br>
2 - ```$ git commit ``` to commit the change.<br> 
3 - Opps😲. I forgot to add some codes.<br>
4 - ```$ git commit --amend  -m "some message" ``` If you want to modify the previous commit message <br> 
or <br>
```$ git commit --amend  --no-edit ``` If you want to keep the previous commit message.

Hope this blog post helps you somehow.
If you have any questions about these git commands, please let me know.

