Common git usage

Source: Internet
Author: User

Address: http://blog.csdn.net/ariesjzj/article/details/7747876

Like SVN and CVS, git is a source code management system. Different from the latter two, it not only supports centralized management, but also works in a distributed manner, that is, all operations are local and fast, local submission does not affect the shared code repository. Git has many features. This article lists some common usage.

Configure and create a code repository
Set the editor for submission (nano by default ):
$ Export git_editor = Vim
Or
$ Git config -- global core. Editor Vim

Set file comparison tool:
$ Git config -- Global merge. Tool vimdiff

$ Git config -- Global diff. Tool vimdiff

$ Git config -- Global difftool. Prompt NO

Ignore file permissions:

$ Git config core. filemode false

Configure personal information, that is, the author information displayed during submission, such:
$ Git config user. Name "jzj"
$ Git config user. Email "jzj @ domain"

These git config commands are actually written ~ /. In the configuration file gitconfig, writing this file directly has the same effect. For example, to highlight the output of the GIT command, add

[Color]

Ui = auto

Create a new git Repository:
$ Git init

Query Information (Inquiry)
The commands in this part are similar to those in SVN.

View configuration information:
$ Git config -- list

View the repository status information:
$ Git status

View the differences between the two versions
$ Git diff
As git introduces the index concept, compared with SVN, git diff can be divided into two types: -- cached and no:
$ Git diff # displays local changes but no stage changes
$ Git diff -- cached # displays the local changes that have been made in the stage, that is, the changes that will be submitted if git commit is executed.

$ Git diff master .. test # differences between the two branchs
$ Git diff -- stat # displays statistics

Historical Information
$ Git log
$ Git log-P # display patch
$ Git log -- stat # number of rows
$ Git log -- TOPO-order -- Graph # use a simple graph to display the topological relationship between commit
$ Git log-N # Only list the first n
$ Git show-branch -- more = N # previous n Submission history information

View changes
$ Git show <Sha> # view the changes to the specified commit

In addition, commands such as git LS-files and git grep are available for file operations in the working directory.

Submit)
Unlike SVN, git's checkin consists of two steps: stage and commit. For example:
$ Git Add a. C # stage change and change the index (index is a temporary dynamic file that describes the entire project directory and records the changes made ). Changes made to the stage are displayed in git diff -- cached.
$ Git commit-M "Comments" # submit changes

You can combine the preceding two steps with the-a parameter:
$ Git commit-
However, it is limited to tracked files. For example, if you create a new file without git add, and it is still in the untracked state, adding-A is useless.

Patch)
Patch generation
$ Git format-patch-N master # generate the last n commit Patches
$ Git format-patch master ~ 4. Master ~ 2 # generate master ~ 4. Master ~ Patch with two differences
$ Git format-patch-S <Sha> # generate the patch for the specified commit and add the signature
Application Patches
Git am uses Git apply, which patches to generate commit information. If an error occurs
Previous rebase directory ../. Git/rebase-apply still exists but mbox given
Available
$ Git am -- abort

The preceding method is used for changes that have been committed. If you use a locally modified patch generated by git diff, you can use the following method:
Generate a locally modified patch:
$ Git diff | tee diff. Patch
Application patch:
$ Git apply -- ignore-space-change -- ignore-whitespace diff. Patch
Or

$ Patc-P1 <diff. Patch
Of course, this is more like the habit of SVN. In git, it is local commit, and the submission cost is very low. Therefore, you can submit the task before generating the patch.

Patch is used between branch:
$ Git cherry-pick

Undo)
There are three commands related to undo: git checkout, git revert, and git reset.

For example, cancel the local stage changes (that is, the changes are not recorded in the index using commands such as git add ):
$ Git checkout. # index unchanged, working directory changed

Another example:
$ Git checkout head ^ # returns to the previous version of COMMIT
$ Git checkout <Sha> # returns to the specified commit version

Git reset not only acts on the working directory, but also on the index file. Git reset has three methods: soft, hard, and mixed.
$ Git reset -- soft <Sha> # does not affect the index or the working directory. That is to say, you can still see the undo changes using git diff -- cached.
$ Git reset -- mixed <Sha> # index changed, but the working directory remained unchanged.
$ Git reset -- hard <Sha>, # restore the index and working directory to the specified version. Equivalent to SVN revert-R *
After undo, git log cannot see the Undo commit history, but you can see it with the following command:
$ Git reflog

The difference between git checkout and git reset -- hard is:
Git checkout. # clear local changes, but not the index. For example, git add has not been used for commit.
Git reset -- hard head # clear local changes, including index. So the GIT added files are cleared.

Git revert performs a commit that is the opposite of the specified version. For example:
$ Git revert <Sha> # the opposite of <Sha> specified commit once and <Sha> specified commit
$ Git revert-n <Sha> # Same as the previous command, but not submitted
Git revert can eliminate the intermediate commit function, and git revert does not change the existing history. This is useful when the project is based on an existing git repository.

If you only want to modify the commit information for commit, you can use:
$ Git commit -- Amend

Branch)
New
$ Git branch new_branch
Or
$ Git checkout-B new_branch
View
$ Git show-branch
Extract
$ Git checkout new_branch
Delete
$ Git branch-D new_branch
Merge other branches
$ Git merge branchname

Miscellaneous:
Some files do not need to be tracked by git. You can set to ignore these files in. gitignore.

Specify a specific file with --, for example:
$ Git checkout -- DIR/Main. c

Git has several built-in symbol index pointers:
Head: always points to the latest commit of the current branch.
Orig_head: After git reset or git merge, the original head is stored here
Fetch_head: Head index pointer of all branches after git fetch
Merge_head: Head index pointer of the other branch when git merge is used

Relative index: Head ^ head ~ 2 Head @ {2}, etc.

If you have done half of the work, but there is a small but urgent bug to fix, you can use git stash. It pushes the current working context to the stack like a stack, and pops up after the bug fix is complete.
$ Git stash "current work"
$ Git commit-a-m "Trivial bug fix"
$ Git stash apply
 
Gitk: Graphical git diff

When encountering regression, you can use git bisect and git blame. The former is used to find the version from which the problem occurs, and the latter lists the modifier and time of each line of code.

Distributed Management
Copy code repository
$ Git clone/tmp/repo # create a code repository copy of/tmp/repo in the current directory

Copy remote code repository (for example, user name zjin, host name zjin-machine)

$ Git clone zjin @ zjin-machine:/home/zjin/Repo

Submit to remote code repository
$ Git push/tmp/repo master # submit the changes in the local master branch to/tmp/Repo
Synchronize data from a remote code repository
$ Git pull/tmp/repo master # synchronize changes in/tmp/repo to the local master Branch
This command is equivalent to git fetch and git merge

 

Related information:
Git tutorial http://git.or.cz/course/svn.html for SVN users
Repo and git http://source.android.com/source/version-control.html
Git-tips http://phoenixtoday.blogbus.com/logs/35149540.html
Version Control with GIT
Git community book

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.