A Brief Git Tutorial
Installation
Windows Download and install msysgit
Linux Install git via your package manager
apt
,yum
,pacman
, etc.OS X Install git via Homebrew if OS-provided version is too old. Or you could download an installer.
Git provides a command line interface. If you wish to have a GUI, here are some free ones.
GitHub for Windows Windows
GitHub for Mac Mac
GitX Mac
Git Extensions Windows
SourceTree Mac & Windows
git-cola Windows, Mac & Linux
Visual Studio 2013 has integrated git support. For earlier versions, these extensions may help.
Git Extensions (GUI with a plugin for Visual Studio 2005/2008/2010/2012)
First-Time Git Setup
Your Identity. Git stores your identity with every commit you make. To tell Git who you are, run
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
You only need to do this once. With the --global
option, the information is stored in ~/.gitconfig
, which is your user-specific git configuration file.
Your Editor.
There are times when Git needs you to type in a message. E.g. when you commit your changes without an inline -m "your commit message" option
. By default, Git uses the system’s default editor, which is usually Vi. To change the editor, you can do:
git config --global core.editor emacs
The annoying fact that Windows and Linux line endings are differnt sometimes messes things up. It would be better if the repository have only LF
. When files are checkout on Windows, line endings are automatically replaced with CRLF
. On Linux, line endings does not change.
To achive this you need the following configurations.
On Windows, run
git config --global core.autocrlf true
On Linux/Mac, run
git config --global core.autocrlf input
For finer control over line endings per project, you probably need a .gitattributes file.
Create a Git Repository
In the project root directory (or create an empty directory for a new project), run
git init
Clone a Existing Repository
To work on an existing project managed with Git, you’ll first get a working copy locally with
git clone /path/to/repository
Suppose your curreny working directory is ~/workspace/
, running git clone https://github.com/edwardtoday/git-example.git
will download the project (including its whole history) to ~/workspace/git-example/
.
Commit files
To commit a changed file, whether it’s newly created or edited, you add it to the Index using
git add <filename>
To commit the changes currently in the Index, run
git commit -m "Commit message describing the changes"
Now the changes has been commit to HEAD of your local repository.
Sync with Remotes
Add Remotes. To get changes from a remote server to your local repository, you need to add that remote server with
git remote add <remote name> <server>
If the local repository was cloned from a remote server, that server has already been added as a remote called origin.
To list all the remotes currently added, run
git remote -v
- Push Changes to Remotes
You have finished a feature/function/bugfix/… and you want publish your local commits to a remote server. Run
git push <remote name> <branch>
So far, <branch>
is normally master
.
- Pull Changes from Remotes
You also wish to get the code written by others into your local repository. Run
git pull <remote name> <branch>
Useful Links
Note
Some materials below have Chinese translations. I still recommend reading the original English versions since the translations appears to be so crappy to make some sections harder to understand.