Git summary of commonly used commands and other related operations

- File directory operations command

1 mkdir * * Create an empty directory refers to the directory name 
2 pwd displays the current directory path. 
3 cat * * View file content 
4 git rm * delete files **

--git initialization

1 git init becomes the current directory git repository, generate hidden .git file. 
2 git remote add origin url to content pushed to the local repository GitHub repository. 
3 git clone git@url/test.git from a remote library clones 
4 git add * x add files to the staging area to go. 
5 git commit "*" is submitted after the file -m -m comment.

  --git clone branch

1 git clone xxx.git most straightforward command 
2 git clone xxx.git "specified directory" clone to a specified directory 
3 git to create a new branch instead of the default Origin HEAD (master) when clone clone -b branchname xxx.git

 

--clone remote branch

  git clone command only creates the default master branch , if you want a remote branch clone designated (eg: dev), it can be as follows:

  1. Check all branches (including hidden) git branch -a display all branches 

    

1 * master
2   remotes/origin/HEAD -> origin/master
3   remotes/origin/dev
4   remotes/origin/master

  2.  In the same name as the new local ( "dev") branch, and switch to the branch

1 git checkout -t origin / dev This command is equivalent to: 
2 -b Git Checkout Origin dev / dev

  - View command

1 git status to view the status of the warehouse 
2 git diff * View X Files modify the content of those    
3 git log to view the history 
git reflog view the history of the version number id 4 (record your every command, whether or not submitted) 
5 git log - pretty = oneline if the amount of information can be much better display list  

  - version rollback

1 git reset -hard HEAD ^ fall back on a version 
2 git reset --hard HEAD ~ If you want to roll back to the first of several third version, use the RESET -hard the HEAD ~ 3 git 
3 git back to the RESET --hard 057d retreated on a specific version number

  

- undo changes

 
1 git checkout file-name restore a modified file (undo changes uncommitted): 
2 git revert to modify the HEAD Revert last submitted: 
3 git Revert to restore the commit-specific version of the above mentioned id modification

  - Branch Management

View all 1 git branch local branch 
 2 git branch -a view of all remote branch 
 3 git branch name to create a branch 
 4 git branch -d dev delete dev branch 
 5 git push origin --delete dev dev delete remote branch 
 6 git branch - m dev develop rename branch 

 7 git checkout -b dev dev branches create and switch to the dev branch 
 8 git merge dev dev combined branch on behalf of the current branch 
 9 git push origin zyf-dev Xinjiang the current branch pushed zyf-dev remote library (no remote repository to the new branch will be the establishment of the branch) 
 
10 git Checkout - * XX to withdraw all files modified in the workspace. 
11 git checkout master switch back to the master branch 
12 git push --set-upstream origin dev commit changes and create a remote branch dev

  --tag related operations

1 git tag lists all of the tag 
2 git tag name to play light tag name 
3 git tag -d delete the local name tag 
4 git tag name the Push Origin --delete delete remote tag 
5 Show git tag name to view information 
6 git push origin name will be submitted to the remote tag

  

- hidden files

1 git stash the current work hidden so after continue to work after recovery site 
2 git stash list to view all the hidden files list 
3 git stash apply recover hidden files, but the content is not deleted 
4 git stash drop deleting files 
5 git stash Meanwhile pop restore files also delete files

 

- View information about the remote repository (git remote usage) 

1 git remote viewing remote repository information 
2 git remote -v view the remote database details 
3 git remote add name url add a remote repository 
4 git remote rename oldname newname rename warehouse 
5 git remote rm delete warehouse

 

- the remote to the local branch pulled

Method 1 a: git checkout -b local branch name x origin / remote branch name x 
2 two ways: git fetch origin remote branch name x: x local branch name

 

--git pull operation

Copy the code
Copy the code
1 git pull action command is to retrieve a branch update the remote host, and then combined with the specified local branch, the basic format is as follows. 
 2 $ git pull <remote host name> <remote branch name>: <local branch name> 
 . 3 
 . 4 retrieved the next branch origin host, combined with local branch master, need to be written as follows 
 . 5 $ Git next pull origin: Master 
 . 6 
 If the remote branch 7 is combined with the current branch, the portion of the colon may be omitted. 
 Git Next pull Origin $. 8 
 . 9 
10 represents the above command, retrieving origin / next branch, and then merged with the current branch. In essence, this is equivalent to do first git fetch, do git merge. 
Origin git FETCH $ 11 
12 $ git Merge Origin / the Next 
13 
14 In some cases, Git automatically between the local branch and a remote branch, to establish a relationship tracking (tracking). For example, when git clone, the local branch of the same name in all branches of default remote host, a tracking relationship, that is, the local master branch automatic "track" origin / master branch. 
15 Git also allows the track to establish relationships manually. 
Git Branch upstream the --set-16 master origin / next 
. 17
18 The above command specifies the master branch to track origin / next branch. If the current relationship between the presence of trace branch and remote branch, git pull will be omitted remote branch name. 
19 $ git pull origin
Copy the code
Copy the code

 

--git setting case sensitive

Git on Windows by default is not case-sensitive, so it is possible to write multi-platform problems. Git Win provided on the following command is case-sensitive

1 git config core.ignorecase false  

 

--git set to ignore a file or folder permissions to modify

1 git config core.filemode false

 

- Create a tracking branch

git push without any parameters, the default only push the current branch, which is called simple way. In addition, there is a matching way, will have to push all of the local branch of the corresponding remote branch. Prior to Git version 2.0, defaults to matching method, is now default to simple. If you want to change this setting, git config command can be used.

Git config --global push.default matching $ 
# or 
$ git config --global push.default simple (preferably used in this way)
  $ Git branch --track master origin / master // add '--track' parameter when using git branch command to manually create a tracking branch

 

- switching git command prompt Chinese to English

Copy the code
// ubuntu installed git do not know how the entire Chinese prompt appeared, not very good, decisive switch to the Chinese, and switching method as follows: 
// 1: Write echo "alias git = 'LANG = en_GB git'" >> ~ / .bashrc // 2: entry into force source ~ / .bashrc
Copy the code

 

--git not added to delete versions of a file or folder

git checkout can only roll back the version modified or deleted, for an added file is not useful, that is to say, the new file or folder is: Untracked files, you want to remove or clean up these files, you need to use git clean command:

Copy the code
Copy the code
Files 1 // remove untracked 
 2 -f Git Clean 
 . 3   
 even untracked directory @ 4 together are also deleted 
 . 5 Git Clean -fd 
 . 6   
 . 7 // even gitignore untrack the file / directory is also deleted together (typically used to delete the compiled out of a class .o file) 
 8 git Clean -xfd 
 9   
10 // clean up before using git clean, suggested adding -n to take a look at which files will be deleted to prevent important files from accidental deletion 
11 git -nxfd Clean 
12 is Git Clean -nf 
13 is Git Clean -NFD
Copy the code
Copy the code


Third, often the error:
1: There IS NO at The Tracking Information for Current Branch ...
    then the local branch and a remote branch relationship did not create a link, use the command:??
    Git Branch --set-upstream Branch-name origin / branch-name.

2:! [Rejected] dev -
> dev (non-fast-forward) ... Updates were rejected because the tip of your current branch     push failed because the latest submission remote code and you try to submit the conflict to push the solution it is also very simple, first with git pull to the latest submission of catch down from the origin / dev, then, in the local merger, conflict resolution, and push them up
    
3: cONFLICT (content):.
.. merge conflict in     this time git pull success, but associated with the conflict, you need to manually resolve, as a solution to resolve the conflict and branch management. After resolving to submit, then the Push:
    
4: by You are not Rate this page Currently ON A Branch, SO the I CAN not use the any
   solution: git Checkout Master

5: Your Branch IS behind 'Origin / Master' by 1 the commit, and CAN BE the FAST-Forwarded .
   above shows mean, there is still no response to an update to local, others may be submitted to the server a little something. 
   You can use git pull command to get these updates to be local.

 

6: When a git push may see the following message: 

warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
  git config --global push.default simpl

  Solution: 'matching' parameter is the default behavior Git 1.x, if you run git push but do not specify a branch, it will push all your local branch to branch remote repository corresponding match. The Git 2.x default is simple, execute git push means when the branch is not specified, only the current branch is the code you use to push git pull obtained. 
When prompted, modify git push:

1 git config --global push.default matching

Execute git push again on the line.

 

7:You asked to pull from the remote 'origin', but did not specify:a branch. Because this is not the default configured remotefor your current branch, you must specify a branch on the command line.

Solution: Find:.git/config  修改如下

1 [branch "master"]
2   remote = origin
3   merge = refs/heads/master

 

8:ERROR: Permission to user1/test.git denied to user2   fatal: The remote end hung up unexpectedly

 Accounts conflict, below for explanation

 

9: Adding the ssh does not work?

   ssh must add permissions to add the root user, other privileges does not work, remember!


Fourth, the rule of thumb:
1: the difference between git revert and git reset the 
start both feel the same, but found that much different after the client and command-line window operations, look at an example: I will submit three 1,2,3 id numbers are (15d5f70, a167eb1,8fbfe3f) as shown below

Then perform a version rollback git reset -hard a167 shown:

说明执行git reset –hard ** 版本回退是撤销某次提交,但是此次之后的修改都会被退回到暂存区,因为上面显示有提交(by 1 commit)
同理执行 git revert –hard a167
git revert是用一次新的commit来回滚之前的commit
  在回滚操作上看,效果一样。但是在日后继续merge以前的老版本时有区别。revert是用一次逆向的commit“中和”之前的提交,因此日后合并老的branch时,导致这部分改变不会再次出现,但是reset是之间把某些commit在某个branch上删除,因而和老的branch再次merge时,这些被回滚的commit应该还会被引入。 
得到结论:git reset 是把HEAD向后移动了一下,而git revert是HEAD继续前进,
只是新的commit的内容和要revert的内容正好相反,能够抵消要被revert的内容。

  这样在版本回退的时候就比较的好,比如本地dev分支,远程master分支,某一次的上线发现线上有重大的bug,然后你reset本地代码到前一个版本,在你git push 的时候git会报一个错误:“说你的本地分支版本低于远程master分支的版本,让你先去pull 下来代码,再提交”,这就不行了,你pull下来的代码还是最新版有bug的代码,不进行pull代码,master分支又不让你提交。解决这个问题还得使用 revert 来进行 “回退”操作,为什么加引号呢,是因为这种回退是向前提交一次中和了上次的修改,这就比较好了,这样你revert之后相当于指针向前移动一次,本地版本dev高于远程master版本,这时你就可以git push 本地代码到远程了。也就是说,git reset 对未提交到(git push)远程的修改做回滚比较好,如果要回滚本地,同时想远程也回滚,就要用到revert

五、常用修改:
1:避免每次提交都输入用户名和密码
    原因是使用了https方式 puh 在termail里边 输入  git remote -v 可以看到形如一下的返回结果
    origin https://github.com/git/librarysystem.git(fetch)
    origin https://github.com/git/librarysystem.git(push)
    下面把它换成ssh方式的。

1. git remote rm origin
2. git remote add origin [email protected]:yu/demo.git
3. git push origin 

2:添加忽略文件 .gitignore 
    .gitignore配置文件用于配置不需要加入版本管理的文件。
    配置语法: "/"  开头表示目录;  "*"  通配多个字符;  "?"  通配单个字符   "[]" 包含单个字符的匹配列表;  "!"  表示不忽略(跟踪)匹配到的文件或目录;
    说明:git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;
    示例:(1) foder/*     忽略目录foder下的全部内容,不管是根目录下的 /foder/ 目录,还是某个子目录 /list/foder/ 目录,都会被忽略。
         (2) /foder/*   说明:忽略根目录下的 /foder/ 目录的全部内容;
         (3) /*!.gitignore
                          !/fw/bin/
                          !/fw/sf/
                          说明:忽略全部内容,但是不忽略 .gitignore文件,根目录下的 /fw/bin/ 和 /fw/sf/ 
    注意问题: .gitignore文件只对还没有加入版本管理的文件起作用,如果之前已经用git把要忽略的文件纳入了版本库,就不起作用了。

3:如何在同一台电脑上使用两个git账户

  问题描述:作为程序员有时候是有多个的项目,而且每个项目git的用户名,邮箱等,都不一样。就我来说,我在github上有一个账号,自己开发一些东西,同时我参与的也有项目需要连接别人的github账号,在我电脑上只有一个git客户端,本地一个身份用户,开始的时候我自己开发是用ssh方式,参与的项目的是用https连接,但是https每次都需要输入密码,非常不方便,所以想转成ssh连接,利用命令:
>$ git remote rm origin
>$ git remote add origin [email protected]:yu/demo.git 
>$ git push origin       
转换成功,不幸的是push的时候出错了:ERROR: Permission to user1 denied to
这种情况的解决方式是方法,使用本地托管多个个ssh的密钥,不同的账号是用不同的密匙。
第一步:生成SSH Key

执行命令,如下图:

具体说明:

Copy the code
Copy the code
1 $ ssh-keygen -t rsa -C "[email protected]"
2   Creates a new ssh key using the provided email Generating public/private rsa key pair.
3   #输入将要保存的路径,我的默认为当前路径(/c/Users/zhaoyafei/.ssh/id_rsa),但是不能使用默认文件id_rsa,要添加新账户就要生起一个成新的好记的名字,例如id_rsa_work
4   Enter file in which to save the key (/c/Users/zhaoyafei/.ssh/id_rsa):
5   #输入回车后提示输入一个类似于密码的号,直接回车就行
6   Enter passphrase (empty for no passphrase):
7   Enter same passphrase again:
Copy the code
Copy the code

第二步:识别新的私钥
    默认SSH只会读取id_rsa,所以为了让SSH识别新的私钥,需要将其添加到SSH agent  

1 ssh-add ~/.ssh/id_rsa_work
2  //该命令如果报错:Could not open a connection to your authentication agent.可执行 ssh-agent bash 命令后再执行ssh-add -l 看是否成功。

执行命令,如下图:

第三步:修改config文件
    该文件用于配置私钥对应的服务器。内容如下:

Copy the code
Copy the code
 1 # Default github (默认的)
 2       Host github.com
 3       HostName github.com
 4       User git
 5       IdentityFile ~/.ssh/id_rsa
 6 
 7 # second user([email protected])
 8       Host github_work
 9       HostName github.com
10       User git
11       IdentityFile ~/.ssh/id_rsa_work
Copy the code
Copy the code

       这样配置,也就是使用hostname为github.com会根据用户名的不同,去使用不用的private key。github上,也可以添加对应的公钥。其实这个配置是关于ssh的与git无关,只是git使用的方式是ssh的方式。Host可随意,方便自己记忆,后续在添加remote是还需要用到。


第四步:将SSH key输入到GitHub网站中    
      将生成的id_rsa_work.pub输入到GitHub网站中就可以了,输入完成后单击Add key后,会看到git-tutorial已经被添加进去了。 配置完成后,在连接非默认帐号的github仓库时,远程库的地址要对应地做一些修改,比如现在添加abc帐号下的一个仓库test,则需要这样添加:git remote add test git@github-work:abc/test.git #并非原来的[email protected]:abc/test.git 这样每次连接都会使用id_rsa_abc与服务器进行连接。


  注意:github根据配置文件的user.email来获取github帐号显示author信息,所以对于多帐号用户一定要记得将user.email改为相应的email([email protected])。我遇到的问题是本地单一用户,链接不同的github账号,所以用户配置可以采用全局配置。如果不同本地用户(不同的身份标识,即需要本地使用不同的用户名),那么git就不要使用有–global的配置,可以按照下面的方法:

Copy the code
1 1)设置局部的user.name和user.email
2     git config user.name "xxxxxx"
3     git config user.email "[email protected]"
4 2)设置全局的user.name和user.email
5     git config --global user.name "xxxxxx"
6     git config –-global user.email ""
Copy the code


六、题外话(相关概念)

Git - 版本控制工具
  Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。Torvalds 开始着手开发 Git 是为了作为一种过渡方案来替代 BitKeeper,后者之前一直是 Linux 内核开发人员在全球使用的主要源代码工具。开放源码社区中的有些人觉得 BitKeeper 的许可证并不适合开放源码社区的工作,因此 Torvalds 决定着手研究许可证更为灵活的版本控制系统。尽管最初 Git 的开发是为了辅助 Linux 内核开发的过程,但是我们已经发现在很多其他自由软件项目中也使用了 Git。例如 最近就迁移到 Git 上来了,很多 Freedesktop 的项目也迁移到了 Git 上。

Github - a site that offers the user space to create git warehousing, document or save some data such as user codes
  as open source code base and version control system, Github currently has more than 140 million developer users. As more and more applications into the cloud, Github has become the preferred method for managing software development and find existing code. As described above, as a distributed version control system, there is not only a main library in Git, each copy of the library can be used independently, any inconsistencies between the two libraries may be merger. GitHub hosts git repository, and provides a web interface, but other services like SourceForge or Google Code different, GitHub's unique selling point is the easy branching from another project. For a project to contribute code is very simple: First, click on the project site of the "fork" button, and then modify the code detected and added to your forked codebase, ultimately responsible to the project through the built-in "pull request" mechanism people proposing a merge. GitHub has already been referred to the Code of players MySpace.

GitLab - Git-based project management software
GitLab is an open source project for warehouse management systems. Using Git as a code management tool, and built up on the basis of web services. The three are based on git, git can be said derivatives.

Guess you like

Origin www.cnblogs.com/flzs/p/11302697.html