Without creating a branch and doing a bunch of funky work on a new branch, is it possible to break a single commit into a few different commits after it's been committed to the local repository?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
From git-rebase manual (SPLITTING COMMITS section)
git rebase -i
will do it.First, start with a clean working directory:
git status
should show no pending modifications, deletions, or additions.To split apart your most recent commit, first:
Now commit the pieces individually in the usual way, producing as many commits as you need.
If it was farther back in the tree, then
where
3
is how many commits back it is.If it was farther back in the tree than you want to count, then
where
123abcd
is the SHA1 of the commit you want to split up.When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace
pick
withedit
(e
for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:Commit the pieces individually in the usual way, producing as many commits as you need, then
Please note there's also
git reset --soft HEAD^
. It's similar togit reset
(which defaults to--mixed
) but it retains the index contents. So that if you've added/removed files, you have them in the index already.Turns out to be very useful in case of giant commits.
Use
git rebase --interactive
to edit that earlier commit, rungit reset HEAD~
, and thengit add -p
to add some, then make a commit, then add some more and make another commit, as many times as you like. When you're done, rungit rebase --continue
, and you'll have all the split commits earlier in your stack.Important: Note that you can play around and make all the changes you want, and not have to worry about losing old changes, because you can always run
git reflog
to find the point in your project that contains the changes you want, (let's call ita8c4ab
), and thengit reset a8c4ab
.Here's a series of commands to show how it works:
mkdir git-test; cd git-test; git init
now add a file
A
vi A
add this line:
one
git commit -am one
then add this line to A:
two
git commit -am two
then add this line to A:
three
git commit -am three
now the file A looks like this:
and our
git log
looks like the following (well, I usegit log --pretty=oneline --pretty="%h %cn %cr ---- %s"
Let's say we want to split the second commit,
two
.git rebase --interactive HEAD~2
This brings up a message that looks like this:
Change the first
pick
to ane
to edit that commit.git reset HEAD~
git diff
shows us that we just unstaged the commit we made for the second commit:Let's stage that change, and add "and a third" to that line in file
A
.git add .
This is usually the point during an interactive rebase where we would run
git rebase --continue
, because we usually just want to go back in our stack of commits to edit an earlier commit. But this time, we want to create a new commit. So we'll rungit commit -am 'two and a third'
. Now we edit fileA
and add the linetwo and two thirds
.git add .
git commit -am 'two and two thirds'
git rebase --continue
We have a conflict with our commit,
three
, so let's resolve it:We'll change
to
git add .; git rebase --continue
Now our
git log -p
looks like this:Now in the latest TortoiseGit on Windows you can do it very easily.
Open the rebase dialog, configure it, and do the following steps.
Edit
" (among pick, squash, delete...).Start
" to start rebasing.Edit/Split
" button and click on "Amend
" directly. The commit dialog opens.commit
".Very helpful, thanks TortoiseGit !