Using Git

1.0 Introduction

Git commands for common source code management use cases are listed.

2.0 Install Git

On a Ubuntu Linux system, Git can be installed with the command,

$ sudo apt-get install git

3.0 Set Identity

Before using Git, the user's identity (name, email) has to be set

$ git config --global user.email "user1@example.com" $ git config --global user.name "Joe Bloggs"

4.0 Initialize git repository

The first job for putting a project under source code management is to create a repository.

$ cd project-root-directory $ git init Initialized empty Git repository in /home/user1/project-root-directory/.git/

5.0 Status

The status of the working tree can be seen with the git status command.

$ git status

6.0 Add files, commit changes

We can add file contents to the staging area with the git add command. The changes can be committed to the repository with the git commit command.

$ cd project-root-directory $ # See the differences between the HEAD and the current files. $ git diff $ # Check what files would be added to the staging area (the index). $ git add -n . $ Add files to the staging area. $ git add . $ # See the differences between the HEAD and the files in the staging area. $ git diff --cached $ # Commit the changes. $ git commit -m "Minor modification." $ # Add and commit in a single command. $ git commit -a -m "Minor modification."

7.0 Undo changes

We can undo the changes made since checking in the last version, or, since the last commit.

$ # Discard changes in the working directory. $ git checkout -- <file> ... $ # Discard changes in all the files in the working directory. $ git checkout -- . $ # To unstage file(s) $ git reset HEAD <file> ... $ # To unstage all files. $ git reset HEAD . $ # remove all working directory and staged changes $ git reset --hard $ # remove untracked files $ git clean -fd

8.0 Add tags and see log

We can add tags with the git tag command. We can see the log of commits with the git log command.

$ # Add the tag vm.n for version m.n $ git tag -a vm.n -m "Version m.n" $ # See the log of commits to the repository $ git log --decorate --all --oneline --graph

9.0 Branching

Branching helps in creating a parallel line of development. One can branch from the main version, do some development and / or testing and optionally merge the work back in the main version.

$ # List branches $ git branch $ # Create a branch $ git branch <branch-name> $ # Switch to <branch-name> $ git checkout <branch-name>

After working on the branch, we can add files in the branch to the staging area and commit. After the commit, we may merge the branch in the master.

$ # Switch to the master. $ git checkout master $ # Merge <branch-name> in the master. $ git merge <branch-name> $ # Delete branch. $ git branch -d <branch-name> $ # Force switch to master, discarding any changes that are not committed. $ git checkout -f master

10.0 Remote Git Server

A remote Git server provides a copy of the repository so that the others can collaborate on a project.

10.1 Create a remote repository

On the remote system, the following commands need to be executed.

$ cd repository $ mkdir project-root-directory $ cd project-root-directory $ git --bare init Initialized empty Git repository in /home/user1/repository/project-root-directory/

You can also configure the server for the Git protocol for read-only access.

11.0 Pushing the repository from local system to the remote server

We can add the remote repository name and URL at the local system.

$ # Add remote repository. $ git remote add origin git-user@git-server:repository/project-root-directory $ git remote add u1 user1@git-server:repository/project-root-directory $ # Show remote repositories. $ git remote -v

After that, updates to the local repository can be pushed to the remote repository.

$ git push origin master --tags

12.0 Setting up local repository from remote repository

We can get the data from the remote repository to the local repository using the git fetch command. Using the git merge command, we can merge the updates to the local repository with the current branch.

$ # Initialize an empty repository $ git init $ git remote add origin git@git-server:/home/git/hello $ git fetch $ # Initial checkout. $ git checkout master

We can periodically update our local repository and then the current branch with the commands,

$ git fetch $ git merge

13.0 Clone the repository at the local system

Using the read-only Git protocol, we can clone the repository on the local system.

$ git clone git://git-server:repository/project-root-directory

Software: