Although my answer is beyond what you are asking, I think it is actually what you are intending to do.
You used git reset --soft HEAD^
to undo the commit you made. This returns the working copy to the state before your commit (since HEAD
points to your current commit, and HEAD^
points to the one before it (assuming there is only one parent).
But now when you git push
you are told something like:
! [rejected] <branch> -> <branch>e (non-fast-forward)
error: failed to push some refs to 'ssh://<remote server>/<remote path>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This is saying that the commits don't line up and it is there to prevent you from making a mistake. The error message is a bit misleading and you don't want to do what it suggests (a pull to get your branch in sync). You can only know NOT to do this because of your intentions.
You can simply get around this with a --force
(or -f
) (*):
git push --force
You may need to set the upstream again:
git push --force --set-upstream origin <branch>
Be aware that this will have consequences if others have already pulled your work, since there will be different commits that will be making the same changes (possibly). See https://www.kernel.org/pub/software/scm/git/docs/user-manual.html#problems-With-rewriting-history.
To prevent any problems, only ever do pushes to your branches (not some communal branch - for example the development
branch the devs merge all their feature branches into) and be sure to have open communication in your team.
A developer would typically use this pattern for what I call Friday Afternoon commits where you want to save your work before the weekend in-case of hardware failure (but return to the pre-commit state on Monday).
*Friday*
git add --all # To add all files whether they are tracked or not
git commit -m "Friday afternoon commit"
git --set-upstream push # --set-upstream is for if the branch doesn't exist on the remote server
*Monday*
git reset --soft HEAD^
git push -f --set-upstream origin <branch>
The advantage of doing it this way, compared to a git revert
discussed in another answer, is to avoid extra commits. The reset will have 2 commits and this way will have none (no extra ones). The advantage of git reset
is that it will NOT rewrite history so is much safer, especially if you aren't sure of what you are doing.
(*) Typically repositories are configured to NOT let you do this to master - fix on a branch and create a pull request instead. For if you've read the above link, to rewrite history on master will have SERIOUS consequences (unless you are the only person who ever clones that code).