Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git partial commit of staged changes

Tags:

git

My current git status looks like this:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   docs/file3.org
#       modified:   src/main/R/lib.R
#
# 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:   docs/file3.org
#       modified:   src/main/R/lib.R

I'd like to commit the staged changes to docs/file3.org first, then the other staged changes. But if I do git commit -m '...' docs/file3.org, it will commit both the staged & unstaged changes to that file.

Is there an easy way to do this? Or do I need to stash my unstaged changes, unstage one of the files, commit the other, restage, commit, and stash pop?

like image 710
Ken Williams Avatar asked Jan 26 '12 15:01

Ken Williams


People also ask

Do staged changes get committed?

01 Adding changes html have been staged. This means that git knows about the change, but it is not permanent in the repository. The next commit will include the changes staged. Should you decide not to commit the change, the status command will remind you that you can use the git reset command to unstage these changes.

What is partial commit in git?

While staging changes made in different files is simple enough in git, it can be quite complicated when there are multiple changes in the same file. To this end, you can take advantage of partial commits in git that allow you to iterate over each line in a file to choose whether you want to stage those changes or not.

What is a partially staged file?

Staging area (index) is a place where you prepare the next commit. This area contains candidate files for going into the local repository. At the end all the content from here will be committed as an assembly into the local repository.


1 Answers

Since you only need to commit the staged changes to a file, you can just stash, keeping the indexed changes intact, and committing the file after that.

git stash --keep-index #Note that the staged changes also become part of the stash
git commit -m "Commit only one file" docs/file3.org

git commit -m "Commit other staged changed"
git stash pop 
# This may raise CONFLICTS since the stash includes the earlier staged changes.
# In case of conflict, the stash is not popped, so you need to drop it explicitly.
like image 132
Sailesh Avatar answered Sep 23 '22 06:09

Sailesh