Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkout old commit and make it a new commit [duplicate]

Tags:

git

On Git, say I mess up my commits, and I want to make the version 3 commits ago as the new version. If I do git checkout xxxx, it creates a new branch and it seems like I can only merge it? Could I make this the new "master version"?

I want:

A-B-C-D-E

to become

A-B-C-D-E-F

where F has exactly the same content as C

If I use git revert xxxx instead, it seems like it definitely will have conflicts and I need to manually resolve it.

What I really want is just make the old commit at some point the new commit, regardless of what's in my working directory or the latest commit.

How would I go about doing this?

like image 879
huggie Avatar asked Aug 01 '10 05:08

huggie


People also ask

Can I checkout a previous commit?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How do you checkout a commit and create new branch?

In order to create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit to create your branch from. Alternatively, you can use the “git branch” command with the branch name and the commit SHA for the new branch.


6 Answers

git rm -r .
git checkout HEAD~3 .
git commit

After the commit, files in the new HEAD will be the same as they were in the revision HEAD~3.

like image 85
svick Avatar answered Sep 29 '22 21:09

svick


It sounds like you just want to reset to C; that is make the tree:

A-B-C

You can do that with reset:

git reset --hard HEAD~3

(Note: You said three commits ago so that's what I wrote; in your example C is only two commits ago, so you might want to use HEAD~2)


You can also use revert if you want, although as far as I know you need to do the reverts one at a time:

git revert HEAD     # Reverts E
git revert HEAD~2   # Reverts D

That will create a new commit F that's the same contents as D, and G that's the same contents as C. You can rebase to squash those together if you want

like image 22
Michael Mrozek Avatar answered Sep 29 '22 22:09

Michael Mrozek


eloone did it file by file with

git checkout <commit-hash> <filename>

but you could checkout all files more easily by doing

git checkout <commit-hash> .
like image 40
Nick Weisser Avatar answered Sep 29 '22 23:09

Nick Weisser


This is exactly what I wanted to do. I was not sure of the previous command git cherry-pick C, it sounds nice but it seems you do this to get changes from another branch but not on same branch, has anyone tried it?

So I did something else which also worked : I got the files I wanted back from the old commit file by file

git checkout <commit-hash> <filename>

ex : git checkout 08a6497b76ad098a5f7eda3e4ec89e8032a4da51 file.css

-> this takes the files as they were from the old commit

Then I did my changes. And I committed again.

git status (to check which files were modified)
git diff (to check the changes you made)
git add .
git commit -m "my message"

I checked my history with git log, and I still have my history along with my new changes made from the old files. And I could push too.

Note that to go back to the state you want you need to put the hash of the commit before the unwanted changes. Also make sure you don't have uncommitted changes before you do that.

like image 30
eloone Avatar answered Sep 29 '22 22:09

eloone


git cherry-pick C

where C is the commit hash for C. This applies the old commit on top of the newest one.

like image 21
Adrian Parsons Avatar answered Sep 29 '22 23:09

Adrian Parsons


The other answers so far create new commits that undo what is in older commits. It is possible to go back and "change history" as it were, but this can be a bit dangerous. You should only do this if the commit you're changing has not been pushed to other repositories.

The command you're looking for is git rebase --interactive

If you want to change HEAD~3, the command you want to issue is git rebase --interactive HEAD~4. This will open a text editor and allow you to specify which commits you want to change.

Practice on a different repository before you try this with something important. The man pages should give you all the rest of the information you need.

like image 38
haydenmuhl Avatar answered Sep 29 '22 22:09

haydenmuhl