简体   繁体   中英

For already git tracked file, can we commit changes without adding them in GIT

I have file which tracked by GIT in my local repository. Now, if I make changes in that file, standard process is as follows.

eg for file demo.txt,

git add demo.txt
git commit -m "changes done" demo.txt

HOWEVER, I am able to directly commit those changes without staging them. eg for GIt tracked file demo.txt

git commit -m "changes done" demo.txt 

this will commit changes.

Do we have any explanation for this

Do we have any explanation for this

Yes, we do. It's called the documentation . Let's read it together, shall we?

The content to be committed can be specified in several ways: ...

  • by listing files as arguments to the commit command..., in which case the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git)

That is exactly what you are doing. You are saying the name of the file explicitly in the commit command. This causes git to skip right past the whole add phase and just pretend that demo.txt is the only thing added to the index, and to form the commit from that.


This is actually a really cool little shortcut, because it doesn't negate the index. The index is still sitting there, possibly full of other stuff, waiting for you to form the next commit from it (if you like). So imagine this scenario:

  1. You edit and add, edit and add, edit and add...

  2. You slap your forehead. Oh, darn! I've formed my index, but what I really want to do is commit this couple of files here, not what's in the index at all!

  3. Now, you might be thinking, I guess I'll have to use stash in some way. But no! You use your shortcut and behold, you've done exactly that. And the index maintains its integrity and you can go on working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM