快捷查询
GIT常用命令查询
工程准备: git init / git clone
$ git init firstproject
$ git clone [URL]
$ git lfs clone [URL] # 项目文件对二进制文件进行了区别管理,clone的时候要使用lfs
注:项目文件对二进制文件进行了区别管理,clone的时候要使用lfs
新增/删除/移动文件到暂存区:git add / git rm / git mv
老版本的git需要先add 再提交新版本的不需要add就可以直接提交
$ git add newfile.txt
一次性将多个文件全部添加到暂存区:
$ git add .
将oldfile从当前分支的缓冲区删除:
$ git rm oldfile.txt
mv可以移动文件也可以重命名文件:
$ git mv nginx.conf config #把.conf文件移动到config目录下
$ git mv config/nginx.conf config/new_nginx.conf
查看工作区:git diff / git status
git diff
比较两个节点的差异
$ git diff 374b8e7 d2f56b8
比较两个分支的差异
$ git diff master..chen/develop/framework
比较当前索引和上次提交的差异
$ git diff --cashed
只查看不同的文件列表
$ git diff master..chen/develop/framework --name-status
M:modified
A:Added
D:Deleted
git status
提交更改的文件 git commit
注意:提交是本地的动作,提交要说明提交信息,简述提交的内容让别人清楚, 不要忘记了-m
$ git commit file_name -m "commit message"
一次性提交所有在暂存区改动的文件到版本库:
$ git commit -am "commit message"
如果要后续修改 commit message
$ git commit --amend
直接键入:i ,此时进入了输入模式,修改完成后,按下 Esc键退出编辑模式,在键入 :wq 回车退出并保存修改,完成提交。
查看日志 git log
可以灵活搭配参数使用,后续补充
与默认相比,新增显示文件名:
$ git log --name-status
只查看最近一次更新的内容:
$ git log -1
推送远端仓库 git push
区别:
git commit:从暂存区到本地版本库
git push: 将本地版本库的分支推送到远程服务器对应的分支
$ git push origin branch_name # 远端服务器上的分支名和本地的分支名相同
$ git push origin branch_name:new_branch_name#本地分支名为branch_name推送到远端后为new_branch_name
注意:Windows中分支名大小写不敏感 无区别
分支管理
查看分支:git branch
查看当前本地工程所有分支的名称
$ git branch
"*"表示当前所在的分支
查看远端服务器上的分支
$ git branch -r
查看远端和本地工程的所有分支
$ git branch -a
新建分支:git checkout -b
新建分支但不切换到新的分支
$ git branch chen/firstgit
新建分支后切换到新的分支(常用)
$ git checkout -b debug_branch_1
删除分支:git branch -d/-D
删除本地分支
$ git branch -d branch_name
$ git branch -D branch_name #强制删除
$ git push origin : branch_name #删除后要推送到服务器上
删除服务器上的分支
$ git branch -d -r branch_name
切换分支:git checkout
$ git checkout branch_name
$ git checkout -f branch_name #强制切换
从远端获取分支: git pull / git fetch
git pull 从服务器中获取分支的更新 同时与本地指定分支自动合并:(一般用来更新同名的分支,不同名的尽量不要这样做)
$ git pull origin remote_branch:local_branch
$ git pull origin remote_branch#(常用,远程分支与本地分支相同)
$ git pull = git fetch + git merge
git fetch 从服务器中获取分支的更新 不与本地分支合并 比较安全一点 温和一点
$ git fetch origin remote_branch:local_branch
$ git fetch origin remote_branch#(常用,远程分支与本地分支相同)
分支合并:git merge / git rebase
从指定分支(节点)合并到当前分支的操作git会将指定的分支与当前分支进行比较,找出二者最近的一个共同节点base,之后将指定分支在base之后分离的节点合并到当前分支上。分支合并,实际上是分支间差异提交节点的合并。
$ git merge branch_name # 把branch_name 上与当前分支之间存在差异的部分合过来
eg:$ git merge chenlei/test1 master
出现了新的merge master into feature的节点,保证了原有的git log的形式和节点,不会对差异节点进行破坏。
$ git rebase branch_name
把master上的节点以新节点的方式添加到新的分支上,会造成log的不清晰,即commit ID 的不清晰,所以绝对不要在公共协作,版本主干上使用rebase。
撤销操作 git reset / git checkout
git reset用于撤销工作区中的git add / commit 等操作 将工作区内容退回到历史节点。注意 没有确认的提示
$ git reset --hard commit_id #这个id是我不想要的
reset 完以后要推送到远端使用命令:
$ git push -f
git checkout . 用于回退本地所有修改但未提交的文件内容,风险命令 谨慎使用
$ git checkout .
回退某个文件的未提交改动(常用):
$ git checkout -filename
将工具区回退到某个提交版本:
$ git checkout commit_id
与GIT常搭配使用的Linux指令
- 在指定目录下新建一个文件
$ touch temp/d.txt
2.使用命令直接在这个文件中写入
$echo “cccc”>>d.txt