Mémo Git usage (part I)

Configuration

Parameters

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

Informations about you

git config --global user.name "your_pseudo"
git config --global user.email my_email@domain.com

All these informations are in the ~/.gitconfig file.

[color]
        diff = auto
        status = auto
        branch = auto
[user]
        name = votre_pseudo
        email = moi@email.com
[alias]
        ci = commit
        co = checkout
        st = status
        br = branch

Initialisation

  1. Either you create a repository : When you are in the correct directory.
    git init
  2. Or you clone an existing one : in this case, you only import the code. You don’t have access to the rep (you can’t modify it, on the online rep)
    git clone http://github.com/symfony/symfony.git

Get informations (status, diff, ….)

git status
git diff src/Symfony/Components/Yaml/Yaml.php

Commit

You must specify the file you want to commit. Making a git status will mark your modified files in red. They will not be taken into account during the commit.

      1. git add file1 file2 : this add the files for the “to commit” list. Then you have to make a git commit. Used when you create new files Git doesn’t know.
      2. git comment -a  will commit all the files (Changes to be committed and Changed but not updated). Used to save all the changes you’ve made.
      3. git commit file1 file2 indicate the files and commit them. Used to commit specific files.

When a commit is done, vim or nano open. You have to write a message describing your changes.

Online Repository

Creation

  • Without an existing local repository
    mkdir myRepository
    cd myRepository
    git init
    touch README
    git add README
    git commit -m 'first commit'
    git remote add origin http://gitlab.be/username/onlineProject.git
    git push -u origin master
  • With an existing local repository
    cd existing_git_repo
    git remote add origin http://gitlab.be/jerome/onlineProject.git
    git push -u origin master

Usage

You can work locally, like explained above. Then, you can save to the online repository. So a typical scenario looks like the following code :

# add all the current directories and files to the staging area
git add .
# commit then locally
git commit -a -m "my message"
# push them to the remote repository
git push

 

Working with Branches

Create a branch when you are doing a huge modification (long duration, many commit required, …)

# lists available branches
git branch
# create a branch called "testing" from the "master" branch
git branch testing master

# or Create branch and directly switch to it
git checkout -b testing

To start working in a branch you have to checkout the branch. If you checkout a branch, the HEAD pointer moves to the last commit in this branch and the files in the working tree are set to the state of this commit.

# switch to your new branch
git checkout testing

# do some changes
echo "Cool new feature in this branch" > test01
git commit -a -m "new feature"

# switch to the master branch
git checkout master

# check that the content of
# the test01 file is the old one
cat test01

Delete a branch

# delete branch testing
git branch -d testing

Push the branch to the remote repository, make some change in the branch and push them too.

# push current branch to a branch called "testing" to remote repository
git push origin testing

# switch to the testing branch
git checkout testing

# some changes
echo "News for you" > test01
git commit -a -m "new feature in branch"

# push all including branch
git push

See the differences between 2 branches

# shows the differences between
# current head of master and your_branch
git diff master your_branch

zerzr
zer

cd existing_git_repo git remote add origin http://gitlab.laluminium.be/jerome/webkot4.git git push -u origin master