Yone
Yone

Reputation: 2134

Git: Check previous commit and put in on top?

Hello and thank you for your time.

I have done some commits which I would like to maintain, however I do need to start from a previous point. So the idea is I want to maintain the latests commits in the history, but I need to code from a previous one which I know it works right.

In particular I would need to get from the repo: https://github.com/PrototipoAtlas/Prototipo/commits/master

The commit: refaactor extract ControlsPanel

So I did:

git checkout 88975b656fae250f469507b3f1f80364810a0e7c

However, how could I put it on top of the repository?

I tried just:

git commit

And it said that no changes have been made.

I have also read:

Git checkout old commit, and further make new commits

How to checkout an old commit and make it a new commit

Checkout old commit and make it a new commit

How to revert Git repository to a previous commit?

But I have not found an answer which I fully understands

Could you help me, please???

Upvotes: 1

Views: 877

Answers (2)

Patrícia Villela
Patrícia Villela

Reputation: 839

TL;DR

$ git cherry-pick --strategy=recursive -X theirs <commit>

If I understand correctly, you want to maintain all the commits, but "pretend" they never occurred by bringing the state to that of the commit. If I'm correct, let me explain some things:

By design, git works with pointers to commits. The state you are in git is HEAD, which is a pointer to a ref. The default ref (i.e., if you didn't move), is refs/head/master, or branch master.

$ cat .git/HEAD 
ref: refs/heads/master

This, in turn, is a pointer to a commit.

$ cat .git/refs/heads/master 
6a7c2df372d612dff5fb5ce8cfbcea02f4ad5bee

I'm telling you this to explain to you what checkout does. When you execute it, you get this message

Note: checking out 'b17058ee12b46cb7b9a15057efe2f1872ff076cc'.

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b

HEAD is now at b17058e... b

If you check now .git/HEAD:

$ cat .git/HEAD
b17058ee12b46cb7b9a15057efe2f1872ff076cc

This means you are pointing to a commit, but you can't commit changes, as pointed by the message. You could create a branch from this commit, but you are not asking for this. To do what you want, you need:

$ git cherry-pick --strategy=recursive -X theirs <commit>

Cherry-pick is a git command that gets a commit and applies it to HEAD. It tries to merge, though, so you'd get a lot of conflicts when all you wanted was to replace the commit. So you can put --strategy=recursive -X theirs as arguments to cherry-pick to say that you want your changes overwritten by the commit you're cherry-picking.

Upvotes: 1

senape
senape

Reputation: 342

I believe that you're looking for something like "git amend", but give also a look to this "rewriting history" article

Another option could be the interactive rebase, that allows you to change the order of your commits.

Upvotes: 1

Related Questions