user1862770
user1862770

Reputation: 317

git - reset to old version, trying to push it

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?

Upvotes: 0

Views: 276

Answers (2)

Highway of Life
Highway of Life

Reputation: 24341

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

Upvotes: 2

Richard
Richard

Reputation: 3386

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.

Upvotes: 3

Related Questions