git常用

git工具的安装下载流程,以及git的常用语法,参照git官网。建议合理运用git --help

下载安装git

在Windows上安装需在官网https://git-scm.com/downloads下载,傻瓜式安装,默认下一步,添加环境变量路径(默认勾选)。

测试是否安装成功

安装完成后通过git --version测试是否安装成功,成功会出现版本号。
1

运行前配置

用户信息

1
2
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

文本编辑器

1
git config --global core.editor emacs

检查配置信息$ git config --list


常用语法

获取帮助

1
git --help

创建版本库

1
2
3
mkdir yourDir
cd yourDir
git init

添加暂存区

1
2
git add *.c
git add LICENSE

提交更新

1
git commit -m 'initial project version'

快捷添加暂存区并提交 $ git commit -a -m 'initial project version'

克隆现有仓库

1
git clone [url]

检查文件当前状态

1
git status

忽略文件

在.gitignore中可以通过正则或者添加文件来忽略git的监视,例如如下文件配置。

1
2
3
4
5
6
7
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/

移除文件

1
git rm [file]

移动文件

git不能移动文件,可以看做改名,重命名。

1
git mv file_from file_to

查看提交历史

默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面。

1
git log

一个常用的选项是 -p,用来显示每次提交的内容差异。 你也可以加上 -2 来仅显示最近两次提交:

1
git log -p -2

More info https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

撤销操作

取消暂存的文件

1
git reset HEAD <file>

修改上一次提交

1
2
3
git commit -m 'initial commit'
git add forgotten_file
git commit --amend

More info https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

远程仓库

查看远程仓库

加参数-v,可以看到url,不加只有简写名称
加show [remote-name],可以获取该仓库的具体信息

1
git remote -v

添加远程仓库

1
git remote add <shortname> <url>

拉取远程仓库

1
git fetch [remote-name]

git fetch 与 git pull的区别在于,git fetch不会自动merge,需要你手动完成。

推送远程仓库

1
2
3
4
git push [remote-name] [branch-name]

git push //默认推送约等于下一行命名,前提没有更改默认远程链接
git push origin master

远程仓库的移除与重命名

重命名pb为paul。

1
2
3
4
git remote rename pb paul
git remote
origin
paul

删除paul。

1
2
3
git remote rm paul
git remote
origin

打标签

列出标签

1
git tag

创建标签

附加标签,带有打标签者的信息,推荐使用。使用 git show 命令可以看到标签信息与对应的提交信息。

1
2
3
4
5
git tag -a v1.4 -m 'my version 1.4'
git tag
v0.1
v1.3
v1.4

轻量标签本质上是将提交校验和存储到一个文件中 - 没有保存任何其他信息。

1
2
3
4
5
6
7
git tag v1.4-lw
git tag

v0.1
v1.3
v1.4
v1.4-lw

后期打标签

查看历史并标记

1
2
3
4
5
6
7
8
9
git log --pretty=oneline

166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
git tag -a v1.2 9fceb02
git tag

v1.2

共享标签

1
git push origin v1.5

检出标签

1
git checkout -b [branchname] [tagname]

分支

创建

1
git branch [branchName]

切换

1
git checkout [branchName]

快捷创建切换使用$ git checkout -b [branchName]

删除

1
git branch -d [branchName]

整合

简单的整合

1
2
git checkout master
git merge [branchName]

变基整合
切换experiment分支,根据与master公共祖先整合

1
2
3
4
git checkout experiment
git rebase master
git checkout master
git merge experiment

More info https://git-scm.com/book/en/v2/Git-Branching-Rebasing

生成ssh密钥

enter跳过,默认无密码

1
ssh-keygen -t rsa -C "email"

可查看是否生成ssh。id_dsa.pub是公钥,用于github之类的托管库来添加ssh。

1
2
3
4
5
6
7
cd ~/.ssh
ls
id_dsa known_hosts
config id_dsa.pub

//查看公钥
vim id_dsa.pub

补充

克隆库使用的https协议,修改ssh协议

在.git文件夹中,通过编辑器打开config文件,设置https地址为ssh地址。

.gitignore规则不生效

在开发过程中,想忽略某个文件或文件夹,将其添加入.gitignore,规则存在不生效,其原因是已被纳入版本管理中,要想删除,需要清除本地缓存:

1
2
3
git rm -r --cached .
git add .
git commit -m 'update .gitignone'

创建可推送远程分支的分支

本地创建分支,并直接可以推送到远程相应分支。

1
2
3
4
5
##前提是远程有test仓库,如果没有先创建推送,在执行此命令
git checkout -b [bracnh] origin/[branch]

##已创建本地分支track远程分支
git branch --set-upstream [branch] [remote-branch]

查看本地分支跟踪的远程分支

1
2
3
4
5
# 当前分支跟踪的远程分支
git branch -vv

# 所有分支跟踪的远程分支
git branch -vv -a

远程库迁移且保存历史提交记录

1
2
3
4
5
6
7
# 方法一:克隆镜像,推送镜像到新的远程库
git clone --bare git://1xxx/project_name.git
cd project_name.git
git push --mirror git://2xxx/project_name.git

# 方法二:克隆到本地,修改远程库推送地址
git remote set-url [--push] <name> <newurl> [<oldurl>]

撤回操作

工作区

1
2
3
# 撤回工作区
git checkout .
git checkout -- <file>

暂存区

1
2
3
4
5
6
# git追踪过的某个文件
git reset <file>
# git未追踪过的新文件
git reset HEAD <file>
# 完全移除不需要
git reset --hard

本地提交

1
2
3
4
5
6
7
# 回退到某个分支,--hard慎用
git reset --hard [<commit>]
# 撤回上一次提交,但保存修改的文件到暂存区
git reset --soft HEAD~1

# 回退另一种方式
git checkout [<commit>]

远程库

1
2
# 通过上述的回退后,强制推送远程
git push -f

撤销撤回的操作

1
2
3
# 查看回退的commitId
git reflog
git reset --hard [<commit>]

更多信息详见git官网
https://git-scm.com/book/zh/v2

------本文结束 感谢阅读------