Setup git repository and start using git.

Last week I tried to use git and here are some tips to set up git environment and a beginner tutorial.

  1. Basics
    The key concept of Git is branches. A branch is a separate code line with its own history. You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (normally named master). The user selects a branch and works in this selected branch, which is called the “working copy”. Selecting a branch is called “checkout a branch”.
  2. Installation

    1
    $ sudo apt-get install git-core
  3. Setup
    To list your git configuration:

    1
    $ git config --list

    To configure your name and email:

    1
    2
    $ git config --global user.name "Example Surname"
    $ git config --global user.email "your.email@gmail.com"

    To enable highlighting:

    1
    2
    $ git config --global color.status auto
    $ git config --global color.branch auto

    To set SVN-like alias:

    1
    2
    3
    4
    $ git config --global alias.st status
    $ git config --global alias.ci commit
    $ git config --global alias.co checkout
    $ git config --global alias.br branch

    To add ignore rules:
    Firstly, create file ~/.gitignore and add these to the file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    # Compiled source #
    ###################
    *.com
    *.class
    *.dll
    *.exe
    *.o
    *.so

    # Packages #
    ############
    # it's better to unpack these files and commit the raw source
    # git has its own built in compression methods
    *.7z
    *.dmg
    *.gz
    *.iso
    *.jar
    *.rar
    *.tar
    *.zip

    # Logs and databases #
    ######################
    *.log
    *.sql
    *.sqlite

    # OS generated files #
    ######################
    .DS_Store*
    ehthumbs.db
    Icon?
    Thumbs.db

    Then run

    1
    $ git config --global core.excludesfile ~/.gitignore
  4. Getting started with Git
    To create and add files to repository:

    1
    2
    3
    4
    5
    6
    7
    8
    # Initialize the local Git repository
    $ git init
    # Add all (files and directories) to stage
    $ git add .
    # Make a commit of your file to the local repository
    $ git commit -m "Initial commit"
    # Show the log file
    $ git log

    To create a new branch:

    1
    $ git branch new_branch_name

    To switch to a branch:

    1
    $ git checkout branch_name

    To delete a file from the existing branch:

    1
    2
    3
    4
    5
    6
    #delete a file from dir
    $ rm one_file
    # add deleted files to the stage
    $ git add -A .
    # commit changes
    $ git commit -m "deleted one file"

    To push changes to branches/master of remote repository

    1
    $ git push

    To pull changes from remote repository:

    1
    $ git pull