Reputation: 427
In my last commit I added a certain lines in a file, which should've been added as a separate commit. How do I undo only those lines from the commit without deleting them altogether (as I still need to add them later)?
I know I can do git reset HEAD~1
to undo the commit entirely and then patch add git add -p
only the lines I need and then commit it again. I was actually looking for a cleaner solution (if one exists)
Upvotes: 7
Views: 7793
Reputation: 1
git add
-> this will add all files that have been modifiedgit commit --amend --no-edit
-> this will merge all changes that were added or removed to your previous commit, without changing the messageUpvotes: 0
Reputation: 60235
Some changes to @~
shouldn't be in @
but in another branch, so reset them here:
git reset -p @~ -- that/file
git commit
commit them there
git checkout another
git commit -p -- that/file
git checkout @{-1} # aannd we're back
There's special-case logic in git commit -- path
that says "I don't care what the index looks like, commit only the changes to the specific paths I gave you and ignore everything else". It's built specifically for cases like this, where you fixed something that needs recording separately. Just fix what's in front of you in your worktree, then drop-ship the changes wherever and however you want them recorded.
This, by the way, is also a handy shortcut for rebase, to completely refactor your current branch you can
git reset --soft `git merge-base @{u} @`
and then do patch commits for your changes, switching and/or making branches as desired. Once you get comfy with that worfklow, there's even git checkout --merge
, which runs an impromptu content merge with your current tip as the base, it can be noticeably easier to resolve minor conflicts than to do the stash-and-pop dance.
Upvotes: 1
Reputation: 179
This is the fastest I can think of.
git add .
git commit --amend --no-edit
(assuming you won't change your last git commit message)Upvotes: 4
Reputation: 508
I don't think there's really anything cleaner than that. Depending on how much you're looking at pulling out and how its dispersed, git commit --amend
would be maybe simpler if you just want to pull out a few separate lines or a large consecutive chunk and re-add + commit them as a part of the next commit.
Upvotes: 0