git command Summary 1

Source: Internet
Author: User
Tags file diff git commands

Git Common Command Daquan

Git is a very powerful distributed version control system. It is not only suitable for managing the source code of large open source software, but also has many advantages in managing private documents and source code.

Git Common operations Commands:

1) Remote Warehouse related commands

Checkout warehouse: $ git clone git://github.com/jquery/jquery.git

View remote repositories: $ git remote-v

Add remote repository: $ git remote add [name] [url]

Delete Remote repository: $ git remote RM [name]

Modify remote repository: $ git remote set-url--push [name] [Newurl]

Pull remote repository: $ git fetch [remotename] [Localbranchname]

Push remote repository: $ git push [remotename] [Localbranchname]

* If you want to submit a local branch test to the remote repository as the master branch of the remote repository, or as another branch called Test, as follows:

$git Push Origin Test:master//submit local Test branch as remote Master Branch

$git Push Origin test:test//submit local test branch as remote Test branch

2) Branch (branch) operation related commands

View local branch: $ git Branch

View Remote branch: $ git branch-r

Create local branch: $ git branch [name]----Note that the new branch will not automatically switch to the current branch after it is created

Switch Branch: $ git checkout [name]

Create a new branch and switch immediately to the new branch: $ git checkout-b [name]

Delete Branch: $ git branch-d [name]-----D option can only delete branches that have already joined the merge and cannot be deleted for branches that do not have a merge. If you want to forcibly delete a branch, you can use the-D option

Merge branch: git merge [name]----Merge the branch with the name [name] with the current branch

Create a remote branch (local branch push to remote): $ Git push origin [name]

Delete Remote branch: git push origin:heads/[name] or $ gitpush origin: [Name]

* Create an empty branch: (Before executing the command remember to submit your current branch of the changes, otherwise it will be forced to delete the clean without regret)

$git symbolic-ref HEAD Refs/heads/[name]

$rm. Git/index

$git CLEAN-FDX

3) version (tag) operation related commands

View version: $ git tag

Create version: $ git tag [name]

Delete version: $ git tag-d [name]

View remote version: $ git tag-r

Create a remote version (local version push to remote): $ Git push origin [name]

Delete Remote version: $ git push origin:refs/tags/[name]

Merge the remote repository's tag to local: Git pull Origin--tags

Upload local tag to remote repository: $ Git push origin--tags

Create annotated tag:$ git tag-a [name]-M ' YourMessage '

4) Sub-module (submodule) Related Operations Command

Add sub-module: $ git submodule add [url] [path]

Example: $git submodule add Git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs

Initialize submodule: $ git submodule init----run only once when the warehouse is first checked out

Update submodule: git submodule update----need to run every time you update or switch branches

To delete a submodule: (4 steps away OH)

1) $ git RM--cached [path]

2) Edit the ". Gitmodules" file to delete the relevant configuration node of the Submodule

3) Edit the ". Git/config" file to delete the relevant configuration node of the Submodule

4) Manually delete the remaining sub-modules directory

5) Ignore some files, folders do not submit

Create a file with a name of ". Gitignore" under the repository root and write an unwanted folder name or file with one line per element, such as

Target

Bin

*.db

=====================

Common Git Commands

Git branch view all local branches
Git Status View current status
Git commit Commit
Git branch-a See all the branches
Git branch-r View all local branches
Git commit-am "Init" to submit and annotate
git remote add origin [email protected]:ndshow
Git push origin master pushes files to the server
Git remote show origin shows the resources in origin
Git push Origin Master:develop
Git push Origin Master:hb-dev associate a local library with a library on the server
git checkout--track origin/dev switch to remote Dev branch
git branch-d master Develop delete local library develop
git checkout-b Dev builds a new local branch dev
Git merge origin/dev Merge branch dev with current branch
git checkout dev switch to local dev branch
Git remote show to view the long-range library
git Add.
git RM file name (including path) removes the specified file from git
git clone git://github.com/schacon/grit.git pull the code down from the server
git config--list See all users
Git ls-files look at the already committed
git rm [file name] Delete a file
Git commit-a commits all changes to the current repos
git add [file name] Add a file to git index
Git commit-v you can see the difference in commit when you use the-v argument
Git commit-m "This is the message describing the commit" adds a commit message
Git commit-a-A is on behalf of add, adding all the change to git index and then commitgit commit-a-v General commit command git log to see your commit log (the log with the current state as the endpoint) git reflog (view All logs performed by the current warehouse) are aware of the differences, and can look at hash values, commit, checkout, reset, merge, and other Git commands. git diff view updates that have not yet been staged
git rm a.a Remove files (removed from staging area and workspaces)
git rm--cached a.a Remove Files (removed from staging area only)
Git commit-m "Remove" Remove files (removed from git)
Git rm-f a.a forcibly remove modified files (removed from staging area and workspaces)
git diff--cached or git diff--staged view the updates that have not yet been submitted
Git stash push to push files to a temporary space
Git stash pop to pop files from scratch space
---------------------------------------------------------
git remote add origin [email protected]:username/hello-world.git
Git push Origin master submits a local project to the server
-----------------------------------------------------------
Git pull local and server-side synchronization
-----------------------------------------------------------------
git push (remote warehouse name) (branch name) pushes the local branch to the server.
Git push Origin Serverfix:awesomebranch
------------------------------------------------------------------
Git fetch is equivalent to getting the latest version from remote to local, not automatically merge
Git commit-a-M "Log_message" (-A is to commit all changes,-M is to add log information) local modifications are synchronized to the server side:
Git branch branch_0.1 master creates branch_0.1 branch from Master Branch Master
git branch-m branch_0.1 branch_1.0 rename branch_0.1 to branch_1.0
git checkout branch_1.0/master switch to Branch_1.0/master branch
Du-hs

-----------------------------------------------------------
mkdir WebApp
CD WEBAPP
Git init
Touch README
git add README
Git commit-m ' first commit '
git remote add origin [email protected]:d Aixu/webapp.gitgit push-u Origin master-----------2-------------git common commands

View, add, submit, delete, retrieve, reset modified files

git help <command> # Show Command's help

Git show # shows the content of a commit git show $id

Git co--<file> # Discard workspace Modifications

Git Co. # Discard Workspace Modifications

git add <file> # Commit working file modifications to local staging area

git Add. # submit all the modified work files to staging area

git rm <file> # remove files from repository

git rm <file>--cached # Remove files from repository without deleting files

git reset <file> # recover from Staging area to working file

git reset--. # Recover from Staging area to working file

git Reset--hard # Restores the state of the last commit, discarding all of the changes since the previous commit

Git ci <file> git ci. Git ci-a # git add, git rm and git ci are all combined to make git ci-am "some comments"

Git ci--amend # Modify the last commit record

git revert < $id > # Restores the state of a commit, and the restore action itself also creates a commit object

git revert HEAD # Restores the state of the last commit

View File diff

git diff <file> # Compare current file and staging area file diff git diff

git diff <id1><id2> # Compare the differences between two commits

git diff <branch1>. <branch2> # Compare between two branches

git diff--staged # compare staging area and repository differences

git diff--cached # compare staging area and repository differences

git diff--stat # just compare statistics

View Commit Record

git log git log <file> # View the file for each commit record

git log-p <file> # view diff for each detail change

Git log-p-2 # See a diff of the last two detailed edits

git log--stat #查看提交统计信息

Tig

You can use TIG instead of diff and Log on your Mac.brew install tig

Git Local Branch Management

View, toggle, create, and delete branches

Git br-r # View Remote Branch

git br <new_branch> # Create a new branch

Git br-v # View the last commit information for each branch

git br--merged # View branches that have been merged into the current branch

git br--no-merged # View branches that have not been merged into the current branch

Git Co <branch> # switch to a branch

Git co-b <new_branch> # Create a new branch and switch to the past

git co-b <new_branch> <branch> # Create a new new_branch based on branch

Git Co $id # Checkout A history commit record, but without branching information, switching to another branch will automatically delete

Git co $id-B <new_branch> # Checkout A history commit record and create a branch

git br-d <branch> # Delete a branch

git br-d <branch> # Force a branch to be removed (the branch that is not merged is removed when it needs to be forced)

Branch Merging and Rebase

git merge <branch> # Merge branch branches into the current branch

git merge origin/master--no-ff # do not fast-foward merge so you can generate a merge commit

git rebase master <branch> # rebase Master to branch, equivalent to: Git co <branch> && git rebase master && ; Git co master && git merge <branch>

git patch management (for easy development of synchronization on multiple machines)

git diff >: /sync.patch # Generate Patches

git apply. /sync.patch # Patching

git apply--check. /sync.patch #测试补丁能否成功

Git staging management

Git Stash # Staging

git Stash List # column all stash

git stash apply # recover staged content

git stash Drop # Delete Staging area

GIT Remote branch Management

Git pull # Crawl all branches of remote repository update and merge to local

Git pull--no-ff # Crawl all branches of remote repository update and merge to local, do not fast-forward merge

Git fetch origin # crawl Remote repository update

git merge Origin/master # merges the remote main branch into the local current branch

Git co--track origin/branch # Track a remote branch to create the appropriate local branch

git co-b <local_branch> origin/<remote_branch> # Create local branch based on remote branch, same as above

Git push # push all branches

Git push Origin Master # pushes the local landlord branch to the remote main branch

Git push-u Origin Master # pushes the local landlord branch to the remote (if no remote main branch is created to initialize the remote repository)

Git push Origin <local_branch> # Create a remote branch, origin is the remote repository name

Git push Origin <local_branch>:<remote_branch> # Create a remote branch

Git push Origin:<remote_branch> #先删除本地分支 (git br-d <branch>), then push to delete remote branch

GIT Remote repository Management

GitHub

Git remote-v # View remote server address and warehouse name

git remote Show Origin # View the Server warehouse status

git remote add origin [email protected] Github:robbin/robbin_site.git # Add repository Address

git remote set-url origin [email protected] Github.com:robbin/robbin_site.git # Set the remote warehouse address (used to modify the remote warehouse address) git remote RM <rep ository> # Deleting a remote repository

Create a remote Warehouse

git clone--bare robbin_site robbin_site.git # Create a version-only warehouse with a versioned project

Scp-r my_project.git [email protected] git.csdn.net:~ # Upload the repository to the server

mkdir robbin_site.git && cd robbin_site.git && git--bare init # Create a pure warehouse on the server

git remote add origin [email protected] Github.com:robbin/robbin_site.git # Set up the repository address

Git push-u Origin Master # client First Commit

Git push-u origin Develop # First submits the local develop branch to the remote develop branch, and the track

git remote Set-head Origin Master # Set the remote repository head to the master branch

You can also command settings to track remote libraries and local libraries

Git branch--set-upstream Master origin/master

GIT branch--set-upstream develop origin/develop ********************************** git branch? Not very familiar with git, there's a problem to figure out.
If the remote repository has dev and master two branches, master as a stable branch, can be used to directly release the product, the daily development is pushed to the Dev branch, I am local is not to create a local branch from the Dev branch, and then the push action in this branch is the default push to On the remote dev branch? Just write the order, thank you. Add comments to sort by poll sorted by Time 5 answers agree 22 objection, will not show your name Lu Yiwang,reading Thousands of books is not as good as foldingLee Maverig, users, LAISX and other people agree 122 points to answer, suggestions are all read.
The 1th answer is your question, and the 2nd is a more comprehensive understanding and other recommendations.

I. Remote repository with master and Dev branches
1. Cloning code
git clone https://github.com/master-dev.git  # 这个git路径是无效的,示例而已
2. View all Branches
git branch --all  # 默认有了dev和master分支,所以会看到如下三个分支# master[本地主分支] origin/master[远程主分支] origin/dev[远程开发分支]# 新克隆下来的代码默认master和origin/master是关联的,也就是他们的代码保持同步# 但是origin/dev分支在本地没有任何的关联,所以我们无法在那里开发
3. Create a branch of the locally associated Origin/dev
git checkout dev origin/dev  # 创建本地分支dev,并且和远程origin/dev分支关联,本地dev分支的初始代码和远程的dev分支代码一样
4. Switch to Dev Branch for development
git checkout dev  # 这个是切换到dev分支,然后就是常规的开发
5. For a better understanding, it is advisable to continue to look at the following.

Second, assume that the remote warehouse only Mater branch
1. Cloning code
git clone https://github.com/master-dev.git  # 这个git路径是无效的,示例而已
2. View all Branches
git branch --all  # 默认只有master分支,所以会看到如下两个分支# master[本地主分支] origin/master[远程主分支]# 新克隆下来的代码默认master和origin/master是关联的,也就是他们的代码保持同步
3. Create a new local dev branch
git branch dev  # 创建本地分支git branch  # 查看分支# 这是会看到master和dev,而且master上会有一个星号# 这个时候dev是一个本地分支,远程仓库不知道它的存在# 本地分支可以不同步到远程仓库,我们可以在dev开发,然后merge到master,使用master同步代码,当然也可以同步
4. Release the Dev Branch
Publishing Dev branch refers to the code that synchronizes the dev branch to the remote server
git push origin dev:dev  # 这样远程仓库也有一个dev分支了
5. Developing code in Dev branch
git checkout dev  # 切换到dev分支进行开发# 开发代码之后,我们有两个选择# 第一个:如果功能开发完成了,可以合并主分支git checkout master  # 切换到主分支git merge dev  # 把dev分支的更改和master合并git push  # 提交主分支代码远程git checkout dev  # 切换到dev远程分支git push  # 提交dev分支到远程# 第二个:如果功能没有完成,可以直接推送git push  # 提交到dev远程分支# 注意:在分支切换之前最好先commit全部的改变,除非你真的知道自己在做什么
6. Delete a branch
git push origin :dev  # 删除远程dev分支,危险命令哦# 下面两条是删除本地分支git checkout master  # 切换到master分支git branch -d dev  # 删除本地dev分支
7. Recommended Books Progit.pdf
Book format and Language: Chinese, English, PDF, EPub
: Http://git-scm.com/book8. There is no understanding of the place can ask $_^------------------------------------------------------------------------------------------------ ----Book to school: 1. View the branch in the form of a chart: git log--graph2, how do I get back to the historical version? (need to know the hash value)git reset--hard hash value3. Create and switch to the new branch git checkout-b branch name 4, merge to Main branch: [Note: first switch to Main branch]git merge-no-ff Small branch name (note: This operation is done on the main branch)5. If the last submission/comment is not written, you can modify it.when Git commit-ament executes, it goes into the editor state and displays the last written comment, which can be modified6, add all the changes of the current workspace to staging area: Git Add-u7, get to a branch of the remote assume that the remote repository has a dev branch, local want to get to this branch: git branch-b dev origin/dev Note: Origin/dev is the source of access Name of the branch 8, the difference between the work area and the current commit git diff head Note: Head points to the latest commit pointer in the current branch

git command Summary 1

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.