0

git status gives me this:

Your branch is behind 'origin/network' by 11 commits, and can be fast-forwarded.

This is because I reset to an earlier version (hoping to discard all I had done after).

However, if I try to push that version (or that version with some minor changes) this is not working. I do not want the new stuff, I just want to start new with the old version I reverted to!

What do I do?

j0k
  • 22,600
  • 28
  • 79
  • 90
user1862770
  • 307
  • 1
  • 4
  • 13

2 Answers2

3

If you are the only user of the repository, you can force the push; but if anyone else has pulled from the repo, you should not force push since it will mess up everyone else's history.

You can git merge --ff-only origin/network and then git revert HEAD~11 or something similar to generate a new commit that undoes the past eleven commits, without altering the commit history.

See also this answer to a similar question.

Community
  • 1
  • 1
Richard
  • 3,316
  • 30
  • 41
  • That does not work: commit commitnumber is a merge but no -m argument was given. As a matter of fact, I tried a similar thing before. The system shows me the parents, The argument I gave was one of them (I triplechecked) and still git tells me that the argument I gave is no parent – user1862770 Jan 11 '13 at 07:53
  • I did a force now. Apparently it did what it should. No one has pulled from the repo since the version I reverted to. This should make no errors to others, should it? – user1862770 Jan 11 '13 at 08:04
  • @user1862770: Well, as you seem to have a non-trivial situation, you should provide more information in your question so that people can help you. Note that you can edit your question post. – Nevik Rehnel Jan 11 '13 at 08:04
  • Or rather: If it did, can they, in turn, still revert to earlier versions or is that impossible now? – user1862770 Jan 11 '13 at 08:05
2

Since you've already pushed the "future" code, you want to avoid rewriting history at all costs if you have other contributors. You would introduce pretty severe updating nightmares.

If you know that nobody else has pulled the newest code, you can do the following to "recreate" the branch at a specific point. The following rewrites the history of your network branch.

git reset --hard <commit-id>
git push --force origin network

If you don't want to rewrite history (you should always avoid rewriting push-ed history), you can 'revert' the last 11 commits on the network branch. The following is done while on the HEAD of your network branch.

git revert OLDER_COMMIT^..NEWER_COMMIT

This effectively creates commits which 'revert' existing commits. Though where you might find yourself in trouble on this is if you merge this branch into another branch later and wanted to preserve your existing 11 commits for the merged branch. See: the answer for Revert Multiple git commits.

You could also revert the 11 commits in one commit.

# reset your network branch to be the same as the remote
git reset --hard origin/network

# Set your branch (soft) index to the commit point you want your branch state to have:
git reset --soft <commit-id>

# The index will change and the files will reflect a difference you can now commit:
git commit -a
Community
  • 1
  • 1
Highway of Life
  • 22,803
  • 16
  • 52
  • 80