tutorial - how to use git
Git for beginners: The definitive practical guide (20)
So what I need is a beginner's practical guide to Git. "Beginner" being defined as someone who knows how to handle their compiler, understands to some level what a Makefile is, and has touched source control without understanding it very well.
"Practical" being defined as this person doesn't want to get into great detail regarding what Git is doing in the background, and doesn't even care (or know) that it's distributed. Your answers might hint at the possibilities, but try to aim for the beginner that wants to keep a 'main' repository on a 'server' which is backed up and secure, and treat their local repository as merely a 'client' resource.
- How to install Git
- How do you set up Git? Try to cover Linux, Windows, Mac, think 'client/server' mindset.
- How do you create a new project/repository?
- How do you configure it to ignore files (.obj, .user, etc) that are not really part of the codebase?
Working with the code
- How do you get the latest code?
- How do you check out code?
- How do you commit changes?
- How do you see what's uncommitted, or the status of your current codebase?
- How do you destroy unwanted commits?
- How do you compare two revisions of a file, or your current file and a previous revision?
- How do you see the history of revisions to a file?
- How do you handle binary files (visio docs, for instance, or compiler environments)?
- How do you merge files changed at the "same time"?
- How do you undo (revert or reset) a commit?
Tagging, branching, releases, baselines
- How do you 'mark' 'tag' or 'release' a particular set of revisions for a particular set of files so you can always pull that one later?
- How do you pull a particular 'release'?
- How do you branch?
- How do you merge branches?
- How do you resolve conflicts and complete the merge?
- How do you merge parts of one branch into another branch?
- What is rebasing?
- How do I track remote branches?
- How can I create a branch on a remote repository?
- How do I delete a branch on a remote repository?
- Git workflow examples
- Describe and link to a good GUI, IDE plugin, etc. that makes Git a non-command line resource, but please list its limitations as well as its good.
- msysgit - Cross platform, included with Git
- gitk - Cross platform history viewer, included with Git
- gitnub - Mac OS X
- gitx - Mac OS X history viewer
- smartgit - Cross platform, commercial, beta
- tig - console GUI for Linux
- qgit - GUI for Windows, Linux
- Git Extensions - package for Windows, includes friendly GUI
- Any other common tasks a beginner should know?
- How do I work effectively with a subversion repository set as my source control source?
Other Git beginner's references
- Git guide
- Git book
- Git magic
- GitHub guides
- Git tutorial
- Progit - book by Scott Chacon
- Git - SVN Crash Course
- Git from the bottom up
- Git ready
- Git visual cheatsheet
Delving into Git
I will go through the entries from time to time and 'tidy' them up so they have a consistent look/feel and it's easy to scan the list - feel free to follow a simple "header - brief explanation - list of instructions - gotchas and extra info" template. I'll also link to the entries from the bullet list above so it's easy to find them later.
Checking Out Code
First go to an empty dir, use "git init" to make it a repository, then clone the remote repo into your own.
git clone [email protected]:/dir/to/repo
Wherever you initially clone from is where "git pull" will pull from by default.
Console UI - Tig
apt-get install tig
While inside a git repo, type 'tig', to view an interactive log, hit 'enter' on any log to see more information about it. h for help, which lists the basic functionality.
"Tig" is "Git" backwards.
Say you make a pull, merge it into your code, and decide you don't like it. Use git-log, or tig, and find the hash of wherever you want to go back to (probably your last commit before the pull/merge) copy the hash, and do:
# Revert to a previous commit by hash: git-reset --hard <hash>
Instead of the hash, you can use HEAD^ as a shortcut for the previous commit.
# Revert to previous commit: git-reset --hard HEAD^
How can I create a branch on a remote repository?
Assuming that you have cloned your remote repository from some single remote repository.
# create a new branch locally git branch name_of_branch git checkout name_of_branch # edit/add/remove files # ... # Commit your changes locally git add fileName git commit -m Message # push changes and new branch to remote repository: git push origin name_of_branch:name_of_branch
How do you branch?
The default branch in a git repository is called
To create a new branch use
git branch <branch-name>
To see a list of all branches in the current repository type
If you want to switch to another branch you can use
git checkout <branch-name>
To create a new branch and switch to it in one step
git checkout -b <branch-name>
To delete a branch, use
git branch -d <branch-name>
To create a branch with the changes from the current branch, do
git stash git stash branch <branch-name>
How do you compare two revisions of a file, or your current file and a previous revision?
Compare command is
To compare 2 revisions of a file:
$ git diff <commit1> <commit2> <file_name>
That diffs commit1 against commit2; if you change order then files are diffed the other way round, which may not be what you expect...
To compare current staged file against the repository:
$ git diff --staged <file_name>
To compare current unstaged file against the repository:
$ git diff <file_name>
How do you merge branches?
If you want to merge a branch (e.g.
release), make sure your current branch is the target branch you'd like to merge into (use
git branch or
git status to see your current branch).
git merge master
master is the name of the branch you want to merge with the current branch).
If there are any conflicts, you can use
to see pending conflicts you have to resolve.
How do you see the history of revisions to a file?
git log -- filename
How to install Git
There are several downloads:
- Git: Use this unless you specifically need one of the other options below.
- PortableGit: Use this if you want to run Git on a PC without installing on that PC (e.g. running Git from a USB drive)
- msysGit: Use this if you want to develop Git itself. If you just want to use Git for your source code, but don't want to edit Git's source code, you don't need this.
This also installs a Cygwin bash shell, so you can use the
git in a nicer shell (than cmd.exe), and also includes git-gui (accessible via
git gui command, or the
Start > All Programs > Git menu)
Mac OS X
Use the git-osx-installer, or you can also install from source
Via a package manager
git using your native package manager. For example, on Debian (or Ubuntu):
apt-get install git-core
Or on Mac OS X, via MacPorts:
sudo port install git-core+bash_completion+doc
fink install git
brew install git
On Red Hat based distributions, such as Fedora:
yum install git
In Cygwin the Git package can be found under the "devel" section
From source (Mac OS X/Linux/BSD/etc.)
In Mac OS X, if you have the Developer Tools installed, you can compile Git from source very easily. Download the latest version of Git as a
.tar.gz from http://git-scm.com/, and extract it (double click in Finder)
On Linux/BSD/etc. it should be much the same. For example, in Debian (and Ubuntu), you need to install the
build-essential package via
Then in a Terminal,
cd to where you extracted the files (Running
cd ~/Downloads/git*/ should work), and then run..
./configure && make && sudo make install
This will install Git into the default place (
/usr/local - so
git will be in
It will prompt you to enter your password (for
sudo), this is so it can write to the
/usr/local/ directory, which can only be accessed by the "root" user so sudo is required!
If you with to install it somewhere separate (so Git's files aren't mixed in with other tools), use
--prefix with the configure command:
./configure --prefix=/usr/local/gitpath make sudo make install
This will install the
git binary into
/usr/local/bin/gitpath/bin/git - so you don't have to type that every time you, you should add into your
$PATH by adding the following line into your
If you do not have sudo access, you can use
--prefix=/Users/myusername/bin and install into your home directory. Remember to add
The script x-git-update-to-latest-version automates a lot of this:
This script updates my local clone of the git repo (localy at
~/work/track/git), and then configures, installs (at
git describe) and updates the
This way, I can have
PATHand I'm always using the latest version.
The latest version of this script also installs the man pages. You need to tweak your
MANPATHto include the
How to track remote branches
Assuming there is a remote repository that you cloned your local repository from and also assuming that there is a branch named 'some_branch' on that remote repository, here is how to track it locally:
# list remote branches git branch -r # start tracking one remote branch git branch --track some_branch origin/some_branch # change to the branch locally git checkout some_branch # make changes and commit them locally .... # push your changes to the remote repository: git push
Getting the latest Code
$ git pull <remote> <branch> # fetches the code and merges it into # your working directory $ git fetch <remote> <branch> # fetches the code but does not merge # it into your working directory $ git pull --tag <remote> <branch> # same as above but fetch tags as well $ git fetch --tag <remote> <branch> # you get the idea
That pretty much covers every case for getting the latest copy of the code from the remote repository.
How do I delete a branch on a remote repository?
Perform a push in your remote using
: before the name of the branch
git push origin :mybranchname
origin the name of your remote and
mybranchname the name of the branch about to be deleted
Git Magic is all you'll ever need. Guaranteed or your money back!
git status is your friend, use it often. Good for answering questions like:
- What did that command just do?
- What branch am I on?
- What changes am I about to commit, and have I forgotten anything?
- Was I in the middle of something last time I worked on this project (days, weeks, or perhaps months ago)?
git status runs nigh-instantly even on large projects. I often found it reassuring while learning git to use it frequently, to make sure my mental model of what was going on was accurate. Now I mostly just use it to remind myself what I've changed since my last commit.
Obviously, it's much more useful if your .gitignore is sanely configured.
A real good paper for understanding how Git works is The Git Parable. Very recommended!
I got started with the official Git tutorial. I think it's practical enough for beginners (I was, and still am, a beginner, by your definition! I barely grasp makefiles, I've only played a bit with Apache Subversion, etc.).
I've also found Git Internals to be very useful. It is written by Scott Chacon (author of Pro Git, and maintainer of the Git Community Book). What I like about Git Internals is it focuses on the concepts first and then the commands, and being that it is ~100 small pages it is quickly digestible.
Well, despite the fact that you asked that we not "simply" link to other resources, it's pretty foolish when there already exists a community grown (and growing) resource that's really quite good: the Git Community Book. Seriously, this 20+ questions in a question is going to be anything but concise and consistent. The Git Community Book is available as both HTML and PDF and answers many of your questions with clear, well formatted and peer reviewed answers and in a format that allows you to jump straight to your problem at hand.
Alas, if my post really upsets you then I'll delete it. Just say so.
Why yet another howto? There are really good ones on the net, like the git guide which is perfect to begin. It has good links including the git book to which one can contribute (hosted on git hub) and which is perfect for this collective task.
On , I would really prefer to see your favorite tricks !
Mine, which I discovered only lately, is
git stash, explained here, which enables you to save your current job and go to another branch
EDIT: as the previous post, if you really prefer stackoverlow format with posts as a wiki I will delete this answer