Reputation: 734
I made some changes to a file and then staged it in the index for the next commit. Then I realized that I needed some more changes to do. Instead of unstaging it, is there a way to capture these subsequent changes before finally committing?
Would it be
git add -u filename (as many times as I make changes to the filename file)
instead of
git reset
git add filename
?
Upvotes: 0
Views: 492
Reputation: 734
echo "new file" > newfile.txt
git add newfile.txt
git ls-files --stage [Note the object corresponding to newfile.txt]
100644 fa49b077972391ad58037050f2a75f74e3671e92 0 newfile.txt
ls -Fls .git/objects/fa [Look into the fa folder]
1 -r--r--r-- 1 user1234 1049089 24 Mar 19 00:17 49b0779.......
echo "adding a second line to my new file" >> newfile.txt
git add newfile.txt
git ls-files --stage [Note the new object corresponding to newfile.txt]
100644 9f70c3077ccb16453d0ad0c152e8c169f81d8466 0 newfile.txt
ls -Fls .git/objects/9f [Look into the 9f folder]
1 -r--r--r-- 1 user1234 1049089 54 Mar 19 00:22 70c3077......
git fsck | grep fa49b0779 [Scanning for dangling blobs]
Checking object directories: 100% (256/256), done. Checking objects: 100% (462/462), done. dangling blob fa49b0779.... [Found that the previous blob is now dangling!]
git fsck | grep 9f70c3077...... [Scanning to see if the current blob is dangling]
Checking object directories: 100% (256/256), done. Checking objects: 100% (462/462), done. [Obviously, not found!]
echo "this is the third line" >> newfile.txt [Making more changes to newfile.txt]
git add newfile.txt
git fsck | grep 9f70c3077..... [Scanning again for 9f70c3077......]
Checking object directories: 100% (256/256), done. Checking objects: 100% (462/462), done. dangling blob 9f70c3077....
[This time, we find 9f70c3077...... is no longer the blob corresponding to newfile.txt. It's now dangling, waiting for the garbage collector to wipe it out!]
Upvotes: 0
Reputation: 219936
You can call git add filename
on the same file as many times as you want.
It'll always add any existing non-staged changes, regardless of whether the file already has changes in the index.
The -u
flag (shortcut for --update
) serves a different purpose. From the man page:
Update the index just where it already has an entry matching
<pathspec>
. This removes as well as modifies index entries to match the working tree, but adds no new files.
In other words: if filename
has never been tracked before, git add -u filename
won't add it to the index. But for files that are already tracked, using the -u
flag makes no difference.
Upvotes: 2