Git Tutorial: Basic Git Overview
|Topic Area:||Revision control|
|1. Basic Git overview|
|2. Creating and Changing Repositories|
Git is a version-control system, commonly used for managing source code and coordinating the work of multiple contributors on a software project.
From a user perspective, the workflow is divided into two stages: first, there are the changes you make to your local copy of the project. This local copy is called the local repository. Then, there is also the remote repository to which the contributors push, i.e., upload, their changes or pull the current version of the project. The main advantage is that all contributors can work on their part of the project simultaneously and merge their changes later on.
The changes are organized as commits. Whenever you feel that you have reached a certain goal, e.g. you fixed a bug or implemented a new feature, you should commit your changes. It is good practice to make only few changes per commit.
You can also organize the work in branches, where each branch has it's own timeline of commits. One branch can be branched from another, copying the state of the branch at this specific point. From then on, they can be worked on independently. Branches can later be merged again. This feature is used e.g., if a new feature is implemented in your project. Then, you would create a new branch where this feature is developed and merge it into the master branch as soon as the development is finished.
Creating a Repository
Before you can start using git productively, you will have to create a git repository first. You can create a git repository using a hosting service of your choice. If you are a member of the RWTH Aachen, you can use the GitLab. Otherwise, e.g. GitHub is a free and popular hosting service. Both provide you with a powerful web interface and rights management, so that you can control who can access or make changes to your project. It might become necessary that you generate an SSH key to prove your identity when you access the remote repository.
When you want to start working with your repository, you will have to clone it to your machine first. Hosting services usually provide a button labeled 'clone' from which you can copy the repository's URL.
firstname.lastname@example.org: $ git clone https://github.com/MyRepository.git
Assuming that your repository is called MyRepository, git will automatically create a directory with this name and download all files in this repository to your machine.
If you already cloned your repository to your machine and you want to start working after a break, you should pull from the remote repository. This will ensure that you have the most recent changes on your machine when you start working.
email@example.com: $ git pull
Before you start working, when you are done with your work or if you want to commit your changes, it is a good idea to request the repository's status from git.
firstname.lastname@example.org: $ git status
When you have worked on your source code and reached a point where you feel confident to commit your changes, you have to tell git which files you want to commit. Ask git for the repository status and it will give you a list of all files that have changed. Then, you can selectively stage files for your commit, i.e. add them to the list of files for your commit.
email@example.com: $ git add File0 [File1 ...]
Always confirm that you have staged the correct files by taking a look at the status! If everything is correct, you can commit your changes and add a description of your changes.
firstname.lastname@example.org: $ git commit -m "Change everything."
Git will confirm your commit and then you are ready to push your changes to the remote repository.
email@example.com: $ git push
If you and another contributor have worked on the same files and git cannot automatically resolve these conflicts, the merge conflicts have to be resolved manually. A good tutorial on how to merge your changes can be found in the GitHub Documentation: Resolving a merge conflict using the command line.
A more detailed explanation of branches can be found here: Git - What a Branch Is.
Git With GUI
For unexperienced users, using the shell can still be difficult. Then, you might want to use a graphical user interface (GUI) for git. Some of the most popular git GUIs are TortoiseGit, the GitHub Desktop Client or SourceTree. Further, some IDEs have a git integration, e.g. Eclipse's EGit