Create

Clone an existing repository

1
$ git clone ssh://user@domain.tld/repo.git

Clone an existing repository and all its sub-modules recursively

1
$ git clone --recurse-submodules ssh://user@domain.tld/repo.git

Create a new local repository

1
$ git init

Configuration

Set the name attached to all your commits

1
$ git config [--global] user.name <name>

Set the email attached to all your commits

1
$ git config [--global] user.email <email>

Set colorization of command line output for all repos

1
$ git config [--global] color.ui auto

Print set name (in current repository or globally)

1
$ git config [--global] user.name

Print set email (in current repository or globally)

1
$ git config [--global] user.email

Local Changes

Changed files in your working directory

1
$ git status

Changes to tracked files

1
$ git diff

Add current changes in file to the next commit

1
$ git add <file>

Add all current changes to the next commit

1
$ git add .

Add some changes in to the next commit interactively

1
$ git add -p <file>

Rename file and add it to next commit

1
$ git mv <file> <new file name>

Delete file and add its deletion to next commit

1
$ git rm <file>

Commit all local changes in tracked files

1
$ git commit -a

Commit previously staged changes

1
$ git commit

Change the last commit, Don‘t amend published commits!

1
$ git commit --amend

Commit History

Show all commits, starting with newest

1
$ git log

Show changes over time for a specific file

1
$ git log -p <file>

Some changes over time for a specific committer

1
$ git log --author=<committer name>

Search (grep) commit message for the given string

1
$ git log --grep=<string>

Who changed what and when in

1
$ git blame <file>

Store changes temporarily

1
$ git stash

Remove and apply stashed changes

1
$ git stash pop

Remove file from all previous commit but keep it locally

1
$ git rm --cached <file>

Branches & Tags

List all existing branches

1
$ git branch [-av]

Switch HEAD branch

1
$ git checkout <branch>

Create a new branch based on your current HEAD

1
$ git branch <new-branch>

Create a new tracking branch based on a remote branch

1
$ git checkout --track <remote/branch>

Delete a local branch

1
$ git branch -d <branch>

Delete a remote branch

1
$ git push origin --delete <branch>

Rename a branch locally

1
$ git branch -m <old name> <new name>

Rename a branch on remote

1
2
$ git push <remote> :<old name>
$ git push <remote> <new name>

Mark the current commit with a tag

1
$ git tag <tag-name>

Update & Publish

List all currently configured remotes

1
$ git remote -v

Show information about a remote

1
$ git remote show <remote>

Add new remote repository, named

1
$ git remote add <remote> <url>

Delete a branch on the remote

1
$ git branch -dr <remote/branch>

Download all changes from , but don‘t integrate into HEAD

1
$ git fetch <remote>

Download changes and directly merge/integrate into HEAD

1
$ git pull <remote> <branch>

Publish local changes on a remote

1
$ git push <remote> <branch>

Track a remote repository

1
$ git remote add --track <remote-branch> <remote> <url>

Publish your tags

1
$ git push --tags

Merge & Rebase

Merge into your current HEAD

1
$ git merge <branch>

Rebase your current HEAD onto . Don‘t rebase published commits!

1
$ git rebase <branch>

Abort a rebase

1
$ git rebase --abort

Continue a rebase after resolving conflicts

1
$ git rebase --continue

Use your configured merge tool to solve conflicts

1
$ git mergetool

Use your editor to manually solve conflicts and ( after resolving) mark file as resolved

1
2
$ git add <resolved-file>
$ git rm <resolved-file>

Undo

Discard all local changes in your working directory

1
$ git reset --hard HEAD

Discard local changes in a specific file 

1
$ git checkout HEAD <file>

Revert a commit (by producing a new commit with contrary changes)

1
$ git revert <commit>

Reset your HEAD pointer to a previous commit and discard all changes since then

1
$ git reset --hard <commit>

Reset your HEAD pointer to a previous commit and preserve all changes as unstaged changes

1
$ git reset <commit>

Reset your HEAD pointer to a previous commit and preserve uncommitted local changes

1
$ git reset --keep <commit>

Submodules

Make changes, commit and checkout submodule files just go the submodules directory and use git as usual.

List all currently configured submodules

1
$ git submodules

or

1
$ git submodules status

Show information about a submodule

1
$ git remote show <remote>

Add a new submodule

  1. $ git submodule add -b <branch> --name <name> <repository-path-or-url>
  2. add the .gitmodule file and submodule folder to the superproject index.
  3. commit both files on the superproject

Git on the Server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
sudo apt-get install openssh-server
sudo userdel -r git 
sudo adduser git

cd /home/git
sudo mkdir .ssh
cd .ssh
touch authorized_keys
cat path/id_rsa.pub >> /home/git/.ssh/authorized_keys
sudo apt-get install git-core

git —bare init /home/git/test1.git
mkdir test1.git

cd test1.git
git —bare init

scp /home/git/.ssh/id_rsa.pub gitServer:/home/git
git clone git@192.168.1.100:/home/git/test1.git

sudo vim /etc/passwd
git❌1000:1000::/home/git:/bin/sh
git❌1000:1000::/home/git:/usr/bin/git-shell

sudo chown -R git:gitgroup path/to/repo.git/

Gitolite

1
2
3
sudo adduser git
su git
Copy ~/.ssh/id_dsa.pub (ssh-keygen) rename to <yourname>.pub scott.pub

https://github.com/sitaramc/gitolite

https://git-scm.com/book/zh/v1/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-Gitolite

http://williamherry.com/blog/2012/10/03/install-gitolite/

steps to install

First, prepare the ssh key:

  • login to “git” on the server
  • make sure ~/.ssh/authorized_keys is empty or non-existent
  • make sure your ssh public key from your workstation has been copied as $HOME/YourName.pub

Next, install gitolite by running these commands:

1
2
3
git clone git://github.com/sitaramc/gitolite
mkdir -p $HOME/bin
gitolite/install -to $HOME/bin

Finally, setup gitolite with yourself as the administrator:

1
gitolite setup -pk YourName.pub

If the last command doesn’t run perhaps “bin” is not in your “PATH”. You can either add it, or just run:

1
$HOME/bin/gitolite setup -pk YourName.pub

If you get any other errors please refer to the online documentation whose URL was given at the top of this file.

adding users and repos

Do NOT add new repos or users manually on the server. Gitolite users, repos, and access rules are maintained by making changes to a special repo called “gitolite-admin” and pushing those changes to the server.

To administer your gitolite installation, start by doing this on your workstation (if you have not already done so):

1
git clone git@host:gitolite-admin

NOTE: if you are asked for a password, something went wrong.. Go hit the link for the complete documentation earlier in this file.

Now if you “cd gitolite-admin”, you will see two subdirectories in it: “conf” and “keydir”.

To add new users alice, bob, and carol, obtain their public keys and add them to “keydir” as alice.pub, bob.pub, and carol.pub respectively.

To add a new repo “foo” and give different levels of access to these users, edit the file “conf/gitolite.conf” and add lines like this:

1
2
3
4
repo foo
   RW+     =  alice
   RW     =  bob
   R      =  carol

Once you have made these changes, do something like this:

1
2
3
4
git add conf
 git add keydir
 git commit -m "added foo, gave access to alice, bob, carol"
 git push

When the push completes, gitolite will add the new users to ~/.ssh/authorized_keys on the server, as well as create a new, empty, repo called “foo”.

help for your users

Once a user has sent you their public key and you have added them as specified above and given them access, you have to tell them what URL to access their repos at. This is usually “git clone git@host:reponame”; see man git-clone for other forms.

NOTE: again, if they are asked for a password, something is wrong.

If they need to know what repos they have access to, they just have to run “ssh git@host info”.

access rule examples

Gitolite’s access rules are very powerful. The simplest use was already shown above. Here is a slightly more detailed example:

1
2
3
4
5
6
7
repo foo
   RW+           =  alice
   \-  master       =  bob
   \-  refs/tags/v[0-9]  =  bob
   RW           =  bob
   RW refs/tags/v[0-9]  =  carol
   R            =  dave

Here’s what these example rules say:

  • alice can do anything to any branch or tag – create, push, delete, rewind/overwrite etc.
  • bob can create or fast-forward push any branch whose name does not start with “master” and create any tag whose name does not start with “v”+digit.
  • carol can create tags whose names start with “v”+digit.
  • dave can clone/fetch.

Please see the main documentation linked above for all the gory details, as well as more features and examples.

groups

Gitolite allows you to group users or repos for convenience. Here’s an example that creates two groups of users:

1
2
3
4
5
6
7
8
9
@staff   =  alice bob carol
@interns  =  ashok
 
repo secret
RW   =  @staff

repo foss
RW+   =  @staff
RW   =  @interns

Group lists accumulate. The following two lines have the same effect as the earlier definition of @staff above:

1
2
@staff   =  alice bob
@staff   =  carol

You can also use group names in other group names:

1
@all-devs  =  @staff @interns

Finally, @all is a special group name that is often convenient to use if you really mean “all repos” or “all users”.

commands

Users can run certain commands remotely, using ssh. Running

1
ssh git@host help

prints a list of available commands.

The most commonly used command is “info”. All commands respond to a single argument of “-h” with suitable information.

If you have shell on the server, you have a lot more commands available to you; try running “gitolite help”.

Reference

  1. GitHub Cheat Sheet | Visual Git Cheat Sheet
  2. Pro Git book