Git Notes - Undo Commands
Uom workshop for git undo-ing workflow
Wrong initialisation of git
1
2
3
4
5git init
# wasn't supposed to initialise git here
rm -rf .git
cd preferred_folder
git initMinor update of the latest commit
1
2
3
4git commit -m "Not the best commit"
# need to do a minor modification
git add .
git commit --amend --no-editI misspelled the latest commit
1
2
3git commit -m "Frist commit"
# should be "First" not "Frist"
git commit --amend -m "First commit"I misspelled an earlier commit
1
2
3git log
git rebase -i HEAD@{N}
# Use 'reword' in your editor and modify the messageI want to update/remove an earlier commit
1
2
3git log
git rebase -i HEAD@{N}
# Use 'reword/drop' in your editorI shouldn’t have pulled aka ‘undo pull’
1
2
3git pull / git fetch + git merge
# wasn't supposed to pull this!
git reset --hard HEAD@{N}I want to start my branch work from scratch
1
2
3
4
5
6git checkout my-working-branch
# I do some work
git add .
git commit -m "Fixing bug #number"
# All I've done is wrong!
git reset --hard origin/my-working-branchI committed in the wrong branch
1
2
3
4
5git commit -m "Committing to master"
# wasn't supposed to commit in master!
git branch new-branch
git reset --hard origin/master
git checkout new-branchUndo undo
1
2git reflog
git reset HEAD@{N} / git revert HEAD@{N}I shouldn’t have pushed aka ‘undo push’
- This one to be used when sb. has already pulled your update and all the team members want to have a consistent commit history.
1
2
3# shouldn't have pushed!
git revert HEAD@{N}
git push origin master- Pushing the changes to a remote repository
1
git push -u origin master
- If your remote is empty everything should be fine. However, if your remote repo was not empty, you would get the following response:
1
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.
- You are seeing a Git safety feature. Git refuses to update the remote branch with your branch, because your branch’s head commit is not a direct descendent of the current head commit of the branch that you are pushing to. It’s like the local branch was ahead. A possible solution would be to force the push:
1
git push -f origin master
- ‘push -f’ is often disabled for master branch, another alternative would be to merge before pushing:
1
2git fetch
git merge
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
TwikooLivere