First notes using Git

If you don’t know, Git is a version control system, like SVN, but better. I’ll try not to stop in their differences and go directly to the Git way.

Install git:
sudo port install git-core

Check out a project:
git clone user@server:project.git

Add a remote repository. We can use whatever name but usually is origin:
git remote add origin user@server:project.git

Create a new project:
mkdir myproject
cd myproject
git init

See the changes of the local copy:
git status

We can ignore files creating a file called .gitignore and add the files:
.DS_Store
thumbs.db

Branches are different versions of our code that we don’t want to mix. With Git we will work in the same files, and change the branch what we are working on from git. The files will be updated. The main branch is called master. We can have several branches in our local machine, but only commit the master to the git server.

See the branches we have in our machine and which one is active:
git branch

See the remote branches:
git branch -r

Create branch:
git branch mybranch

Delete branch
git branch -d mybranch

Switch branch:
git checkout mybranch

Update the current branch with changes from the server:
git pull

Add all new files to our local branch:
git add .

Commit all files changed to our local branch:
git commit -a -m'my message'

Commit only modified files, not untracked:
git commit . -m'my message'

Update the server with your commits across all common branches:
git push

Update the server with our new local branch:
git push origin mybranch

See the commits in our local branch:
git log

See the differences between your working files and the last commit
git diff

See the differences between one commit and the current version (HEAD)
git diff 129fc3d1e9f70a1e4c2e38fdbf3acd576a63b314 HEAD

Undo changes since last commit:
git reset --hard

Undo last commit:
git reset --hard HEAD^
To undo more than one commit add carets:
HEAD^^^ -> 3 commits, or HEAD~3

Revert a specific commit. Next commits will remain:
git revert 5998a70e469f323a3386355fcd09323576abaf93

Extract specific files as they were in another commit:
git checkout 5998a70e469f323a3386355fcd09323576abaf93 -- path/to.file

Merge remote branch into the current branch:
git pull origin remotebranch

Merge local branch into the current branch:
git pull . myotherbranch

Usually the workflow will be:
git pull
-- make changes --
git pull
git add .
git commit -a -m "my message"
git push

To have a more detailed cheat sheet visit http://cheat.errtheblog.com/s/git/, and for a more detailed explanation on how git works visit http://lwn.net/Articles/210045/

Leave a comment