Reputation: 44431
I have some changes to be committed in one file, already added (but not committed). The file has other changes in the current directory, which are not to be committed. But I need to edit some of the changes that are about to be committed, before I actually commit. Clear? I thought so!
» git status
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: myfile <-- I need to edit this
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: myfile <-- I do not care about this (for this specific commit)
So, before actually committing, I want to change the staged changes. git commit -i
lets me manually edit a patch. Can I somehow use this feature directly on the staged changes?
Another way to solve this would be to stash the non-staged changes, edit the file in the working directory and adding those changes. But I have the impression that when I unstash I will get conflicts.
Upvotes: 0
Views: 7103
Reputation: 591
If I got you right, the situation is:
You have a file xxx
, and have staged some content e.g.:
line 1
line 2
Besides, you have added more content in this file but not staged, e.g.:
line 3
line 4
Which you don't want to add to stage.
Now, you want to edit the staged content, e.g. remove line 2
.
If I described correctly, git reset -p
is what you need.
Upvotes: 1
Reputation: 990
This seems exactly what git add -p
was made for. It allows you to choose and add snippets of a file separately. You can go ahead and edit your file, afterwards enter the command git add <file path> -p
. This will open your default git editor and allow you to select the changes you want to stage for commiting.
Git will automatically split your changes in snippets that you can either add (by pressing y
), not add (by pressing n
) or split further into separate snippets (by pressing s
).
Googling got me this link that seems to explain how it works and how to use it.
Of course stashing your current changed is also an option, but as you said that might lead to conflicts which would have to be solved afterwards. But this shouldn't pose any problem (apart from the time consumed) if you have the other changes committed and pushed before popping the stash.
Upvotes: 0