
Since I've been developing an app in a team, I realized there are very useful git commands that I often use.
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.
Case1. Add some changes but you want to undo them.
1 - $ git add . to add all changed files.
2 - Opps馃槻. I want to undo the add.
3- $ git reset --mixed <commit #>
*You can find the commit number by hitting $ git log
You should see something like below.
commit 23c627d4ea9349c5d2499ad43ee57b17a7e9d93b (HEAD -> master, origin/master)
// Commit # is 23c627d4ea9349c5d2499ad43ee57b17a7e9d93b
or
$ git reset -- hard HEAD@{head#}
*You can find the head number by hitting $ git reflog
23c627d (HEAD -> master, origin/master) HEAD@{0}: commit: something
// Head # is 0
Case 2. Commit some changes but you want to undo it.
1 - $ git add . to add all changed files.
2 - $ git commit to commit the change.
3 - Opps馃槻. I want to undo the commit.
4 - $ git reset --mixed <commit #> if you still want to need the changes.
or
$ git reset --hard <commit #> if you don't need the changes anymore.
Case 3. Pull from your remote repository but you want to undo.
1 - $ git pull origin <branch name>
2 - Opps馃槻. I want to undo the pull.
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.
2 - Opps馃槻. I'm working on the wrong branch.
3 - $ git stash save "comment"
If you wish to save untracked files, you can do $ git stash save -u
4 - $ git checkout <branch name> to move to a branch you want to work on.
5 - $ git stash pop stash@{0}
If you still want to keep the stash, you can hit $ git stash apply stash@{0}
3. Git commit --amend
Let's say that you've committed some changes to your branch but forgot to add, modify or remove codes.
1 - $ git add . to add all changed files.
2 - $ git commit to commit the change.
3 - Opps馃槻. I forgot to add some codes.
4 - $ git commit --amend -m "some message" If you want to modify the previous commit message
or
$ 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.
Comments
Join the discussionNo comments yet. Be the first to comment.




