Track & commit files
Commit
Create a new file hello.txt with the text “hello world”:
Execute the git status
command again:
Git informs us that there are files in your directory that are not yet tracked by Git (untracked files present). If you want to keep track of the files, you have to add them with the git add
command:
Now git is aware of that file. This does not mean that git will automatically take a snapshot of every change to that file. You have to decide when to take a snapshot of the changes in your file(s) and add it to your git history. You do this by executing a commit:
git add
and git commit
are two commands that you will use a lot when working with git.
Modify files
Edit the contents of the file to “hello devine”, and execute the git status command:
Git has detected changes to an already tracked file. You will need to “add” this file again to queue the changes for a commit (called stage for commit):
Commit the changes:
You can add and commit multiple changes in a single command of course. Edit the text, and create a second file:
Git status will now report 2 things: modifying an already tracked file, and detecting a new file that is not yet tracked:
You can now stage these two files separately by executing a git add twice, but you can also do this at once, by using git add .
Now both are staged for commit, and with a single commit you can log them into the git repository:
git add -A .
We’ve seen before that with git add .
you can stage all changes before commit. Keep in mind that deletes (deleting files) are not staged. To do that, you need to add the -A flag to the command.
GUI
It is important to know how to work with git via the command line. Without understanding the basic commands, it is impossible to understand more complex commands that you will need sooner or later.
That said, for some very common commands, it can become cumbersome to execute them all the time. Luckily for us, some GIT functionality is baked in VS Code already. See this paragraph about commits for more information.