开发者

Difference between a Git commit modified and Git commit new file

I am confused here...

How do you do a git commit for a modified file and ho开发者_JAVA技巧w do you do a git commit for new file?

Also, how do you individually commit files in git?


There are two parts to the git committing process. First you "stage" individual files and then you do the "commit" command which actually adds the "staged" files to the repository. So if you want to "stage" files you first will use the "git add" command:

git add myfile1

You can do that for as many files as you like. When you call "git status" you should see list of the staged files. You can "add" files that are already in your repository but have changes, and you can also "add" files that aren't yet in your repository. New files will come up as new in the staged area and other ones will just come up as modified, but when you finally call "git commit", you will commit everything that is staged. The "-a" option to git commit means automatically stage all modified files, and also commit them (Note: this does not include files that are not already in the repository ).

Here is an example:

I have a repository where I have two files: file1.txt and file2.txt When I call git status I get the following:

# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   file2.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   file3.txt

So if I want to commit both file2.txt changes, and file3.txt I can do the following:

git add file2.txt
git add file3.txt
git commit

That will commit both the new file and the changes made to file2.txt

If however, I used git commit -a only file2.txt changes would have been commited and the new file (file3.txt) would have been ignored.

So to get the same effect, I could also do:

git add file3.txt
git commit -a

In this case I don't have to add the changed file because the -a option to commit will take care of that.

Note: If you call git commit without any files "staged", nothing will happen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜