Git course

Posted by Daniel Vela on September 18, 2013

1. Introduction

Whit git the work repository is located in the root directory of the project. Specifically is located in a hide directory named .git.

In this directory all commits, versions and branches are stored.

Git principal characteristic is the posibility of working with different remote repositories. With git is easy to share code between programmers or send the final code to a production or preproduction repository without loosing track of the version and changes always.

Installing git in macOS

Git is inlcuded with the last Xcode command line tools. Also it can be downloaded from git-osx-installer for Mac OS X


$git config --global "Your name here"   
$git config --global ""   
# optional, but recommended
$git config --global merge.tool vimdiff    
$git config --global core.editor vim   
$git config --global color.ui true   

2. Basic commands

To initialize the version control of git use the command init.

$ cd project
$ git init

This will creata a subdirectory .git . In this directory all commits will be stored. But initially is all empty: the command init doesn’t add the files to the repository. To do that use the command add

$ git add .

Command add is recursive. Indicating a directory as parameter, it add that directory and all files and subdirectories recursively.

Also it can be used to add only one file.

$ git add README

Although the add command doesn’t add the file to the repo directly. wht it does is to add the file to a virtual zone of the repository called stage. To add to the repository avery change or file, first it must be added to the stage those modified or created files. Once do it, you can add the changes to the repo with the command commit.

$ git commit -m 'first commit'

Command commit requires a message. Without a text, appears a text editor to set a message text.

To know the state of a file use the command:

$ git status

Is recommended to configure git with the flag color.ui set to true

3. Branches

To create a branch:

$ git branch new_branch

This will create a new developement branch copying the las made commit. To create a new brach is recommended to have save or undo every change in the current path. Otherwise, git will alert that the new branch cannot be created.

To change from one branch to anohter use the command checkout

$ git checkout new_branch

To merge branches:

$ git merge branch1

This will mix the current branch with the indicated as paran. Also creates a new commit in the current branch.

In case of conflicts, they should be resolved before create a new commit.


There are cases in which, making a merge, happends a fastforward

When a change is made in a branch, the original branch -from where the current branch was created- it’s possible that remains untouched. If so, when a merge is donem git realizes that doen’t have to mix the code, it only has to change the HEAD pointer to the last commit of the original branch to the HEAD pointer of the new branch. This is called fastforward

The problem of this fastforwards is that the new branch dessapears without a trace. If we want to keep that branch or create a new commit in the original branch, you can user the parameter –no-ff .

$ git merge --no-ff branch1 

This will avoid the fastforward.

4. Undoing changes

There are some ways to undo changes.

Avoid to add a change to a commit

If we had added a file to the stage incorrectly, we can remove with the command reset. This way we avoid to add the change to the commit.

$ git reset HEAD file

Changing the las commit

If in the las commit you forgot to add the file, you can use the parameter –ammend to add a change to the las commit done.

$ git commit -m 'first commit'
$ git add file
$ git commit --ammend

Deleting changes in a local file

If we had changed incorrectly a file or we want to return to a previous version, we can use the commmand checkout. This command extracts from the repository the wanted version and overwites the files contained in the working directory. By default this command extracts the las version of the current branch.

$ git checkout -- file

Deleting the las commit

If we had made an incorrect commit, we can undo it with reset command:

$ git reset --hard HEAD^

In order to eliminate several commits use the parameter HEAD~3

5. Resolving conflicts

A conflict can be solved in two diffent ways:

Edit the file wiht the conflict

Git marks conflicts en the files with a text using the followings marks:

  1. «««< HEAD
  2. =======
  3. >>>>>>> branch

The text between the marks 1 and 2 is the text from the active branch. The text between the marks 2 and 3 is the one of the merged branch.

Once eliminated these marks, we must add the file to the stage with the command add. Once all the conflicts are resolved, we must make the commit.

Undo the merge of a conflicted file

If we don’t want to resolve the conflicts, we can extract a new version of the previous file to substitue the conflicted one.

If we want to keep the current branch version, we use the modifier –ours:

$ git checkout --ours file

If we want to keep the merged branch version of the file, we use the modifier –theirs:

$ git checkout --theirs file

6. Remote repositories

To initialize a new project downloading the files from a remote repository, you must use the command clone:

$ git clone proyect_url

This command not only copies the files of the remote project, also it configures the current branch linking it with the remote brach (track).

When a local branch has one or more associated remote branches, we can share code with that branches.

With the command pull we can download the last changes done in the remote branch. With the command push we can send to the remote branch the last changes done.

$ git pull origin master
$ git push origin master

Need two parameters. First the name of the remote repository. Sencond, the name of the remote branch.

To know what remotes has configured a local branch use the following command:

$ git remote show origin

To add a new remote to a local repository use the command remote add:

$ git remote add remote_name remote_url

Once configured the remote, all files and references can be downloaded with the command fetch:

$ git fetch remote_name

Create a remote respository

Two things are needed to create a remote repository:

  1. A remote access
  2. A bare repository

Remote access

You can use ssh, or a shared directory. The allowed protocols are:

  • file://
  • ssh://
  • git://
  • http://
  • https://

bare repository

A repository is named bare when has not any associated directory. This allows to use like a remote repository in order to use by others working repositories.

You can create an empty initial bare repository with the following command:

$ git init --bare

Also it can create a bare repository cloning an existing repository:

$ git clone --bare url_rep new_rep

commits information

In order to extract information about the commits of the current branch we have the command log:

$ git log

A command very useful to see graphically all the branches and merges, also to see the messages associated to the following:

$ git log --graph --decorate --pretty=oneline --abbrev-commit --all

Web sites that allow to use git

Websites that host Git

How to install git

Install git on Linux, Mac y Windows