I have two working branches, master and forum and I\'ve just made some modifications in forum branch, that I\'d like to ch
Run git gui
, select the "Amend last commit" radio button, and unstage (Commit > Unstage From Commit, or Ctrl-U) changes that you do not want to go into first commit. I think that's the easiest way to go about it.
Another thing you could do is cherry-pick the change without committing (git cherry-pick -n
) and then either manually or with git gui
select desired changes before committing.
This might be another solution targeted for cases where there is a huge commit and a small amount of files needs to be moved into a new commit. This will work if a set of <path>
files are to be extracted out of the last commit at HEAD and all moved to a new commit. If multiple commits are needed the other solutions can be used.
First make patches into the staged and unstaged areas that would contain the changes to revert the code to before modification and after modification respectively:
git reset HEAD^ <path>
$ git status
On branch <your-branch>
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <path>
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <path>
To understand what's gonna happen (arrow and comments are not part of command):
git diff --cached -> show staged changes to revert <path> to before HEAD
git diff -> show unstaged changes to add current <path> changes
Revert <path>
changes in last commit:
git commit --amend -> reverts changes on HEAD by amending with staged changes
Create new commit with <path>
changes:
git commit -a -m "New Commit" -> adds new commit with unstaged changes
This has the effect of creating a new commit containing the changes extracted out of the last commit.
Since you're cherry-picking, you can:
cherry-pick
it with --no-commit
option added.reset
and use add --patch
, add --edit
or just add
to stage what you want to keep.commit
the staged changes.
--reuse-message=<old-commit-ref>
or --reedit-message=<old-commit-ref>
options to the commit
command.reset --hard
.Another way, preserving or editing the original commit message:
cherry-pick
the original commit as normal.add
to stage the reversal.
commit --amend
to effect the reversal on the cherry-picked commit.
git reset HEAD^
the --hard is what's killing your changes.