Git常用命令及操作总结
By:授客 QQ:1033553122
利用TortoiseGit克隆源码库到本地
1、安装TortoiseGit
2、打开Git,进入到源码库,点击图示红色选框框选按钮,弹窗界面中复制链接
2、进入要存放源码库的本地目标目录(比如:E:Git),右键鼠标-选择Git-Clone,
弹出如下界面
3、如上图,按默认配置,点击Ok 按钮,弹窗密码输入框中输入密码,点击Ok
就这样,会把远程代码的master分支下载到本地目录(例中为 E:Gitddt-core-web)。
注:只会克隆master分支到本地
常用命令总结
git clone
git clone [option] <</span>版本库url> <</span>本地目录>
例:下载源码库master分支到本地E:Gitddt-core-web目录
git clone --progress -v "http://01094711@10.116.218.109:8080/scm/ddt/ddt-core-web.git" "E:Gitddt-core-web"
Cloning into 'E:Gitddt-core-web'...
POST git-upload-pack (gzip 1100 to 620 bytes)
remote: Counting objects: 20726, done.
remote: Compressing objects: 100% (10996/10996), done.
Receiving objects: 100% (20726/20726), 60.25 MiB | 11.28 MiB/s, done.
Resolving deltas: 100% (10079/10079), done.
remote: Total 20726 (delta 10079), reused 15985 (delta 7525)
Checking connectivity... done.
Checking out files: 100% (2132/2132), done.
Success (59156 ms @ 2017/5/25 11:15:16)
参考连接:
http://www.yiibai.com/git/git_clone.html
git
remote
Git要求每个远程主机都必须指定一个主机名。git remote命令就用于管理主机名。
例:
cd E:Gitddt-core-web
注:除git colne命令外,git remote及以下命令的执行,都要先进入克隆的版本库所在的本地库目录才可以执行,否则会提示类似如下错误
fatal: Not a git repository (or any of the parent directories): .git
列出所有远程主机
E:Gitddt-core-web>git remote
origin
使用-v选项,可以参看远程主机的网址
E:Gitddt-core-web>git remote -v
origin http://01094711@10.116.218.109:8080/scm/ddt/ddt-core-web.git (fetch)
origin http://01094711@10.116.218.109:8080/scm/ddt/ddt-core-web.git (push)
输出说明当前只有一台远程主机,叫做origin,以及它的网址
查看主机的详细信息(git remote show <</span>远程主机名>)
E:Gitddt-core-web>git remote show origin
Password for 'http://01094711@10.116.218.109:8080':
* remote origin
Fetch URL: http://01094711@10.116.218.109:8080/scm/ddt/ddt-core-web.git
Push URL: http://01094711@10.116.218.109:8080/scm/ddt/ddt-core-web.git
HEAD branch: master
Remote branches:
master tracked
production tracked
release/V5.3 tracked
release/V5.4 tracked
release/V5.5 tracked
release/V5.6 tracked
release/V5.7 tracked
release/V5.8 tracked
release/V5.9 tracked
release/V6.0 tracked
release/V6.1 tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
参考链接
http://www.yiibai.com/git/git_remote.html
git branch
查看所有本地分支
E:Gitddt-core-web>git branch* master
说明:* 表示该分支为当前分支
查看所有远程分支
E:Gitddt-core-web>git branch -r
origin/HEAD -> origin/master
origin/master
origin/production
origin/release/V5.3
origin/release/V5.4
origin/release/V5.5
origin/release/V5.6
origin/release/V5.7
origin/release/V5.8
origin/release/V5.9
origin/release/V6.0
origin/release/V6.1
查看所有分支
E:Gitddt-core-web>git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/production
remotes/origin/release/V5.3
remotes/origin/release/V5.4
remotes/origin/release/V5.5
remotes/origin/release/V5.6
remotes/origin/release/V5.7
remotes/origin/release/V5.8
remotes/origin/release/V5.9
remotes/origin/release/V6.0
remotes/origin/release/V6.1
删除指定分支(git branch -d branch_name)
E:Gitddt-core-web>git branch -d myBranch
git fetch
将某个远程主机的更新,全部取回本地(git fetch <<font face="宋体">远程主机名>)。
E:Gitddt-core-web>git fetch origin
Password for 'http://01094711@10.116.218.109:8080':输入密码,回车
remote: Counting objects: 46, done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 46 (delta 14), reused 0 (delta 0)
Unpacking objects: 100% (46/46), done.
From http://10.116.218.109:8080/scm/ddt/ddt-core-web
4999476..99ebceb master -> origin/master
注意:默认情况下,git fetch取回所有分支(branch)的更新。如果只想取回特定分支的更新,可以指定分支名
取回远程主机的指定分支(git fetch 远程主机名 分支名)
E:Gitddt-core-web>git fetch origin master
Password for 'http://01094711@10.116.218.109:8080':输入密码,回车
From http://10.116.218.109:8080/scm/ddt/ddt-core-web
* branch master -> FETCH_HEAD
例中意在取回orgin远程主机上名为master的分支
git check out
执行fetch命令,取回远程主机的更新后,便可使用git check out命令来建立本地分支
git checkout -b -newbranch []
创建新的分支并切换到新分支上去,b代表branch的意思,newbranch 是本地分支的名称,如果本地分支名已经存在, 则不需要“-b”选项。如果没有指定提交点(start_point),默认从HEAD指向的提交创建分支。
在指定分支的基础上,新建本地分支
E:Gitddt-core-web>git checkout -b newBranch origin/master
Branch newBranch set up to track remote branch master from origin.
Switched to a new branch 'newBranch'
以上命令的执行,会在origin/master的基础上,新建newBranch分支
说明:
1、创建本地分支并不会新建对应名称的文件夹;
2、新建分支后,会自动切换当前分支为新建的本地分支,如下
E:Gitddt-core-web>git branch -a
master
* newBranch
remotes/origin/HEAD -> origin/master
remotes/origin/master
……
再新建一个本地分支newBranch2
E:Gitddt-core-web>git checkout -b newBranch2 origin/master
Branch newBranch2 set up to track remote branch master from origin.
Switched to a new branch 'newBranch2'
E:Gitddt-core-web>git branch -a
master
newBranch
* newBranch2
……
切换当前分支为其它某个已存在分支(git checkout )
E:Gitddt-core-web>git checkout newBranch
Switched to branch 'newBranch'
Your branch is up-to-date with 'origin/master'.
E:Gitddt-core-web>git branch -a
master
* newBranch
……
参考连接:http://www.yiibai.com/git/git_fetch.html
git merge
在本地分支上合并指定分支。
E:Gitddt-core-web>git merge newBranch2
Already up-to-date.
以上命令的执行会把newBranch2分支合并到当前分支上
把origin/master分支合并到当前分支上
E:Gitddt-core-web>git merge origin/master
Already up-to-date.
参考连接:http://www.yiibai.com/git/git_fetch.html
git pull
git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。
git pull <<font face="宋体">远程主机名> <</font>远程分支名>:<</font>本地分支名>
例,取回origin主机的master分支,与本地的newBranch分支合并
E:Gitddt-core-web>git pull origin master:newBranch
Password for 'http://01094711@10.116.218.109:8080':
remote: Counting objects: 209, done.
remote: Compressing objects: 100% (177/177), done.
remote: Total 209 (delta 93), reused 0 (delta 0)
Receiving objects: 100% (209/209), 284.77 KiB | 0 bytes/s, done.
Resolving deltas: 100% (93/93), completed with 38 local objects.
From http://10.116.218.109:8080/scm/ddt/ddt-core-web
99ebceb..1ef1bd3 master -> newBranch
99ebceb..1ef1bd3 master -> origin/master
warning: fetch updated the current branch head.
fast-forwarding your working tree from
commit 99ebceb3a6430ef828f9fe8a4df6f4283ef35b99.
Already up-to-date.
取回origin/master分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。
git fetch origin
git merge origin/next
如果远程分支是与当前分支合并,则冒号后面的部分可以省略
E:Gitddt-core-web>git pull origin master
Password for 'http://01094711@10.116.218.109:8080':
From http://10.116.218.109:8080/scm/ddt/ddt-core-web
* branch master -> FETCH_HEAD
Updating 4999476..1ef1bd3
Fast-forward
ddt-main/pom.xml | 7 ++
.../java/com/sf/ddt/auth/action/AuthAction.java | 4 +-
.../sf/ddt/express/action/SelfHelpDatasAction.java | 28 +++----
……
在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。
Git也允许手动建立追踪关系。
git branch --set-upstream master origin/next
上面命令指定master分支追踪origin/next分支。
如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名
E:Gitddt-core-web>git pull origin
Password for 'http://01094711@10.116.218.109:8080':
Already up-to-date.
上面命令表示,取回当前分支与对应的origin主机”追踪分支”(remote-tracking branch),与本地的当前分支进行合并。
如果当前分支只有一个追踪分支,连远程主机名都可以省略
E:Gitddt-core-web>git pull
Password for 'http://01094711@10.116.218.109:8080':
Already up-to-date.
如果远程主机删除了某个分支,默认情况下,git pull 不会在拉取远程分支的时候,删除对应的本地分支。这是为了防止,由于其他人操作了远程主机,导致git pull不知不觉删除了本地分支。但是,你可以改变这个行为,加上参数 -p 就会在本地删除远程已经删除的分支。
$ git pull -p
# 等同于下面的命令
$ git fetch --prune origin
$ git fetch -p
git pull --rebase
如果采用git rebase呢,git rebase命令会把你的当前分支里的每个提交(commit,上次 pull 之后的变更)取消掉,并且把它们临时保存为补丁(patch)(这些补丁放到".git/rebase"目录中),然后合并远程分支到当前分支,最后把保存的这些补丁应用到当前分支上
参考链接:
http://www.yiibai.com/git/git_pull.html