Git Quick Reference Manual

Source: Internet
Author: User
1. Create a new version Library

Create from a compressed package:

$ tar xzf project.tar.gz
$ cd project
$ git init #Initialized empty Git repository in .git/
$ git add .
$ git commit

Create from remote version Library:

$ git clone git://example.com/pub/project.git
$ cd project
2. Manage branches
$ git branch         # list all local branches in this repo
$ git checkout test  # switch working directory to branch "test"
$ git branch new     # create branch "new" starting at current HEAD
$ git branch -d new  # delete branch "new"

Create a branch that does not start with the current head. Use:

$ git branch new test    # branch named "test"
$ git branch new v2.6.15 # tag named v2.6.15
$ git branch new HEAD^   # commit before the most recent
$ git branch new HEAD^^  # commit before that
$ git branch new test~10 # ten commits before tip of branch "test"

Create and switch to the new branch:

$ git checkout -b new v2.6.15

Update and verify the branches cloned from the remote version Library:

$ git fetch             # update
$ git branch -r         # list
  origin/master
  origin/next
  ...
$ git checkout -b masterwork origin/master

Capture branches from different version libraries and give a new branch name in your version Library:

$ git fetch git://example.com/project.git theirbranch:mybranch
$ git fetch git://example.com/project.git v2.6.15:mybranch

Create a list of version libraries that you want to work with regularly:

$ git remote add example git://example.com/project.git
$ git remote                    # list remote repositories
example
origin
$ git remote show example       # get details
* remote example
  URL: git://example.com/project.git
  Tracked remote branches
    master
    next
    ...
$ git fetch example             # update branches from example
$ git branch -r                 # list all remote branches
3. Exploration History
$ gitk                      # visualize and browse history
$ git log                   # list all commits
$ git log src/              # ...modifying src/
$ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
$ git log master..test      # ...in branch test, not in branch master
$ git log test..master      # ...in branch master, but not in test
$ git log test...master     # ...in one branch, not in both
$ git log -S'foo()'         # ...where difference contain "foo()"
$ git log --since="2 weeks ago"
$ git log -p                # show patches as well
$ git show                  # most recent commit
$ git diff v2.6.15..v2.6.16 # diff between two tagged versions
$ git diff v2.6.15..HEAD    # diff with current head
$ git grep "foo()"          # search working directory for "foo()"
$ git grep v2.6.15 "foo()"  # search old tree for "foo()"
$ git show v2.6.15:a.txt    # look at old version of a.txt

Find the retreat point:

$ git bisect start
$ git bisect bad                # current version is bad
$ git bisect good v2.6.13-rc2   # last known good revision
Bisecting: 675 revisions left to test after this
                                # test here, then:
$ git bisect good               # if this revision is good, or
$ git bisect bad                # if this revision is bad.
                                # repeat until done.
4. production changes

Configure git

VI ~ /. Gitconfig

[User]
Name = Phoenix
Email = phoenixtoday@gmail.com
[Alias]
CO = checkout
Ci = commit-
St = Status
BR = Branch
Oneline = log -- pretty = oneline -- since = '2 Days ago'
Onelog = Log-p-1
[Color]
Status = auto
Branch = auto
Ui = auto

Select the files to be included in the next submission, and then make the delivery:

$ git add a.txt    # updated file
$ git add b.txt    # new file
$ git rm c.txt     # old file
$ git commit

Or prepare to submit and create delivery in one step:

$ git commit d.txt # use latest content only of d.txt
$ git commit -a    # use latest content of all tracked files
5. Merge
$ git merge test   # merge branch "test" into the current branch
$ git pull git://example.com/project.git master
                   # fetch and merge in remote branch
$ git pull . test  # equivalent to git merge test
6. Share your changes

Introduce or export patches:

$ git format-patch origin..HEAD # format a patch for each commit
                                # in HEAD but not in origin
$ git am mbox # import patches from the mailbox "mbox"

Capture the branches of a different git version library and merge them into the current Branch:

$ git pull git://example.com/project.git theirbranch

Before merging to the current branch, save the changes to the remote branch as the local branch:

$ git pull git://example.com/project.git theirbranch:mybranch

After the delivery of the local branch is created, use the delivery to update the remote branch.

$ git push ssh://example.com/project.git mybranch:theirbranch

When both local and remote branches are named "test:

$ git push ssh://example.com/project.git test

For remote version libraries that frequently communicate, there is a quick command version:

$ git remote add example ssh://example.com/project.git
$ git push example test
7. Maintenance of the version Library

Check for damage:

$ git fsck

Repackage and delete useless things:

$ git gc

 

8. Others

Ignore some files and directories

$ vi .gitignore

You can use wildcards. Do not add a slash at the end of the directory.

 

Discard all unsubmitted content

$ git reset --hard HEAD

 

When there is unsubmitted content, quickly switch to another branch

git stash save "work in progress for foo feature"
Switch to another branch, modify and submit...
Switch back to original Branch
git stash apply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.