Introduction

This article offers some tips for git. Today, git is an essential program to know as a developper or a sysadmin. Many open source projects use Github today and can be download with it. That’s why it’s important to know some basics command.

The Official Documentation still a good point to start !

Install Git

To install git you have just to install from packages in Linux:

Linux commands

On Windows and other OS, just download and install it:

Downloads

Once install, open a terminal and type following command to see if it’s install:

git --version

Before use Git

Before you use git, ensure you have read this article. It explain what git does and in which case it’s usefull.

# Configure git before first use:

To begin, you have to configure git, like your mail or your name. This is essential information which git needs so he can sign and commit properly.

Type following commands and replace informations with yours:

git config --global user.name "Lastname Firstname"
git config --global user.email "mymail@mail.com"

Note: the option --global set configuration for all git repositories. If not use, it’ll set only for current repository.

For future commits, you can set an editor:

# On Linux: choose as you want "vim", "nano", "gedit"
git config --global core.editor vim
# On Windows for notepad++
git config --global core.editor \
"'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Normally, git as all he need to works.

Basics Commands

Here is the commands you have to know. They are the base of git and are very essential to use git in a repository (or call repos sometimes).

  1. Initialize a repos: git init
  2. See state of repos: git status
  3. Add files to repos: git add myfile.txt myfolder/
  4. Commit changes : git commit

If you want to see changes before add or before commit, use diff:

# See changes before add
git diff
# See changes who's already added
git diff --cached

Aliases

Alias are very usefull. Especially for long git commands. Here is some usefull alias I recommend:

# For checkout:
git config --global alias.ck 'checkout'
# For status
git config --global alias.s 'status --short'
# For pretty log
git config --global alias.lg 'log --oneline --graph --decorate --branches --all'

After just use alias like a command:

# Example for "lg"
git lg

Cool prompt

In Linux, you can have name of your branch in terminal. You have to replace lines in your ~/.bashrc file. Open it and search for following lines (on Debian-like distributions):

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

And comment it with #. Then paste following lines above:

# Add git branch if its present to PS1
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Open a new terminal and go back to your repository folder. You will see something like that:

user@host:~/workspace/repos(master)$

Conclusion

Git is a very complex program and can be configured as you want. This tips are just here to help and find usefull commands. There is a lot of other possibilities. I’ll had new tips if needed.