本文记录 Git 的常用命令与 Git GUI 软件的基本使用。
软件下载
SourceTree
安装

设置提交名字与邮箱

克隆远程仓库
点击 Clone 按钮,填入远程仓库地址,选择保存位置进行克隆

克隆完成后会在会自动打开仓库选项卡,下次打开 SourceTree 时已克隆的仓库会展示在 Local 页面

创建本地仓库
点击 Create 按钮,输入保存路径与仓库名字点击创建即可

添加已存在的本地仓库
点击 Add 按钮,选择想要添加的本地仓库路径后点击添加

之后已添加的仓库会显示在 Local 页面
从远程仓库拉取变更
从首页双击进入选择的仓库,点击拉取按钮

然后弹窗确认拉取

新增的文件会显示在 History 里
提交与推送
进入仓库页面后选择左侧面板的文件状态,会展示当前仓库的改动状况
选中某个文件可以可视化地比较当前文件的改动
点击文件右侧加号可以暂存文件,实际上是执行 git add
命令
在下方输入框里输入提交信息,点击提交,就完成了一个本地提交,实际上执行了 git commit -m "message"
命令
然后点击推送按钮推送到远程仓库,也可以在提交时勾选立即推送,实际是执行 git push
命令
推送完成后可以去 History 里查看自己的提交

分支
点击上方按钮栏的分支按钮,在弹出的界面里可以新建分支,创建后会切换到该分支

在左侧面板的“远程”下,列出了当前远程仓库的所有分支。右键某个分支,在弹出菜单中选择“检出 <分支名>”可以切换到改分支,实际上是执行命令git checkout <分支名>

左侧面板的“分支”下,同理切换本地分支
要合并分支,选择待合并分支,例如 wo
,然后点击右键,在弹出菜单中选择“合并 wo 至当前分支”,实际上是执行 git merge wo
命令

GithubDesktop
安装

初始化仓库
- 新建仓库: Ctrl + N
- 打开本地仓库: Ctrl + O
- 克隆仓库: Ctrl + Shift + O

拉取(pull)

提交(commit)

推送(push)

切换分支(checkout)

提交历史

常用命令
常用的6个命令,图片来自 www.ruanyifeng.com
初始化/克隆仓库
1 2 3 4 5 6 7 8
| $ git init
$ git init [project-name]
$ git clone [url]
|
配置文件
Git 的设置文件为 .gitconfig
,全局配置放在用户目录(~
或 C:\Users\<username>
)下,项目专用配置可以放在项目目录下。
1 2 3 4 5 6 7 8 9
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
|
增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
$ git rm [file1] [file2] ...
$ git rm --cached [file]
$ git mv [file-original] [file-renamed]
|
提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git fetch [remote]
$ git remote -v
$ git remote show [remote]
$ git remote add [shortname] [url]
$ git pull [remote] [branch]
$ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
|
分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| $ git branch
$ git branch -r
$ git branch -a
$ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
$ git checkout [branch-name]
$ git checkout -
$ git branch --set-upstream [branch] [remote-branch]
$ git merge [branch]
$ git cherry-pick [commit]
$ git branch -d [branch-name]
$ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
|
标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
|
查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| $ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
|
撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
其他
Reference
https://confluence.atlassian.com/get-started-with-sourcetree/get-started-with-sourcetree-847359026.html
https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
https://www.liaoxuefeng.com/wiki/896043488029600/1317161920364578
fin.