Git
是一个分布式的版本控制系统, 起初用作
Linux
内核代码的管理
,
而现在在其他项目中也取得了很大的成功。
Github
作为
git
库最大的托管平台,拥有百万的开发者,海量的开源代码,是大家学习和工作很好的平台,所以不管学习什么语言,掌握
Git
相关的知识是开发者必备的技能。本文和大家分享的主要是git
的相关命令,一起来看看吧,希望对大家
学习有所帮助。
基本使用命令
git clone <url>
ssh/http(s)/git
三种协议,
ssh
和
https
可推送
git init
初始化
Git
仓库
Git
日常操作
git add <file>
将文件加入
index file
git rm [--cached]
删除,加
--cached
表示仅从
index file
中删除文件,即放弃跟踪
git mv <src> <dest>
移动
/
更名
git diff --cached/--staged
当前索引与上次提交(有哪些需要
commit
)
git diff
当前索引与工作目录(有哪些需要
add
)
git diff HEAD[^]
工作目录与上次提交(当前目录与上次提交有何改变)
git commit [-a] -m <msg>
提交
git commit --amend [-m <msg>]
修复上次提交
git reset HEAD <file>
同
--mixed
,
default option
git reset --mixed HEAD
撤销
commit
和
index file,
只保留
working tree
的信息
git reset --hard HEAD[^]
将
working tree
和
index file
都撤销到以前状态
git reset --soft HEAD[^]
只撤销
commit,
而保留
working tree
和
index file
的信息
回复到某个状态。以git reset --soft HEAD
为例,
commit
回退到
HEAD
(相当于无变化),若是
HEAD^
,则
commit
回退到
HEAD^
git gc
用垃圾回收机制清除由于
reset
而造成的垃圾代码
git status
显示当前工作目录状态
git log [-p]
显示提交历史(
many useful options to be learned
)
git branch [branch]
显示
/
新建分支
git branch -d/-D
删除分支(
d
表示“在分支合并后删除分支”,
D
表示无论如何都删除分支)
git show-branch
git checkout <branch>
切换分支(分支未
commit
无法切换)
git merge <branch>
合并分支
git remote [-v]
显示远程仓库,加
-v
选项可显示仓库地址
git remote add <repo_name> <url>
添加远程仓库,
repo_name
为
shortname
,指代仓库地址
git remote rename <old_name> <new_name>
更名
git remote rm <repo_name>
删除远程仓库
git remote show <repo_name>
查看远程仓库信息
git remote fetch <repo_name>
从远程仓库抓取数据(并不合并)
git pull <repo_name> <branch_name>
拉去数据并合并到当前分支
git push <repo_name> <branch_name>
推送指定分支到指定仓库