2

I have used git using both command line and GUI, I used to use SourceTree as Git GUI, and in SourceTree, you could, for every modification in a file, visualize wich line were added/removed, and zone of changement were wrapped into block that you could add or not.

For instance, let's take this file:

Foo
Bar
Baz
Git is awesome!

I modify it to that :

Foo
Modif 1
Bar
Baz
Git is awesome!
Modif 2

SourceTree allow you to only select "Modif1" to be staged for commit, leaving Modif2 unstaged, but not deleted, in my local files.

But git status only give me the name of the files that are modified and you can't go with such precision into what will be staged and what won't be staged.

How can I, in command line only, achieve this precision?

The only way I found is to git diff, remove every part you don't want to stage, then commit, and replace the part you haven't staged. That is extremely unconvenient.

1 Answers1

0

Git will allow you to stage chunks of files if you include the '-p' flag for the git add command. Using the file from your example, let's call it foo.txt, you could stage the 'Modif 1' line without staging the rest like so:

git add -p foo.txt

Git will then give you output showing a portion of the diff of the file and asking what to stage. If you type a '?' at the propmt, you'll get a help message explaining the options you have. With the file in the example, both modifications were included in the first hunk, but if you supply an 's' at the staging prompt, then the hunk is split into smaller hunks allowing you to stage just the first modification by providing a 'y', then answering 'n' to the second change. If you then run git status again, you will see the file listed under both the staged and unstaged changes. Running the 'git diff' command will show you the set of changes that are not staged, and running 'git diff --cached' will show you the set of changes that are staged.

Christopher
  • 404
  • 2
  • 9