编辑推荐: |
本文来自mamicode,主要写给使用命令行来操作Git的用户,用于提高Git使用的效率。 |
|
前言
本文主要写给使用命令行来操作Git的用户,用于提高Git使用的效率。至于使用命令还是GUI(Tortoise
Git或VS的Git插件)就不在此讨论了,大家根据自己的的喜好选择就好。我个人是比较推荐使用命令行的,因为命令行是原生的客户端,当熟悉了命令行以后,使用效率并不会比GUI低。而且我是从GUI(Tortoise
SVN)转命令行的,刚开始的时候也是吐槽命令行的“非人性化”,但我走出了舒适区,真正使用起命令行后,我才发现了自己之前的想法是肤浅的。
指令别名
指令别名就是给一些常用的指令设置一个较短的指令别名来提高Git的使用效率。
如git st(git status):

使用指令别名,并不会把原指令覆盖掉,而是同时生效:

指令别名输错的时候,也会有提示:

我在这里会分享我设置的指令别名,希望能帮助提高使用效率。
1. 查看状态(Show the working tree status) - git status
git st

2. 提交(Record changes to the repository) - git commit
git ci

3. 分支操作(List, create, or delete branches) - git
branch
git br

4. 签出(Switch branches or restore working tree files)
- git checkout
git co
签出分支:

签出(还原)工作区文件:

签出(还原)工作区所有文件:

5.差异(Show changes between commits, commit and working
tree, etc) - git diff
工作区(working driectory)与暂存区(staging area)的差异:
git df
单个文件差异:

所有文件差异:

暂存区(staging area)与本地仓库(repository)的差异:
git dfs
单个文件差异:

多个文件差异:

6.合并(Join two or more development histories together)
- git merge
git mr

7.日志(Show commit logs) - git log
查看最近一条日志:
git last

查看日志及分支提交信息:
git ls

其中绿色的表示local分支,红色的的表示host分支,从上图可以看出,local的master分支、host的master分支、host的dev分支都处于同一个提交,local的dev分支领先于前面提到的3个分支。当版本显示不全提交时,可以按回车(回车)继续加载提交,需要退出是,按字母q。另外,你可以加“-n”指定查看前n条记录:

查看日志、分支提交信息、提交人、提交时间:
git lg

当版本显示不全提交时,可以按回车(回车)继续加载提交,需要退出是,按字母q。另外,你可以加“-n”指定查看前n条记录:

8.变基(Reapply commits on top of another base tip)
- git rebase
git rb

我理解的rebase和merge功能上比较类似,只是rebase操作后,提交基本都会在一条线上,而merge会从一条线合到到另外一条线。
Merge:

Rebase:

9.拣选(Apply the changes introduced by some existing
commits) - git cherry-pick
git cb

从图中看看出,cherry-pick是把某个提交复制过来作为一个新的提交,和merge或者rebase的效果不同,check
pick后可以进行merge或rebase操作:

使用Beyond Compare作为差异比较工具
命令行自带的文件差异比较工具功能比较简单,用来查看或对比文件差异时,可能效果不太好,特别是在解决冲突的时候,但是我们可以集成一些比较强大的差异比较工具,比如本文提到的Beyond
Compare。那么,我们先看一下例子:
自带的差异比较效果:

使用difftool指令调用Beyond Compare的比较效果:


当然difftool也可以使用指令别名:

解决合并冲突:
我们在dev的a.txt文件中添加“dev”内容,在master分支上添加“master”内容,然后用master分支合并dev。

使用mergetool指令调用Beyond Compare合并冲突:


由上图可以看出,左边是当前分支的版本,右边的是需要合并的分支的版本,中间的是相同部分的版本,处理冲突的策略大概有以下几种:
#1 当前分支版本替换合并分支版本,点击绿色的箭头。
#2 合并分支版本替换当前分支版本,点击粉色的箭头。
#3 同时需要两个版本的内容,点击绿色或粉色的箭头,然后把另外的分支的内容拷贝到下方的文本编辑框。
#4 只要相同部分的内容,什么都不干。
#5 其他。
最后,点击红色的感叹号,表示已解决冲突。我这里选择#3的策略:

关闭Beyond Compare,完成合并操作。

如何配置
.gitconfig是配置的主要文件,它一般在C:\Users\{用户名}目录中,如果没有,可以通过以下两步创建:
#1 打开命令行工具

#2 执行设置用户名或者设置邮箱的指令。
设置用户名:
git config --global user.name "{your username}"
设置邮箱:
git config --global user.email "{your email}"
打开.gitconfig文件,把以下内容拷贝到文件中,完成指令别名的配置:
[alias]
st = status
ci = commit
co = checkout
br = branch
df = diff
dft = difftool
dfs = diff --staged
dfts = difftool --staged
mr = merge
mrt = mergetool
last = log -1 HEAD
ls = log --oneline --graph --all
--decorate
g = log --oneline --graph --all --decorate
--pretty= format :"%h%x20% Cgreen%d% x20% Cred%
an%x20% C(yellow)% ad%x20% Creset%s" --full-history
--date = short
rb = rebase -i
cp = cherry-pick
打开.gitconfig文件,把以下内容拷贝到文件中,完成Beyond Compare的配置:
[diff]
tool = bc4
[difftool]
prompt = true
[difftool "bc4"]
cmd = \"D: /Program Files /Beyond
Compare 4 /bcomp.exe\" "$ (cygpath -w $LOCAL)"
"$ REMOTE "
[merge]
tool = bc4
[mergetool]
prompt = true
[mergetool "bc4"]
#trustExitCode = true
cmd = \"D:/Program Files /Beyond
Compare 4 /bcomp.exe \" "$LOCAL" "$REMOTE"
"$BASE" "$ MERGED "
最终效果如图所示:

最后
希望以上分享的小技巧可以提高大家的效率,大家也可以根据自己的需要去设置指令别名,欢迎大家讨论及分享指令别名的使用心得。 |