Git breviary

On November 19, 2011, in Git, by lucasterdev

Git is a distributed version control system.

“Distributed” = everyone has and entire copy/history of the repository, not just the current copy like in svn. All working copies are a full copy of the repository.

Because of that, in Git most operatins are local; hence, it’s fast (whilst svn is all remote and slow).

It’s used for Linux kernel, Xorg X Server, Android, VLC and others…

 

Single developer mode

Tell git who we are:

git config –global user.name “Luca”

git config –global user.email “luca@somemail.com”

Use colors:

git config –global color.diff auto

git config –global color.status auto

git config –global color.branch auto

Settings are saved in %USER_HOME%/.gitconfig

Create a project

mkdir SomeProject

cd SomeProject

Set up the repository for our project

(we are in SomeProject folder)

git init

> Initialized empty Git repository in %USER_HOME/luca/SomeProject/.git/

(Yes, the repo has to be inside the project folder)

Get info on the repo:

git status

git log

Commit changes is a 2-step process

Why? Because we my want to have files that are only for personal use but have no bearing on the project itself. (example: notes and sketches)

Git commits only changes to files that we staged for commit.

1. Add a file to the repository and stage it for commit

git add nameoffile

2. Commit changes

git commit -m ‘some message’

Each commit gets a SHA1 hash number for data integrity.

Or you can perform steps 1. and 2. in once:

git commit -a

Empty the staging area:

git reset

Empty the staging area and revert the changes to the files:

git reset –hard

Create a remote repo

A remote repo can be an external drive or a remote server accessed through ssh.

cd G:  // an external drive, for instance

md Backup

cd Backup

git init –bare // this reates a remote repo

Add a remote repo to our project to push to

git remote add remote_name path_or_url_to_the_remote

If remote_name == “origin”, it will be the default remote server git pushes to.

Push changes to a remote repo

If the remote is empty, you also create the master branch

git push remote_name master

Following pushes:

git push remote_name

 

Multiple developer mode

The remote repo had better be a server!

Protocols to access the server: WebDav, git://, SSH

Create a remote SSH repository

ssh luca@sshservername

> enter password

mkdir export/git

cd export/git

mkdir ProjectServer

cd ProjectServer

git init –bare –shared

exit // exit the ssh session

Add the remote SSH server to push to

(we are in SomeProject folder)

git remote add remote_server_name luca@sshservername:/export/git/ProjectServer

If server_name == “origin”, it will be the default remote server git pushes to.

Push to the SSH remote

git push origin master

> enter ssh password

Before doing push, remember to do fetch+rebase!

Fetching changes is a 2 step process

git fetch remote_name // gets the changes

git rebase remote_name/branch_name // applies changes to our local repo

> enter ssh password

Remove a remote

git remote rm remote_name

 

Github

Add a remote github repo

git remote add origin git@github.com:username/repo_name.git // github creates ssh repos

Clone: creates a local repo cloning the contents of the remote repo

git clone git@github.com:username/repo_name.git

 

More on Rebase

1. Reverts your local repo changes back to where the local was the same as the remote.

2. Applies changes from the remote.

3. Adds your changes back on top.

 

Pull

1. Applies the remote’s changes on top of our local’s.