Skip to content
laurenttainturier edited this page Feb 4, 2019 · 3 revisions

Introduction

Git is a powerful distributed version control system.

As a developer you need to know how this tool works and you'll see that it can save your life more than once !

Warnings

  • In the following, $name is a kind of a variable, used to describe anything: a commit, a branch, a repository, etc. Do not type it directly in your terminal!
  • Anything framed by [] is optional.

How to use it?

Prerequisite

To be used, git must be installed on your machine, it's already the case if you use have a UNIX os like Linux or Mac. For the windows user, you need to install it.

The following commands can be executed in the default terminal for UNIX and for Windows you can use git bash which is more efficient than the Windows command line for executing git command.

Create or clone a repository

git clone $repository_name

If you are working on an existing repository or if you have create a new one on github, you can use this command to create a copy (or clone) of the repository locally. You can find your repository name on the home page of Github, you can see a clone or download button, your repository name is the https url.

You have the option of copy the ssh url, you can find more information about this down below

If you want to clone this repository for instance type in your terminal:

git clone https://github.com/ETICINSATechnologies/AnnuaireAncien_v2.0.git

When it's done, a new directory is created, type cd AnnuaireAncien_v2.0 to go inside it.

Check the state of your local repository

git status

This is the most useful command of git, it shows the current state of the repo, ie on which branch you are, what files will be commited, etc.

Classical workflow for a dev

Branches

Branches is a very powerful tools which comes with git. Thanks to that, many developers can develop their features without interfering with others.

In this project we have a main branch called develop, which contains the code fully functional and tested. Each features that are still in development has its own branch called feature/$feature_name. When a feature achieved (functional and tested with integration tests), the branch of the feature is merge on the develop, then is deleted.

Navigate through the existing branches and commits

git branch

Without any parameter, it shows all the existing branches.

git log [-h]

Without any parameter, it shows all the commit history of the current branch. You can use it with the parameter -h if you want to see the added and deleted lines of each commit.

If you do not like the default theme of the display, you can change it by adding the following lines in your global .gitconfig file:

[format]
    pretty = format:%C(auto,yellow)%h %C(auto,blue)%>(26,trunc)%ad %C(auto,green)%<(19,trunc)%aN%C(auto,reset)%s%C(auto,red)% gD% D

git checkout [-b] $commitOrBranch

  • Without the b, this command allows you to navigate trough a branch or a commit. If you have precised a specific commit, this will create a new branch, it is not often what we want, you can then use git reset --hard instead (like described just below). It is not always possible to use this command, especially when you have uncommited files. Go to the next section if you are in this case.
  • With the -b, you can create a new branch and go directly inside it. It is a shortcut for the following:
git branch -b $branch
git checkout $branch

Cannot change your branch ?

This situation happens when you have not commited files, git doesn't know what to do with them, so you have to fix them before changing your branch. The easiest way to fix that is to first commit you files, but it is not always something wanted, especially if you are working on something under development.

git reset --hard [$branchOrCommit]

Warning! Be sure of what you are doing ! This will delete all the uncommited files ! If you do not precise a commit or a file name, this will restore your repository to the state of the latest commit of the current branch. Otherwise you can restore

git stash

If you need to change your branch, and do not want to loose all your changes and do not want to commit, there is a solution for you ! git stash will save your uncommited changes in its memory, and restore your repository to the state of the last commit. You can then change your branch. When you want to get your uncommited files, you can use git stash pop to restore it. This can be very useful if you are developing something and you find out that you are not in the correct branch. You just have to stash, go to the correct branch and pop your changes.

Warning! If you use the stash command more than once, it acts like a FILO structure ! You cannot pop more than once the same state.

Create a new feature branch

When a developer wants to develop a new feature, he needs to create a new branch for this feature. This branch needs to be forked from the develop branch to minimize conflicts when merging these two branches later.

Firstly, be sure to be in the develop branch, use git status to be sure and use git checkout develop if you are not already in this branch.

git pull

This command will update your local repository from the remote one.

Now, you can create your new branch :

git checkout -b feature/$featureName

It's time to code! At least!

When you have made changes in the code and you think it is the great moment to make a backup, you then need to add the files you want to save then make a commit.

git add $filename

This command add $filename file to the files that will be commited. You can directly the content of a directory, or replace $filename by a . that will add everything, or if you want to only some category of files (like the js files), you can replace it by *.js.

Note You can create a .gitignore file in the root directory if you do not want to commit certain category of file. For instance, in this project, we do not want neither to commit .idea directory, node_modules, nor git files. You just have to add inside it the name of the files you do not want to commit:

.idea
.git
.gitignore
node_modules

git commit [-m $commitName]

When you have added all the files you wanted to commit, you can then make a commit. I recommend to use this command with the -m option because you can directly set the commit name. Otherwise it will open the vi editor, which is quite difficult to master.

git push

You can then upload your commit to the remote repository by using this command. Be warned that it will fail if your repository isn't up-to-date with the remote. You then need to make a git pull, but it can create conflicts if you made changes on already modified files. If you are in this case, you will need to resolve this conflicts and then commit them before being able to push.

Pull Request (PR)

For now, your code is only on your branch, but when you have at least developed and tested your feature and everything works perfectly, this is the moment to merge your code to the develop.

This process can be done directly on github: go to the home page of the project, click on the new pull request button, then choose the develop branch as the base and your branch as compare, give a name - and eventually a comment, to the PR can make then click on the create pull request button.

The continous integration tool (like travis) will then run all the tests. If any of then fail, the PR cannot be merged. You need to correct your code in order to pass all the tests. Just modify you code and commit the changes, you do not have to make a new PR, the new commit will be automatically added to the PR.

A reviewer needs to review your PR and approve it before it can be merged. It can also required some changes.

When all tests have passed and the PR is approved, it can finally be merged.

Advanced

SSH

If you do not want to always login when you push some code to the remote repository you can use an SSH key. You can follow the github tutorial to generate one and associate it to your github profile.