I have a list of changed files in git repository. There is one file I don't wanna commit for the current moment. Can I do:
git commit -a
To commit all files and then somehow remove that file from current commit? After such removing it should still be in the list of uncommited files.
You have to reset that file to the original state and commit it again using --amend
. This is done easiest using git checkout HEAD^
.
Prepare demo:
$ git init
$ date >file-a
$ date >file-b
$ git add .
$ git commit -m "Initial commit"
$ date >file-a
$ date >file-b
$ git commit -a -m "the change which should only be file-a"
State before:
$ git show --stat
commit 4aa38f84e04d40a1cb40a5207ccd1a3cb3a4a317 (HEAD -> master)
Date: Wed Feb 7 17:24:45 2018 +0100
the change which should only be file-a
file-a | 2 +-
file-b | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
Here it comes: restore the previous version
$ git checkout HEAD^ file-b
commit it:
$ git commit --amend file-b
[master 9ef8b8b] the change which should only be file-a
Date: Wed Feb 7 17:24:45 2018 +0100
1 file changed, 1 insertion(+), 1 deletion(-)
State after:
$ git show --stat
commit 9ef8b8bab224c4d117f515fc9537255941b75885 (HEAD -> master)
Date: Wed Feb 7 17:24:45 2018 +0100
the change which should only be file-a
file-a | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
git rm --cached
will remove it from the commit set ("un-adding" it); that sounds like what you want.
Use stash; like this:
git add .
git reset Files/I/Want/To/Keep
git stash --keep-index
git commit -a -m "Done!"
git stash pop
If you accidentally commit a file, and want to rewrite your git history, use:
git reset HEAD~1 path/to/file
git commit -a -m "rollback"
git rebase -i HEAD~2
and squash to the two leading commits. You can write a helper script to do either of these if you have a known set of files you prefer not to automatically commit.
Most of these answers circulate around removing a file from the "staging area" pre-commit, but I often find myself looking here after I've already committed and I want to remove some sensitive information from the commit I just made.
An easy to remember trick for all of you git commit --amend
folks out there like me is that you can:
git add .
to add the deletion to the "staging area"git commit --amend
to remove the file from the previous commit.You will notice in the commit message that the unwanted file is now missing. Hooray! (Commit SHA
will have changed, so be careful if you already pushed your changes to the remote.)
if you have already pushed your commit then. do
git checkout origin/<remote-branch> <filename>
git commit --amend
AND If you have not pushed the changes on the server you can use
git reset --soft HEAD~1
Maybe you could also use stash to store temporaly your modifications in a patch file and then reapply it (after a checkout to come back to the old version). This could be related to this other topic : How would I extract a single file (or changes to a file) from a git stash?.
Answer:
git reset HEAD path/to/file
if you did a git add and you haven't pushed anything yet, you just have to do this to unstage it from your commit.
git reset HEAD <file>
Source: Stackoverflow.com