常用流程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| git init
git config user.email "you@example.com" git config user.name "YourName"
vi .gitignore
git add *
git commit -m "test"
git remote add origin https://你的token@github.com/用户名/仓库名.git
git push origin master
|
设置用户信息
1 2 3 4 5 6
| git config --global user.email "you@example.com" git config --global user.name "YourName"
git config user.email git config user.name
|
查看本repo连接的仓库连接
提交文件到暂存区的不同命令
1 2 3 4 5 6
| git add README.md mydir
git add *
git add .
|
查看文件的修改状态
仓库推送的不同方式
可以参考这里
仓库推送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
git push origin master
git branch -vv git push --set-upstream origin master:master
git branch -vv
git push
|
仓库的获取方式
1 2 3 4 5 6
| git clone https://github.com/用户名/仓库名.git
git clone https://github.com/用户名/仓库名.git hellogit
git clone -b hexo https://github.com/用户名/仓库名.git
|
仓库的抓取
将仓库的更新抓取到本地但不合并
1 2 3 4 5 6 7 8 9 10
|
git fetch origin master
git log --all --pretty=oneline --abbrev-commit --graph
git merge origin/master
|
仓库的拉取
抓取到仓库的更新后自动合并
1 2 3 4
|
git pull origin master
|
拉取或抓取后的冲突解决
1 2 3 4 5 6
|
git add . git commit -m "合并后的提交"
|
git设置代理
1 2 3 4 5 6 7 8 9 10 11
| git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'socks5://127.0.0.1:1080'
git config --global http.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0.0.1:1080
git config --global --unset http.proxy git config --global --unset https.proxy
|
git查看配置
1 2 3 4 5 6
| git config --system --list
git config --global --list
git config --local --list
|
查看提交日志
1 2 3 4 5 6 7 8 9
|
git log git log --all --pretty=oneline --abbrev-commit --graph
git reflog
|
查看提交文件列表
1 2 3 4 5 6 7 8 9 10
| git log --name-status
git log --name-only
git log --stat
git whatchanged
git whatchanged --stat
|
git查看最近提交的文件列表
查看提交内容
查看指定commitID下的提交记录
1 2 3 4
| git log --all --pretty=oneline --abbrev-commit --graph
git show commitId
|
直接查看当前HEAD上的最近一次提交
1 2 3 4 5 6 7
| git show
git log -n1 -p
git show -5
|
commit message修改
commit后还没有push
1 2 3 4
| git commit --amend --only
git commit --amend --only -m 'xxxxxxx'
|
commit后已经push
修改commit后再强制推送,不推荐这么做
1 2 3 4 5 6 7
|
git commit --amend --only
git commit --amend --only -m 'xxxxxxx'
git push -f origin master
|
commit用户名邮箱修改
1 2 3 4
| git commit --amend --author "New Authorname <authoremail@mydomain.com>"
|
git版本回退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
git log
git reset --hard commitId
git reset –-soft commitId
git reset --hard HEAD^
git reflog
git reset --hard commitId
git revert HEAD
|
HEAD^n和HEAD~n的区别
Git之版本回退
Git使用revert回滚
分支操作
可用于将工作从主线分离出去,用来修改bug,或者开发新功能
也可以用于不同开发人员开发自己的主线
创建查看分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git branch
git branch dev
git checkout dev
git checkout -b dev
git branch -vv
|
创建空白分支
1 2 3 4 5 6 7 8
| git checkout --orphan newBranchName
git rm -rf .
touch .gitignore git add .gitignore git commit -m 'Init an empty branch'
|
参考
合并分支
1 2 3 4 5 6 7
|
git checkout master
git merge dev
|
删除分支
不能删除当前分支,只能删除其他分支
1 2 3 4 5
| git branch -d dev
git branch -D dev
|
恢复分支
1 2 3 4 5 6
| git log --all --pretty=oneline --abbrev-commit --graph
git branch newBranch commitId
git branch
|
冲突解决
查看这里
删除远程分支
1
| git push origin --delete my-branch
|
rebase
可以用于主线分支更新后,自己基于新的改变继续操作自己的feature开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
git checkout master git pull origin master
git checkout feature
git rebase master
git checkout master git rebase feature git push origin master
|
git的rebase操作
git的tag
可以看做大量commit下的书签,需要在commit后再tag
创建
1 2 3 4 5 6
| git tag v1.4-lw
git tag -a tagName commitId
git tag -a v1.4 -m "my version 1.4"
|
查看tag
1 2 3 4 5 6
| git show tagName
git tag
git ls-remote --tags origin
|
删除tag
1 2 3 4
| git tag -d tagName
git push origin :refs/tags/tagName
|
推送
1 2 3 4 5
|
git push origin :tagName
git push origin --tags
|
检出标签到分支
1
| git checkout -b branchName tagName
|
git中tag的操作
git常识
版本控制的好处
- 代码备份
- 当前版本代码改的很多,已经不知道原来是什么样了,想回到原来的样子
- 协同开发,可用于多人改了同一个文件的情况
- 代码追溯,可以知道哪行代码出了问题
版本控制方式
- SVN
集中式版本控制,只用一台服务器存储代码,工作时下载下来代码,修改后提交到中央版本库。必须要联网
- Git
分布式版本控制,没有中央服务器,每个人的电脑都有完整版本,工作时联网不是必须的,修改后推给对方修改的数据即可,或推给共享版本库。
分支常用规范
- master分支:生产分支,跑这个代码。代码会从develop分支合并过来
- develop分支:开发分支。可以先进入develop分支,然后branch一个feature分支,修改好后merge到develop分支
- feature分支:新特性的分支,用于开发新功能。合并到develop后可以删除该分支
- hotfix分支:用于修复bug的分支。先进入master分支,然后branch一个hotfix分支,修复后merge到master和develop分支
- test分支:用于测试
- pre分支:预上线分支