Uom workshop for git undo-ing workflow

  1. Wrong initialisation of git

    1
    2
    3
    4
    5
    git init
    # wasn't supposed to initialise git here
    rm -rf .git
    cd preferred_folder
    git init
  2. Minor update of the latest commit

    1
    2
    3
    4
    git commit -m "Not the best commit"
    # need to do a minor modification
    git add .
    git commit --amend --no-edit
  3. I misspelled the latest commit

    1
    2
    3
    git commit -m "Frist commit"
    # should be "First" not "Frist"
    git commit --amend -m "First commit"
  4. I misspelled an earlier commit

    1
    2
    3
    git log
    git rebase -i HEAD@{N}
    # Use 'reword' in your editor and modify the message
  5. I want to update/remove an earlier commit

    1
    2
    3
    git log
    git rebase -i HEAD@{N}
    # Use 'reword/drop' in your editor
  6. I shouldn’t have pulled aka ‘undo pull’

    1
    2
    3
    git pull / git fetch + git merge
    # wasn't supposed to pull this!
    git reset --hard HEAD@{N}
  7. I want to start my branch work from scratch

    1
    2
    3
    4
    5
    6
    git 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-branch
  8. I committed in the wrong branch

    1
    2
    3
    4
    5
    git commit -m "Committing to master"
    # wasn't supposed to commit in master!
    git branch new-branch
    git reset --hard origin/master
    git checkout new-branch
  9. Undo undo

    1
    2
    git reflog
    git reset HEAD@{N} / git revert HEAD@{N}
  10. 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
    2
    git fetch
    git merge