I wanted to "follow-up" with another question about this matter: Checkout old commit and make it a new commit
But they say "Don't Do That!" so it seems I must ask a new question. (Even though it is the same question where the answer I think is best for me didn't work as expected...
If I have commits
A-B-C-D-E-F
(imagine these are all SHA's).
I want to make the entire repository exactly as C
and then commit that to create 'G'
which is exactly like C
. So that when I am done the log is A-B-C-D-E-F-G even though C & G are identical.
One answer indicated cherry-pick, which seemed perfect, except it made me resolve every conflict between C
and F
. I looked at the documentation and I tried --force
anyway, and then the recommendation for --patch
. --patch
was closest but it wasn't absolute. So is there a way to make cherry-pick just choose the C
version of the conflict?
The other closest answer would be to checkout C
, but I couldn't find the magic word that would keep it on the same branch. The recent training I completed said use the "magic dash dash" at the end to tell git
you want this on the current branch, but no matter what I do it creates a "(no branch)" branch.
As I am sure you can tell, I am pretty new to git (and command line in general) but I tried to figure it out on my own. If you could use the verbose versions of what you recommend then it sticks in my head better and would be greatly appreciated. (-a
= --all
or -a = --annotate
or -a = --albuquerque?
)
It seems so simple, and exactly what you might want to do with git -- go back a previous commit without losing the intermediary commits in case you change your mind.
To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.
The git commit –amend command lets you modify your last commit. You can change your log message and the files that appear in the commit. The old commit is replaced with a new commit which means that when you amend your old commit it will no longer be visible in the project history.
We can do this by git reset –hard HEAD^ and moving the head pointer by one after one commit, git reset –hard HEAD~n will move the master branch 'n' commits before the recent commit, and git reset –hard <sha1-commit-hash> will directly move the master branch to the desire commit.
Do you mind generating: A-B-C-D-E-F-F'-E'-D'
where the state of the code at D'
is exactly as it was at C
(so G
== D'
)? In that case, just revert F
, E
, and D
. If you do not want the intermediate steps, revert
but do not apply, and then commit. That is:
$ git revert -n HEAD
$ git revert -n HEAD~
$ git revert -n HEAD~2
$ git commit -m 'Revert D,E,and F'
After this, the current HEAD is G, and the two commits G and C should contain the same code tree. You can verify by checking the output of git cat-file -p HEAD | grep ^tree
and git cat-file -p C | grep ^tree
. Both of those commands should give the same output.
But it's not really clear why you want to keep the intermediate commits. If you want them for posterity, then do something like: git branch old-stuff
, git reset C
That will set your current branch back to C
, but D
, E
, and F
can still be viewed in the branch named old-stuff
.
I thihk you need to use git cherry-pick
.
https://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With