前文篇幅有限, 本文接着总结 4-10 个小点。
四、远程仓库
1️⃣ 查看当前的远程仓库
要查看当前配置有哪些远程仓库,可以用git remote命令,它会列出每个远程仓库的简短名字。在克隆完某个项目后, 至少 可以看到一个名为origin的远程仓库,也可以使用git remote -v显示对应的克隆地址。
2️⃣ 添加远程仓库
要添加一个新的远程仓库,可以指定-一个简单的名字,以便将来引用:
git remote add [shortname] [url]
这里的url也可以是一个本地Git项目文件夹,如
git remote add local repository ./test_repository
3️⃣ 从远程同步信息
git fetch [remote] #下载所有仓库的变动
git pull [remote] [branch] #取回远程仓库的变化并合并到本地分支
4️⃣ 推送数据到远程仓库
项目进行到一定阶段,可以将本地仓库中的数据推送到远程仓库。命令如下
git push [remote-name] [branch-name]
把本地的 master 分支推送到 origin 服务器上(克隆操作会自动使用默认的 master 和 origin ,并关联),可以运行下面的命令:
git push origin master
git push -u origin master #push的同时默认追踪分支
只有在所克隆的服务器上有写权限,或者同一时刻没有其他人在Push数据,这条命令才会执行成功。如果在Push数据前,已经有其他人推送了若干更新,那推送操作就会被驳回。必须先把其他人的更新merge到本地才能继续。
此外,当本次版本落后于远程仓库,但是想要用旧版本覆盖远程版本的话,使用 --force 选项
git push --force origin master
推送所有分支到远程仓库
git push [remote] --all
5️⃣ 查看远程仓库信息
git remote show [remote-name] # 查看某个远程仓库的详细信息
6️⃣ 远程仓库的删除和重命名
git remote rename #修改某个远程仓库在本地的简短名称
git remote rm #删除远程仓库
7️⃣ 检出远程仓库的某一分支
git checkout -b [local.branch] [remote.branch]
五、分支的使用
分支是在开发中经常使用到的一个功能。
-
查看分支
git branch #列出本地分支 git branch -r #列出远程分支 git branch -a #列出所有 本地分支 和 远程分支 git branch -v #查看各个分支最后一个提交对象的信息 git branch --merge #查看已经合并到当前分支的分支 git branch --no-merge #查看未合并到当前分支的分支
-
创建分支、重命名分支
git branch [branch-name] #新建立分支,但仍然停留在当前分支 git branch [branch] [commit] #新建立分支,指向指定commmit git branch -m [old_branch-name] [new_branch-name] #重命名立分支
-
切换分支
git checkout [branch-name] #切换到分支 git checkout -b [branch-name] #新建并切换到该分支 git checkout -b [branch1][branch2] #基于branch2新建branch1分支,并切换到branch1分支
-
删除分支
git branch -d [branch-name] #删除分支 git branch -D [branch-name] #强制删除分支
-
合并分支
git merge [branch-name] #将分支合并到当前分支 git rebase [branch-name] #将 branch-name 分支上超前的提交,变基到当前分支 git branch --set-upstream [branch] [remote-branch] #建立现有分支和指定远程分支的追踪关系
-
删除分支
git push origin --delete [branch-name] git push origin :[branch-name] git branch -dr [remote/branch-name]
六、标签的使用
当完成一个版本的开发,需要做发布的时候,给此版本打一个标签:
git tag # 列出现有的标签
-
**创建标签**
git tag [tag-name] #新建标签 git tag [tag-name] #新建一个 tag 在当前 commit git tag [tag-name] [commit] #新建一个 tag 在指定 commit git tag -a [tag] -m 'tag comment' # 新建带注释的标签 git checkout -b [branch] [tag] #新建一个分支,指向某个tag
-
**查看标签信息**
git show [tag] #查看 tag 信息
-
**切换到标签**
git checkout [tag] #切换到标签
-
**推送分支**
git push [remote] [tag] #推送分支到源上 git push [remote] --tags #一次性推送所有分支
-
**删除标签**
git tag -d #删除标签 git push origin :refs/tags/v0.1 #删除远程标签
七、日志
通过查看版本的日志记录,可以确定、跟踪代码的变化等;
-
**通过日志, 显示版本历史**
git log #显示当前分支的版本历史 git log --stat #显示 commit 历史,以及每次 commit 发生变更的文件,每次提交的文件增删数量
-
**通过日志, 显示指定文件的版本历史,包括文件改名**
git log --follow [file] git whatchanged [file]
git blame [file] #显示指定文件由谁何时修改过
git log -p [file] #显示指定文件相关的每一次diff
- 通过日志, 显示指定文件的版本历史,包括文件改名
-
**使用show查看提交变化**
git show [commit] #显示每次提交的元数据和内容变化 git show --name-only [commit] #显示某次提交发生变化的文件 git show [commit]:[filename] #显示某次提交某个文件的内容
git reflog #显示当前分支的最近几次提交
下面是git log 的高级用法:
git log --oneline # 把每一个提交压缩到一行
git log --decorate #显示指向这个提交的所有引用(比如说分支、标签等)
git shortlog #把每个提交按作者分类,显示提交信息第1行。这样可以容易地看到谁做了什么
git log --graph # 绘制一个ASCII图像来展示提交历史的分支结构
git log -[n] # 限制显示的提交数量
按照现实日期过滤显示结果,日期可以使用多种格式,比如 2017-1-1,yesterday等
git log --after=[date] #在指定日期之后
git log --before=[date] #在指定日期之前
按照作者、是否外来合并提交过滤显示结果
git log --author=[author] #按照作者,作者的邮箱地址就是作者的名字
git log --no-merges #排除外来合并提交
git log --merges #只显示外来和并提交
从master分支fork到feature分支后发生的变化
git log master..feature
git log -- xxx.java # --告诉后面是文件名不是分支名
git log --grep="xxx" #按提交信息来过滤提交
git log [last release] HEAD --grep feature #仅仅显示本次发布新增加的功能
git log -S xxx (-G [regex]) #根据内容(源代码)来过滤提交
git log --pretty=format:[string] #自定义输出格式,占位符: %cn指作者名字,%h指缩略标识,%cd指提交日期
git log [last-tag] HEAD --pretty=format:%s #显示上次发布后的变动,每个commit占据一行
八、撤销操作
在提交了错误的修改或者想要撤销文件的变动时,需要以下命令:
-
使用 git checkout 恢复工作区域
git checkout [file] #恢复暂存区域指定文件到工作区域 git checkout [commit] [file] #恢复某个commit的指定文件到工作区域 git checkout . #恢复上一个 commit 的所有文件到工作区域
-
使用 git reset 重置到commit中
git reset --hard #重置暂存区域和工作区域到上一次commit git reset [commit] [file] #重置当前分支到commit,重置暂存区域,但是工作区域不变 git reset --soft #只回退 commit,此时可以直接 git commit
git reset --hard [commit] #重置当前分支的HEAD为指定commit,同时重置暂存区域和工作区域,与指定 commit 保持一致
git reset --keep [commit] #重置当前分支的HEAD为指定commit,但是保持暂存区域和工作区域不变
git revert [commit] #新建一个commit,撤销指定commit,后者的所有变化都将被前者抵消,并且应用到当前分支中
-
回退版本
git reset HEAD^ #回退所有内容到上一个版本 git reset HEAD^ [file] #回退指定文件到上一个版本 git reset --soft HEAD~3 #向前回退到第3个版本
九、选择某些commit操作
使用 git cherry-pick 命令可以选择某一分支中的一个或者几个 commit 来进行操作。
比如,假设我们有一个稳定版本的分支 master,另外还有一个开发版本的分支 dev,我们无法直接合并两个分支,这样会导致稳定版本master混乱,但是又期望在master中使用dev的一个功能,这个时候就可以使用 cherry-pick。
git cherry-pick [commit-id]
十、解决冲突
在 rebase 或者 merge 时,有时候会产生 conflict,无法 auto merge,一般采取两种处理方式。
-
手动修改冲突文件: 修改冲突文件之后,使用 git add \ git commit \ git rebase continue 等后续操作即可
-
使用任意一方作为最新文件 git checkout --ours xx git checkout --theirs xx
本文使用 mdnice 排版