简体   繁体   中英

What is the equivalent of TFVC “Rollback” in Git?

If I use T eam F oundation S ource C ontrol I can easily rollback to old changeset.

Assuming I have a file with 4 versions, each version have a changset:

  • Changeset a126 - Version 1.
  • Changeset b347 - Version 2.
  • Changeset c560 - Version 3.
  • Changeset d912 - Version 4.

Now I found a lot of bugs in Versions 3 & 4 and I want quickly return back to Version 2.

In Visual Studio I can click "View History" on the file, click on Changeset b347 ("Version 2") and then "Rollback".

Now I have a new changeset with the file in Version 2 and I can check-in it (I still have Version 3/4 in my history, so I can also return to them sometime).

In Git, I know there are revert , reset (hard, soft) but I don't know exactly what I need to do to reach the same result.

So what is the best way to rollback to old version in Git (preferred via Visual Studio)?

git reset --hard b347
git push -f origin master

This removes c560 and d912 completely, but after that you need to force-push.

See What's the difference between Git Revert, Checkout and Reset?

To undo changes creating new commits:

git revert d912 c560

Suppose you have 4 commits ABCD , the file foo.txt is changed in each of them, and the codes of C and D have bugs introduced by foo.txt .

1.Other files may be changed in C and D. If you want to rollback only foo.txt to the version of B :

git checkout B -- foo.txt

If you want to commit the rollback,

git commit

And the history will be ABCD-R1 .

2.If you want to rollback all the changes of C and D (including those of other files if any), and CD have been pushed to the remote repository:

git revert D C

And the history will be ABCD-R2-R3 .

3.If you want to rollback all the changes of C and D, and CD have NOT been pushed to the remote repository:

git reset B --hard

The history will be AB . You could also use this solution for Case 2, but you then need to force-push the branch to overwrite the branch in the remote repository. If other contributors have fetched the old history, you need to tell them to fetch the new history.

不知道什么是TFVC中的签入,但是如果您只想查看v2文件,则可以git checkout b347 -- thefilenameyouwant您想要的文件git checkout b347 -- thefilenameyouwant

To continue work on the v2 state of the repository in your working copy, you can do

# get uncommitted changes out of the way
git stash

# recreate v2 in the working copy
git checkout commit-hash-or-tag-of-v2 

# create new branch based on v2
git checkout -b branchname-for-new-branch

(1) How to roll back and "lose" Versions 3 & 4:

It's as simple as:

git reset --hard b347

Explanation:

  • goes back to version 2.
  • you will "lose" versions 3 and 4.
  • if you still want to get versions 3 & 4 then those commits are still available in your reflog for a certain period of time depending on your settings (default time period is 90 days from memory).

or (2) Create a new branch (to keep versions 3 & 4):

  • You can simply create a new branch at version 2.

    git checkout b347

    git checkout -b redo-work-from-version-2

Explanation:

  • You'll go back to b347, in a headless state. Just ignore this for the moment.
  • We create a new branch called redo-work-from-version-2 .
  • Version 3 and version 4 will still exist and are available, but they will be on a different branch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM