Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rebase vs reset vs revert? I just want to roll back [duplicate]

Tags:

git

Let's say I make a number of commits, let's call them 1, 2, 3, 4, 5, and 6 (hashes).

Let's say I'm on a commit with hash 6. All I want to do is to go back to hash 3, make it so the state of my codebase is as it was when I committed to hash 3 as if the other commits never happened.

When I look at answers like this, it seems like everybody has a different answer. reset, revert, rebase? I'm not even sure that I know the difference between those three words in English.

I just want to be at a previous commit. Can someone tell me how to do this?

like image 478
Alexander Kleinhans Avatar asked Aug 31 '16 22:08

Alexander Kleinhans


People also ask

Can you roll back a rebase?

1 Answer. You can use the reflog to search out the first action before the rebase started then reset --hard back to that. e.g. Now you probably should go back to before the rebase started.

What is the difference between reset and rebase?

They are completely different. git-reset works with refs, on your working directory and the index, without touching any commit objects (or other objects). git-rebase on the other hand is used to rewrite previously made commit objects. So if you want to rewrite the history, git-rebase is what you want.

Why might you want to reset your files back to a previous commit?

Reset - On the commit-level, resetting is a way to move the tip of a branch to a different commit. This can be used to remove commits from the current branch. Revert - Reverting undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history.

What is the difference between revert and rebase in git?

'revert' means to add more commits to make the code look like it did at a different commit, but the history is different (the history includes the old state and the path back to the different state). rebase doesn't change the code at all, but just changes the history.


1 Answers

If you have already pushed your branch somewhere or someone pulled from you, your only option is to git revert $COMMIT....

This will create a commit that undoes whatever you've done in commit(s) $COMMIT....

For example, to revert the last three commits:

git revert HEAD~2..HEAD

If you have kept your commits entirely local and private, you can simply git reset $COMMIT.

This will move your branch pointer to $COMMIT so the branch no longer includes the following commits.

Depending on the state of your index and working tree, you might want any of the options git reset --soft $COMMIT or git reset --hard $COMMIT.

For example, to reset your branch to the commit before the last three:

git reset HEAD~3

git rebase does not sound like what you want.

You use it when you want to copy or "move" some commits from one commit that they are based on, to be based on another commit (another base), hence "rebase".

like image 79
Christoffer Hammarström Avatar answered Oct 04 '22 20:10

Christoffer Hammarström