GIT 常用命令

1. 初始命令

init 初始化仓库

$ git init

clone 克隆仓库

$ git clone https://github.com/repname
$ git clone https://github.com/repname myrep #myrep作为本地仓库名

config 配置

$ git config --list                #查看配置
$ git config --system user.name    #系统级配置
$ git config --global user.name    #全局配置,系统用户级
$ git config user.name             #仓库级配置

help 帮助

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

remot 远程仓库

$ git remote -v                   #查看远程仓库
$ git remote show origin           #查看远程仓库
$ git remote add pb https://github.com/pb #添加远程仓库
$ git remote rename pb paul        #重命名远程仓库
$ git remote rm paul             #移除远程仓库

 2.基础命令

status 查看文件状态

$ git status
$ git status -s     #状态简览

add 暂存已修改文件

$ git add filename
$ git add -A        #暂存所有已修改文件

commit 提交更新

$ git commit -m "commit message"
$ git commit -a -m "commit message"    #跳过暂存区,直接提交

push 推送到远程仓库

$ git push
$ git push origin master

fetch 从远程仓库拉取数据

$ git fetch [remote-name]

merge 合并分支

$ git merge

pull ( fetch + merge )

#用 fetch 和 merge 好了

3.常用命令

diff 查看修改

$ git diff              #比较 暂存区-工作区
$ git diff --staged     #比较 仓库-暂存区

log 查看提交历史

$ git log
$ git log --stat       #展示提交的简略统计信息

撤销操作

$ git commit --amend         #重新提交
$ git reset HEAD filename    #取消暂存的文件

checkout 撤销对文件的修改

$ git checkout filename
# 会覆盖工作区文件
# 如果暂存区有改动的文件,则从暂存区到工作区
# 如果暂存区无改动的文件,则从仓库到工作区

tag 打标签

$ git tag     #查看标签
$ git tag -a v1.4 -m "my version 1.4"   #创建附注标签
$ git tag v1.4                  #创建轻量标签
$ git tag -a v1.2 9fceb02           #对某次提交后期打标签
$ git push origin v1.5             #上传某个标签,GIT 默认不会 push 标签到远程仓库
$ git push origin --tags           #上传所有不在远程仓库的标签
$ git checkout -b version2 v2.0.0      #检出标签

rm 移除文件

$ git rm filename         #个人感觉效果同 rm
$ git rm --cached filename  #移除暂存区中的文件

mv 移动文件

$ git mv file_from file_to    #个人感觉效果同 mv

 3.分支命令

branch 创建分支

$ git branch          #查看分支,前面带星号*的,是当前分支
$ git branch testing     #创建 testing 分支
$ git branch -d testing  #删除 testing 分支
$ git branch -v   #查看每个分支最后一次提交
$ git branch --merged     #查看已合并到当前分支的分支
$ git branch --no-merged  #查看未合并到当前分支的分支

checkout 切换分支

$ git checkout testing
$ git checkout -b iss53   #创建分支,并切换到新创建的分支

merge 合并分支

$ git merge hotfix           #把 hotfix 分支,合并到当前分支
$ git mergetool    #图形化解决冲突的工具

4.底层命令

cat-file 读取 GIT 仓库对象

$ git cat-file -p f8a67de1d4bf0d6dbaaaf8990ffe8394e5fa88ee    #查看对象内容
$ git cat-file -p master^{tree} #master 分支上最新的提交所指向的 tree 对象
$ git cat-file -t f8a67de1d4bf0d6dbaaaf8990ffe8394e5fa88ee #查看对象类型
$ git cat-file -s f8a67de1d4bf0d6dbaaaf8990ffe8394e5fa88ee #查看对象大小

hash-object 操作 GIT 仓库对象

$ git hash-object -w filename.txt   #把 filename.txt 文件内容写入 GIT 仓库

update-index 操作暂存区

$ git update-index --add --cacheinfo 100644 f8a67de1d4bf0d6dbaaaf8990ffe8394e5fa88ee #创建暂存区,并把 blob 对象添加到暂存区
$ git update-index test.txt      #更新暂存区
$ git update-index --add new.txt  #向暂存区添加文件

write-tree 创建树对象

$ git write-tree         #根据当前暂存区内容,创建树对象

read-tree 把树对象读入到暂存区

$ git read-tree 754a1e2d567bbbcee762a2d7768407f4d3290fc8
$ git read-tree --prefix=bak 754a1e2d567bbbcee762a2d7768407f4d3290fc8 #把树对象当作子树读入暂存区

commit-tree 创建提交对象

$ echo 'first commit' | git commit-tree 754a1e             #根据树对象,创建提交对象
$ echo 'second commit' | git commit-tree 754a1e -p 32b8d2 #创建提交对象时,指定父提交对象

update-ref 创建引用(分支、标签)

$ git update-ref refs/heads/master 32b8d2
#或者如下直接编辑引用文件,不提倡这么做
$ echo "32b8d2094acef696efa3ca1b0a29639d97be1684" > .git/refs/heads/master

sysmbolic-ref 符号引用(HEAD)

$ git symbolic-ref HEAD refs/heads/test
#或者如下直接编辑引用文件,不提倡这么做
$ echo "refs/heads/test" .git/HEAD

远程引用

#远程引用是只读的,看看就好了

gc 生成包文件

$ git gc
#作用:完整保存最新版文件,历史版本文件保存差异
#GIT 会根据情况自己执行,一般不需要手动之行
原文地址:https://www.cnblogs.com/wanggs/p/5595521.html