Working with the repository
We have to take a few minutes to look at the life cycle of a file inside Git.
A file will go through the following states, and the Git command line will take the file from one state to another. We will explain each state and its command line.
The important part of this schema is the triangle between the three states
UNMODIFIED, MODIFIED, and
STAGED. This triangle is an infinite loop. Indeed, every time you change a file, its state is set to modified, and then staged; when you commit the file, it returns to the unmodified state, and so on.
UNTRACKED is the first state where the file is created, but this isn't tracked by Git.
To change the state of a file, you have to add it.
When you start an empty repository and add a file, it will be in the untracked state, which means that it isn't in the Git repository.
To track a file, you have to execute this command line:
So, your file is now tracked by Git.
If you want to add all files because you already have something inside the directory while you create the repository, add a period (.
) just after git add
to specify to take all files inside the current directory:
The file is currently staged and ready to be committed inside the repository.
As soon as your file is tracked, all changes will be notified by Git, and you have to commit the change on the repository.
Tip
Remember to commit your change as soon as possible (not for every line, but it's a marker to validate what you have done).
The commit
command is local to your own repository, nobody except you can see it.
The commit
command line offers various options. For example, you can commit a file, as shown in the following example:
To commit everything, use the following command:
You will create a new commit object in the Git repository. This commit is referenced by an SHA-1
checksum and includes various data (content files, content directories, the commit history, the committer, and so on). You can show this information by executing the following command line:
It will display something similar to the following:
The file is in the unmodified state because you just committed the change; you can push the files in the remote repository.
Once committed, you can push the files in the remote repository. It can be on a bare repository, using init
with the git init --bare
command, so just type the following command:
If you create a remote repository on another server, you have to configure your local Git repository.
If you use Git 2.0 or later, the previous command will print out something like this on the screen:
The 'matching'
value from the push.default
configuration variable denotes that git push
will push all your local branches to the branches with the same name on the remote. This makes it easy to accidentally push a branch you didn't intend to.
The 'simple'
value from the push.default
configuration variable denotes that git push
will push only the current branch to the branch that git pull
will pull from; it also checks that their names match. This is a more intuitive behavior, which is why the default should be changed to this configuration value.
Firstly, check if a remote repository is defined:
If it's not, define the remote repository named origin
:
Now, push the changes using the following command:
After this, you will have a resume of what was pushed.
In fact, the remote repository will check the current Head
(the reference to the commit) and compare it with its own. If there are differences between them, it will fail.
If you don't want a file anymore, there are two ways to remove it:
- Delete the file manually and commit the changes. This will delete the file locally and on the repository. Use the following command line
- Delete the file only through Git:
There is a way to display the working tree status, that is, the files that have changed and those that need to be pushed, and of course, there is a way to display the conflicts:
If everything is correct and up to date, you will get this result:
If you add a file, Git will warn you to track it by using the git add
command:
If you edit MyFile2.txt
and type git status
again, then you will have new lines:
On these lines, separate paragraphs display all files in each state. The MyFile2.txt
file is not tracked by Git and text5.txt
is ready to be committed.
If you add text5.txt
using the git add
command, you will notice the following changes:
Git can easily ignore some files or folders from your working tree. For example, consider a website on which you are working, and there is an upload
folder that you might not push on the repository to avoid having test images in your repository.
To do so, create a .gitignore
file inside the root of your working tree:
Then, add this line in the file; it will untrack the upload
folder and its contents:
Files or folders you define in this file will not be tracked by Git anymore.
You can add some easy regex, such as the following:
- If you want to ignore all PHP files, use the following regex:
- If you want to ignore all files having
p
or l
at the end of its name, use the following regex: - If you want to ignore all temporary files (finishing by
~
), use the following regex:
If the file is already pushed on the repository, the file is tracked by Git. To remove it, you will have to use the gitrmcommand
line by typing this: