Basic Section
In this section, we will talk about some Git concepts, how to work with a remote Git server and some useful tools inside Git.
The status of a repository
We have already used the git status
command to check for new or deleted files, but it has more uses.
With git status
we can check the state of our repository. The repository can be up-to-date, have untracked files, have unstaged changes, can be behind in terms of updates, have conflicts, etc.
The log of a repository
We can use the following command to access the log of our repository:
git log
The log
shows all commits done in the repository, with information of the changes, author and date. It is useful to get some quick information about the changes done to the repository.
States of files
Files in Git have 4 possible states:
- Untracked: These files are not being tracked by Git (they were not present on last snapshot).
-
Tracked: These files are tracked by Git (they were present on last snapshot).
- Unmodified: The file hasn't been modified since last snapshot.
- Modified: The file has been modified since las snapshot.
- Staged: Changes to the files are now ready to be commited.
(More info at: http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository)
Pushing to the remote server
Now that we have and idea of the possible states of a repository and files, it is time to have some fun with GitHub.
The term push
refers to sending our local changes to the server. To push a commit use:
git push
then enter your user and password.
Pulling from the remote server
We can see pull
as the reverse function of push
. Pulling refers to getting the changes from the server to our local repository. To pull use:
git pull
Note that there were unstaged changes on the local repository, so they were first commited and then merged with the changes in the remote server. We will talk about this in the Intermediate Section.
Tags
Tags are basically names that we assign to commits. To add a tag we use the following command:
git tag -a <tag-name> -m <description>
Example:
As in the picture, the command git show <tag-name>
gives us information on the tag and the commit associated with it.
To push
or pull
tags use:
git push --tags
git pull --tags
Aliases
We can use aliases to shorten out commands or give them more easy to remember names. To create aliases use the following command:
git config --global alias.<new-alias> <command>
Examples:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Note that --global
makes the alias available in all local repositories.
.gitignore - Ignoring files
Sometimes we want to ignore files: for example, we may have a huge historical database that we are going to analyze. Our little, 500 GB database is not going to change, so letting Git ignore the database is going to be a relief.
To let Git ignore files, we have to create a file named .gitignore
in our repository folder. Once created, we can add file names to it, and these files will be ignored. It is also possible to write regular expressions to ignore certain files that match a pattern (compilation files, temp files, debugging files, etc).
We can see that only the .gitignore
file was added, and the crap.data
was not.
JetBrains IDEs and Git
I have been Intellij IDEA for developing Java programs and PyCharm for Python scripts for quite some time now. I really like the features of this family of IDEs.
They have native integration with Git. According to them: “You can push and pull local repositories, rebase, apply various merge strategies, highlight commits and much more.”
You can get more information at JetBrains.
Hey! You made it this far, continue with our Intermediate Section for more!