0

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.

blueFast
  • 41,341
  • 63
  • 198
  • 344
  • 1
    It would probably be easier to commit what you have currently staged, then edit the files, and then either amend (eg `git commit --amend`) the previous commit or make another commit and squash the two. – William Pursell Jan 03 '18 at 18:24
  • @WilliamPursell I completely disagree, rewriting history does not seem like a better option when this can be fixed simply before committing. – yarwest Jan 04 '18 at 00:43
  • "Rewriting history" in a local repository is not something to be avoided. git is a tool for preparing patch sets; using the tool to achieve the results you desire rather than trying to do it by hand is a good thing. When you "rewrite" history, you don't lose any of your commits. If a mistake is made, you can easily recover. If you try to monkey patch things by hand you are liable to lose work. – William Pursell Jan 04 '18 at 15:26

2 Answers2

1

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.

Twisted Lullaby
  • 551
  • 1
  • 8
  • 20
0

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.

yarwest
  • 873
  • 8
  • 19
  • Not sure this will serve me well. The non-committed changes in the working dir are big, and having to go through them to modify the staged changes wll be too much work. The changes to the staged changes are trivial, so that's what I want to directly edit. I am not sure how the staged changes are stored, if as patch or as full file. If it is a patch, I would like to directly modify the patch. If it is a full file, I would like to open an editor with the staged file and change it manually. Not sure if that staged patch / file is accessible from outside git core. – blueFast Jan 04 '18 at 10:55
  • 1
    You can't change in staging, you will have to stage any changes that you want to include. What you can do is commit your current staged changes. Then stash the changes you don't want to commit using ```git stash``` and then revert the last commit using ```git reset HEAD~```. At this point you can make the modifications and recommit the changes (using ```git add . && git commit```). Afterwards you can retrieve the other changes from the stash using ```git stash pop``` and do whatever you want with them (after resolving the conflicts). – yarwest Jan 04 '18 at 11:12
  • Yes, that is what I usually do, and gives me merge conflicts when pulling from stash area – blueFast Jan 04 '18 at 11:35
  • You are going to have to make a choice, either manually patch or resolve the conflicts. – yarwest Jan 04 '18 at 11:58