There is a major commit, after it there are several more commits that cannot be deployed yet. It is necessary to somehow throw out the changes of this commit from the project, then turn around from the next commit before it, and then continue working from the last commit, but excluding the changes of the main commit. Please give a sequence of actions.
4 answers
then turn around from the next commit
Detailed information in the question. How to return (roll back) to an earlier commit? . In short - when you fix everything, you can simply go to the desired commit or even create a new branch in it.
discard changes to this commit from the project,
About this will be the whole answer further.
The most important question:
Did you manage to push this thread to a remote repository? Is it possible that another developer has already received commit B from a remote repository or one of the following ones and continues to work on it. If the answer is yes, then it is generally desirable to revert
, but not rebase, cherry-pick, etc., which rewrites the history of Git, as it creates big problems with integration .
Since you need to turn from C, which does not contain changes from B, you will almost certainly have to rewrite the story. In this case, warn colleagues in advance. They will have to similarly rebuild their changes, for example, from commit D
to D'
.
We denote the initial situation in the following scheme:
A - B - C - D ↑ branchname (HEAD)
A
, B
, C
, D
- commits in branchname
branch. B
- "bad commit", we need to delete it. Commit C
needed. (HEAD)
- location of the HEAD pointer.
Denotes a commit pointed to by a particular branch or pointer.
Option 1 - via rebase -i
- Plus - less trash in history. And you can be proud to have mastered rebase.
- Plus - allows you to turn around from C that does not contain changes from B.
- Minus - rewrites history, it is dangerous during teamwork.
Commands:
git checkout branchname git rebase -i A git checkout -b deployme C'
As A, we specify a commit immediately before the one we exclude. In this case, A will remain in place. The editor will open, there will be commits in reverse order. Change pick
to drop
, denoting that we want to throw this commit.
drop 323d4e6 comment for B pick 31da2b9 comment for C pick 24d420c comment for D # Rebase c8893f9..24d420c onto c8893f9 (3 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit
Result
A - C' - D' ↑ branchname (HEAD) ↑ deployme (HEAD)
New commits now have a different ancestor, so these are new, new commits (although the content is the same minus B). New deployme branch looks at commit C'
.
Option 2 - through revert
- Plus - does not rewrite history and is safe during teamwork. Easier than rebase.
- Minus - an erroneous commit will remain in history. It is bad if there is information that cannot be shown or a code that is embarrassing to publish. :)
- Minus - commit
C
will still contain changes fromB
Commands:
git checkout branchname git revert B
Result:
A - B - C - D - xB ↑ branchname (HEAD)
The new commit xB
contains changes that override changes to commit B
However, we have a problem: commit C
still contains changes from B
A little more about revert
:
In addition:
- If you make a mistake in the process or you lose the wrong commit: How to undo the rollback of changes (restore a lost commit)?
- At the same time, if you wish, you can do something with other commits: How to split / glue the old commit? .
- Thank. Decided to try rebase. But something went wrong: Could not apply (.... last commit ....) fell, I executed git rebase - skip and in the end - the last commits were gone. Only commit A - Mik left
- one@Mik there is a recovery instruction. You had a merge conflict, you had to manually resolve it. Now git reflog, see where to recover. - Nick Volynkin ♦
- Just did reset --hard to the last commit, and everything was restored. As a result, it was decided not to delete the commit, since the bug was easily fixed through branching. But, nevertheless, your answer is mega-useful and will certainly come in handy in the future. Thank! - Mik
Create a branch from the commit you need and continue working in this branch.
git checkout -b <имя ветки> <номер коммита>
Then hold the commits you need in this thread.
git cherry-pick <номер коммита>
Commit deleting changes saved by unwanted commit
git revert HEAD
Deleting a commit from a branch
git reset --hard <tag or hash>
- one
git reset --hard
does not help pull one commit out of a branch. - Nick Volynkin ♦ - However, it was useful for a rollback after unsuccessful rebase - Mik
git revert
- creates a rolling commit, see https://git-scm.com/docs/git-revert
git cherry-pick
- allows you to create a new commit in another branch with changes to the specified commit https://git-scm.com/docs/git-cherry-pick
Theoretically, this combination should be enough
- Thanks, here and met this team in practice =) - Mik