This page is a quick start tutorial of using git. Git is a version control system. This tutorial is designed so that after reading this, you should be self-sufficient and all you need is git man pages. This tutorial assumes you have good experience with unix shell or another version control system.
If you don't have git installed, on Ubuntu linux, do sudo apt-get install git.
Then, you should setup a default name and email. It'll be used as info when you commit.
# set up default name and email git config --global user.name "Mary Lee" git config --global user.email "mary@example.com"
the above will create a file at ~/.gitconfig. You can run these commands again to change.
What commands are available?
Type git help.
Here's the most commonly used commands:
| Command Name | Description |
|---|---|
| init | Create a empty git repository or reinitialize a existing one |
| clone | Clone (svn “checkout”) a repository into a new directory |
| Command Name | Description |
|---|---|
| add | Add file contents to the index |
| commit | Record changes to the repository |
| mv | Move or rename a file, a directory, or a symlink |
| rm | Remove files from the working tree and from the index |
| checkout | Checkout a branch or paths to the working tree |
| Command Name | Description |
|---|---|
| status | Show the working tree status |
| log | Show commit logs |
| diff | Show changes between commits, commit and working tree, etc |
| show | Show various types of objects |
| Command Name | Description |
|---|---|
| pull | Fetch from and merge with another repository or a local branch |
| push | Update remote refs along with associated objects |
How to create a project?
cd to your source code dir, then git init; git add . ; git commit -m"message". Example:
cd xah_emacs_init # cd to the dir containing your code git init # initialize. This creates a 〔.git〕 dir. git add . # add all files to “staging area” git commit . -m"my first commit" # commit to local repository.
Git will create a dir named 〔.git〕 in the same dir. This dir is used by git for all its info, including its database.
Adding 20k files takes ≈6 minutes. Commiting 20k files takes 6 minutes.
How to clone a project?
cd to a directory where you want the files to be (⁖ 〔~/mygit/〕), then git clone ‹path or url›. Examples:
# get a copy of git source code (≈10MB) git clone git://git.kernel.org/pub/scm/git/git.git # get a copy of ErgoEmacs source code (≈18MB) git clone https://code.google.com/p/ergoemacs/
git clone will create a dir in the current dir. The dir will have the same name as the project name of the remote repository.
Note to svn users: git also has a “checkout” command. It's used for updating/reverting your current dir from a older version or from a different branch. git checkout is similar to svn merge and svn revert.
With git, you almost always commit to the local repository. Then, you “push” to a remote repository, or “pull” from a remote repository.
When using git, you should always cd to your project dir first, as opposed to calling commands then giving full path to your project dir. The current dir is meaingful to many git commands.
How to commit a change to local repository?
After you made changes to the file, then you can check back in (commit your changes). To commit, cd to the working dir, then do:
# commit one file git add myFile.py git commit myFile.py -m "summary of what you changed."
Any file you want to commit, you must first “git add”, even if the file already exists in the repository.
The “add” in git basically means add it to the “index” file for next commit. This “index” file is also called “staging area” or “cache”.
When you commit, git commits the “index” to your local repository.
More examples of commit:
# commit several files git add file1 file2 git commit . -m "summary of what you changed."
# commit all files whose name ends in “py” git add *py git commit . -m "summary of what you changed."
# commiting all files in current dir, including new or deleted file/dir git add -A . git commit . -m "summary of what you changed."
The -A option will include any new files in your dir, or files you removed. Basically, it'll commit your working dir as is to the repository.
How to add a new file or dir?
Use the command “add”, then “commit”.
git add myFile.py # add a new file git commit myFile.py -m "summary of what you changed."
How to delete a file or dir?
Use the “rm” command, then commit. The “rm” command is like “add”, in the sense that both changes the staging area.
git rm ‹file name›
git commit ‹file name› -m"summery of change."
git rm -r ‹dir name›
git commit ‹dir name› -m"summery of change."
git will delete the files/dir for you.
If you already deleted the files, you can still call “git rm” to update the staging area, then do a commit.
A better way is simply delete files/dir you don't want, then use the option “-A” in git-add:
# commiting all files in current dir, including new or deleted file/dir git add -A . git commit . -m "summary of what you changed."
How to commit a change to remote repository?
git push ‹repository address›
This will push your local repository to the remote git server.
Ususually, you use git remote add to associate a name to ther remote server address. Then, you can just push using a name. Like this:
# cd to your project dir cd ~/git/xah_emacs_init/ # associate a name to a remote server, for the git project of current dir git remote add github https://github.com/xahlee/xah_emacs_init.git # push code to the server associated with the name “github” git push github
How to get update from a remote repository?
use git pull ‹git server URL›. Example:
cd ~/git/xah_emacs_init # cd to my git project dir git pull https://github.com/xahlee/xah_emacs_init.git # pull from a remote repository
This will update your local repository (and working dir) from remote server.
If you have associated a name with a server URL, then you can use the name directly. Examples:
git pull github
git pull googlecode
How to get update from a remote repository, and ignore all local changes?
here's one way.
git fetch --all # get latest from remote but without merging into local git reset --hard origin/master # rid of all local state
here's another way.
# discard local changes git reset --hard # delete all untracked local files and dirs. (Dangerous. Be Careful.) git clean -xdf git pull ‹remote server or name›
How to revert a file?
git checkout -- ‹file name›
This will update your working directory, from your local repository. Effectively, revert whatever you've done to the files that hasn't been added/committed.
How to revert to a particular version?
git checkout v1.2.3 -- ‹file name› # tag v1.2.3 git checkout stable -- ‹file name› # stable branch git checkout origin/master -- ‹file name› # upstream master git checkout HEAD -- ‹file name› # the version from the most recent commit git checkout HEAD^ -- ‹file name› # the version before the most recent commit
〔How do I revert one file to the last commit in git? By Brian Campbell. @ stackoverflow.com…〕
How to tag a version?
git tag "‹version number or any label›"
This will tag the current state of your local repository. (not the index/“staging area”) You probably want to do a commit before you tag.
You can set git to ignore some files, such as emacs backup files, compiled files.
Each git project dir top level should have a ignore file file named 〔.gitignore〕. This file contain patterns that git should ignore (⁖ emacs backup files, compiled files, ….)
Here's a sample content of git ignore file:
#------------------- # emacs *~ *.elc [#]*[#] #------------------- # compiled files *.com *.class *.dll *.exe *.o *.so #------------------- # packed files *.7z *.dmg *.gz *.iso *.jar *.rar *.tar *.zip #------------------- # Logs and databases *.log *.sql *.sqlite #------------------- # OS junk .DS_Store ._* .Spotlight-V100 .Trashes Thumbs.db
To create a global ignore file (for any git project), run git config --global core.excludesfile ~/.gitignore_global, then create a file at ~/.gitignore_global.
You probably shouldn't use a global ignore file. Be very careful in what you put there if you do use one. Project specific ones makes it explicit, and other programers can also see it.
For detail, see git help gitignore
How to get a history of commits?
Use git log. Example of git log output:
◆ git log
commit a6205ee19e5dd0c049b7fff35de0d09189077594
Author: Xah Lee <xah@xahlee.org>
Date: Sat Dec 22 04:14:11 2012 -0800
fixed bug 324
commit c40dc81c7d6ca3d8ad3a4b9996a5b4ed4b0c6d08
Author: Xah Lee <xah@xahlee.org>
Date: Fri Dec 21 22:29:28 2012 -0800
changed getinput to not take parameters
Use git log -3 to show just last 3 commits.
This command is important because it shows the ID for each commit. You'll need to use the ID when doing comparisons or diff or revert.
See: git how-to
See: git: Setting up GitHub, GitCafe, Google Code.
See also: