git common operation commands

git common operation commands

1 git working principle diagram

git schematic

2 git remote operation

2.1 git clone

  • The first step in a remote operation is usually to clone a repository from a remote host:
git clone <版本库的网址>  克隆远程代码
  • git clone supports multiple protocols:
$ git clone http[s]://example.com/path/to/repo.git/

$ git clone ssh://example.com/path/to/repo.git/

$ git clone git://example.com/path/to/repo.git/

$ git clone /opt/git/project.git

$ git clone file:///opt/git/project.git

$ git clone ftp[s]://example.com/path/to/repo.git/

$ git clone rsync://example.com/path/to/repo.git/

2.2 git fetch

  • Pull all code updates from the remote server:
$ git fetch <远程主机名>
  • Pull the specified branch code:
$ git fetch <远程主机名> <分支名>

如:

$ git fetch origin master 拉取远程仓库代码,但不合并代码

$ git merge origin/master 合并远程代码到当前分支
  • The retrieved updates should be read on the local host in the form of "remote hostname/branchname". For example, the master of the origin host should be read with origin/master.

  • The -r option of the git branch command can be used to view remote branches, and the -a option to view all branches.

$ git branch -r
$ git branch -a


- On the basis of the above, use the git checkout command to create a new branch.

$ git checkout -b newBrach origin/master
上面命令表示,在origin/master的基础上,创建一个新分支。
  • You can also use the git merge command or the git rebase command to merge a remote branch on a local branch:
$ git merge origin/master
或者
$ git rebase origin/master
上面命令表示在当前分支上,合并origin/master

2.3 git remote

  • For ease of administration, Git requires that each remote host must specify a hostname. The git remote command is used to manage hostnames.
    With no options, the git remote command lists all remote hosts.
$ git remote
  • Using the -v option, you can see the URL of the remote host:
$ git remote -v

The above command indicates that there is currently only one remote host, called origin, and its URL.

  • The git remote show command plus the host name can view the detailed information of the host.
$ git remote show <主机名>

2.4 git pull

  • The function of the git pull command is to retrieve the update of a branch of the remote host, and then merge it with the specified local branch
$ git pull <远程主机名><远程分支名>:<本地分支名>

For example, to retrieve the next branch of the origin host and merge it with the local master branch, it needs to be written as follows:

$ git pull origin next:master

The above command indicates that the origin/next branch is retrieved, and then merged with the current branch (here refers to master). Essentially, this is equivalent to:

$ git fetch origin next

$ git merge origin/next
  • In some cases, Git will automatically establish a tracking relationship between the local branch and the remote branch. For example, during git clone, all local branches default to the remote host's branch with the same name to establish a tracking relationship, that is, the local master branch automatically "tracks" the origin/master branch. Git also allows manual tracking of relationships.
$ git branch --set-upstream master origin/next
上面命令指定master分支追踪origin/next分支
  • If the current branch has a tracking relationship with the remote branch, git pull can omit the remote branch name.
$ git pull origin

上面命令表示,本地的当前分支自动与对应的origin主机"追踪分支"(remote-tracking branch)进行合并
  • If the current branch has only one tracking branch, even the remote hostname can be omitted
$ git pull
上面命令表示,当前分支自动与唯一一个追踪分支进行合并

2.5 git push

  • The git push command is used to push the update of the local branch to the remote host
$ git push <远程主机名> <本地分支名>:<远程分支名>
注意,分支推送顺序的写法是<来源地>:<目的地>,所以git pull是<远程分支>:<本地分支>,而git push是<本地分支>:<远程分支>
  • If the remote branch name is omitted, it means that the local branch will be pushed to the remote branch with which there is a "tracking relationship" (usually the two have the same name). If the remote branch does not exist, it will be created.
$ git push origin master
上面命令表示,将本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建。
  • If the local branch name is omitted, it means to delete the specified remote branch, as this is equivalent to pushing an empty local branch to the remote branch.
$ git push origin :master
等同于
$ git push origin --delete master
上面命令表示删除origin主机的master分支
  • If there is a tracking relationship between the current branch and the remote branch, both the local branch and the remote branch can be omitted.
$ git push origin
上面命令表示,将当前分支推送到origin主机的对应分支
  • If the current branch has only one tracking branch, the hostname can be omitted
$ git push
  • If the current branch has a tracking relationship with multiple hosts, you can use the -u option to specify a default host, so that you can use git push without any parameters later
$ git push -u origin master
上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push

2.6 Delete the local branch and the server branch at the same time (eg: delete the test branch)

  • the first method:
git checkout master    首先切换到其他分支
git branch -d test     删除本地test分支
git push --delete origin test  删除远程服务器上的test分支
  • The second method:
git checkout master    首先切换到其他分支
git push origin :test   用一个空的分支替换远程test分支

3 local operation commands

  • Common commands:
git init   对文件夹进行git初始化
git init myProject 新建一个文件夹,然后在文件中初始化git;
git status   获取git状态
git add . / git add -A 两者效果一样:添加所有文件到分支中(暂存修改文件,处于待提交状态)
git diff 查看工作区和暂存区的差异
git diff --cached 查看工作区和历史区的差异
git rm xxx 移除文件
git rm -f xxx 强制移除文件
git mv old.xx new.xx  重命名文件
git branch 查看分支
git branch wmh  新建分支
git checkout wmh  切换到wmh分支
git checkout master -b test_branch    在master代码基础上创建test_branch分支并切换到test_branch分支(新分支是master的代码);
git commit -m "log" 提交到本地分支
git branch -d wmh 删除本地分支
git merge origin/test11 合并远程分支
git reset --hard ORIG_HEAD 撤销合并(副作用:清除workdirectory)
git merge orgin/test11 撤销合并并保存work directory
git merge --abort 放弃合并;
git log --oneline --decorate(引用) --graph(图形) --all(所有) 查看历史提交记录
git config --global alias.lol "log --oneline --decorate --graph --all" 给命令取别名 之后就直接可以使用:git lol 查看历史提交记录了
git stash save -a "暂存分支代码"   把当前分支的代码暂存起来,并填写暂存信息;
git stash list   查看当前分支的暂存列表;
git stash pop --index stash@{0}  把当前分支暂存列表的第0个暂存还原出来;如果暂区存只有一条暂存消息则直接使用git stash pop 默认还原第0条暂存信息;pop:会还原暂存信息并删除暂存区中已还原的暂存信息(还原并删除);
git stash drop stash@{0} 删除当前分支指定暂存;
git stash clear  删除当前分支所有暂存;
git show HEAD 查看当前分支提交记录

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588253&siteId=291194637