一直用SourceTree习惯了,上次提交出了点问题。被同事用命令行解决了,突然感觉命令行还是很好用的,研究下。
1 简介
免费的版本控制工具,多的就不用多说了。
1.1 基本结构
- 工作区
- 暂存区
- 版本库
- 远程库
2 操作
2.1 暂存区
git add 将文件添加到暂存区.
添加多个文件
git add [file1][file2]
添加文件夹
git add [dir]
添加所有文件
git add .
2.1 代码提交
提交暂存区到本地仓库
git commit -m [message]
提交指定文件到本地仓库
git commit [file1][file2] -m [message]
提交上次commit 之后的变化到本地仓库
git commit -a
2.1 分支
列出所有本地分支
git branch
列出所有远程分支
git branch -r
列出所有分支(本地和远程)
git branch -a
新建一个分支(并留在当前分支)
git branch [branch-name]
新建一个分支,并且切换到新分支
git checkout -b [branch-name]
新建一个分支,与指定的远程分支建立关系
git branch --track[branch-name][remote-branch]
切换到指定分支,并且更新工作区
git checkout [branch-name]
切换到上一个分支
git checkout -
合并指定分支到当前分支
git merge [branch-name]
选择一个commit。合并进当前分支
git cherry-pick [commit]
删除分支
git branch -d [branch-name]
删除远端分支-- 需要权限
git push orgin --delete [branch-name]
git branch -dr [rremote/ branch]
2.3 日志
查看有变更的文件
git status
查看当前分支的历史版本
git log
搜索提交历史
git log -s [keyword]
显示过去的五次提交
git log -5 --pretty --oneline
显示暂存区和工作区的差异
git diff
显示今天写了多少代码
git diff --shortstat "@{0 day ago}"
显示最近的几次提交
git reflog
2.4 远程同步
下载远程仓库的所有变动
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
2.5 撤销
恢复暂存区的指定文件到工作区
git checkout [file]
恢复某个commit 的指定文件到工作区
git chrckout [commit][file]
恢复暂存区的所有文件到工作区
git checkout .
重置暂存区的指定文件,与上次commit的保持一致
git reset [file]
重置暂存区与工作区,与上次commit保持一致
git reset --hard
重置当前分支的指针为指定commit,同时重置暂存区,但是工作区保持不变
git reset --hard[commit]
重置当前分支的head为指定commmit,同时重置暂存区和工作区,与指定commit一致
git reset --keep [commit]
新建一个commit,但是保持暂存区和工作区不变
git revert commit[commit]