If you pushed on master branch :
You can revert your changes by adding a new commit, containing reverse diff of your changes, i.e using git revert <unwanted_commit_number>
If you pushed on a "work in progress branch" :
- You can use revert as above (history-safe)
- You can amend your last commit to add/remove some changes, using
git commit --amend
and force push (/!\ rewrite history)
- You can remove your commit, i.e using interactive rebase
git rebase -i <base_commit_number>^
. Remove your unwanted commits in interactive window and force push (/!\ rewrite history)
- You can move back to a previous state, using
git reset ---hard <commit_number>
and force push (/!\ rewrite history)
Get out of detached HEAD state :
If you come from a checkout like git checkout <commit_number>
or git checkout remote/some_branch
, you can cretae a new branch using git branch <branch_name>
If not, you may have a merge or a rebase, which is in progress. Using git merge --abort
or git rebase --abort
to move back to previous branch