How To: Add untracked files - Save changes
related commands:
add commit status
Your project and your repository are created. Now it’s time to say to Git which files must be tracked and save changes.
First of all, a usefull command to know your local repository state:
$ git status
Git will show you untracked files, modified files and/or changes to be committed. Worst case?
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_1>
modified: <file_2>
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
modified: <file_3>
Untracked files:
(use "git add <file>..." to include in what will be committed)
modified: <file_4>
modified: <file_5>
The Untracked files
section shows you all files found in your local repository which doesn’t exist in remote repository.
The Changed but not updated
section shows you all locally modified files which won’t be commited.
The Changes to be commited
section shows you modified files which will be commited.
You surely noticed that <file_1>
is found in Changed but not updated
and Changes to be commited
. Yes, it is possible. Remember that Git tracks changes in files, not files. This status appears when you register a first modification then you modify again the same file.
Now, our goal is to see:
$ git status
On branch master
nothing to commit (working directory clean)
and here is the walkthrough:
Say to git I want you to track changes in these files
:
$ git add <file_4> <file_5>
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_1>
modified: <file_2>
modified: <file_4>
modified: <file_5>
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: <file_1>
modified: <file_3>
Say to Git I want you to register changes in these files
:
$ git add <file_1> <file_3>
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: <file_1>
modified: <file_2>
modified: <file_3>
modified: <file_4>
modified: <file_5>
Then, I want you to save all recorded changes
:
$ git commit -m "message that explain the commit"
$ git status
On branch master
nothing to commit (working directory clean)
Now, all your changes are saved in a commit, but anyone except you knows about that.
This is the main difference between Git and CVS/SVN: committing something doesn’t share changes.
Reminder
add
Add file content to index if file is untracked or changes in file to index if file is tracked
$ git add <file>
Add multiple files content and/or changes in files to index, depending on tracking status
$ git add <file_1> <file_2> <file_3> ...
commit
Tells Git to save current validated changes into a commit
$ git commit -m "<message>"
The commit will contains all changes in your working directory, even if you didn’t validate them with
git add
$ git commit -a -m "<message>"
status
Shows one to four usefull informations:
. the working branch
. the staged changed files if any
. the unstaged changed files if any
. the untracked files if any$ git status