Common GIT Commands summary

Source: Internet
Author: User
Tags git commands

Common GIT Commands summary

With R brother came to the new company (a transition from hardware to the Internet company), the new company's previous code is basically using SVN version control, and even some code is not version control, so called Hg did a git share, ready to put all the company's code git version control. Although I usually use git every day, but the total feeling of knowledge is scattered, so summarized some common git commands.

Common configuration
--system #系统级别--global #用户全局--local #单独一个项目git config --global user.name "xxxx" #用户名git config --global user.email "[email protected]" #邮箱git config --global core.editor vim #编辑器git config --global alias.st status #按这种方法,配置别名git config -l #列举所有配置
Some of the 3 state actions in Git
#将工作区的修改提交到暂存区git Add <file>git Add. #------------------------------------------#将暂存区的内容提交到版本库git Commit <file>git commit. Git commit-a #包括git add/ Git rm/git commint These three operations, all generally in the operating workspace, when the file is deleted directly, instead of using Git rm, the last commit is can use this, as follows #git Commit-am "commit information" git commit- Amend #修改最后一次提交的信息 #------------------------------------------# Discard Workspace Modifications (use current staging area content state to overwrite workspaces to discard workspace modifications) git  Checkout <file> git checkout. #------------------------------------------#改变暂存区的修改 (in fact, reset the head, will specify the repository's content state to overwrite staging area, so as to achieve staging area change) git reset <file  > #从暂存区恢复到工作区 (does not specify the version ID, the default is the last committed version ID) git reset. #从暂存区恢复到工作区git Reset $id # revert to the specified commit version, the $id after the version submission is restored to the workspace git reset--hard $id #恢复到指定的提交版本, the $id after the release commit all will be discarded, will not appear in the workspace # Note: If you accidentally use the wrong head reset, you will find that head points to the version ID of the reset, after which the version submission is gone and the Git log cannot be found, so how do I recover it? Use the following two commands for Git reflog show Master | Head #会显示所有的版本记录git Reset--hard $id #重新重置, as for--hard, depending on when you put the changed content in the workspace or simply discard the option #----------------------------------- -------#恢复某次提交 (in fact, a rollback operation that does not affect the other commits, the resulting effect creates a new version of the commit to roll back deletes the specified commit, including the resulting diff file does not appear in the workspace, but is directly discarded) git revert < $id >git revert head# Here's a good explanation of the difference between revert and reset: Git reset is moving the HEAD back a bit, And git revert is head continues to move forward, but the content of the new commit and the content to revert is just the opposite, can offset the content to be revert. #------------------------------------------#删除文件的几种方法 (seems to have changed after Git2.0) #第一种直接在工作区删除rm your_file #直接在工作区删除文件git add-u . #将有改动的都提交到暂存区 (including modified, deleted, etc.), seemingly git2.0 without the-u parameter can also git commint-m "message" #提交版本库 # The second method removes RM your_file directly from the workspace # Delete files directly in the workspace git commit-am "message" #这个在前面提过, can directly submit the repository,-a will include Git add/git rm/git commint Three Operations # The third method uses Git rmgit rm <fi Le> #不仅在工作区将文件删除, and submit the deletion to staging area git commint-m "message" #提交版本库 # Other additions to git rm--cached <file> #从暂存区中除去该文件, Git will no longer track changes to the file, but it is still in the workspace and is often used when needed. Gitignore.
File directly compare differences diff
git diffgit diff <file> #比较工作区与暂存区文件的差异git diff --cached   # 比较暂存区和版本库差异git diff <$id1> <$id2>   # 比较两次提交之间的差异git diff <branch1>..<branch2> # 在两个分支之间比较
Branch
git branch -r #查看远程分支git branch new_branch_name #新建一个分支git branch --merged #查看已经被合并到当前分支的分支git branch --no-merged #查看未被合并到当前分支的分支git checkout branch_name #切换分支git checkout -b branch_name #创建分支并切换git branch -d branch_name #删除分支git branch -D branch_name #强制删除分支git push origin :branch-name #删除远程分支(先在本地删除该分支),原理是把一个空分支push到server上,相当于删除该分支。#从远程clone一个项目,虽然远程上该项目是有分支的,但clone下来后发现只有master分支,解决:git checkout -b not_master_branch  origin/not_master_branch #本地创建一个分支,指向对应的远程分支git pull origin not_master_branch #将远程的not_master_branch分支pull下来git push origin not_master_branch #将修改后的not_master_branch分支push到远程的not_master_branch
Remote
git remote -v                    # 查看远程服务器地址和仓库名称git remote show origin           # 查看远程服务器仓库状态git remote add origin [email protected]:robbin/robbin_site.git         # 添加远程仓库地址git remote set-url origin [email protected]:robbin/robbin #修改远程地址git remote rm #删除远程创库地址
Pull content from remote, submit content to remote
git pull #=git fetch + git mergegit fetch #拉取git merge #合并git push                         # push所有分支git push origin master           # 将本地主分支推到远程主分支git push -u origin master        # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)git push origin <local_branch>   # 创建远程分支, origin是远程仓库名git push origin <local_branch>:<remote_branch>  # 创建远程分支git push origin :<remote_branch>  #先删除本地分支(git br -d <branch>),然后再push删除远程分支
Staging Management
git stash #将工作区做的修改暂存到一个git栈中git stash list #查看栈中所有暂存git stash apply <暂存编号> #回复对应编号暂存到工作区,如果不指定编号为栈顶的,注意:这些暂存还在栈中git stash pop #将栈顶的暂存,恢复到工作区,并从栈中弹出git stash clear #清空暂存栈
Create a remote Library
git clone --bare git_url_path #clone的时候,将其创建成远程创库git --bare init #初始化项目的时候,创建成远程创库

Section reference Robbin fan great god's blog "Git Common Command Memo"

Reference http://www.jianshu.com/p/0f2ffa404ac1#

Common GIT Commands summary

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.