Git 相关汇总
Created|Updated
|Post Views:
Git 相关汇总
◇ 初次使用及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| scoop install git
git config --list
git config --global user.name "username" git config --global user.email [email protected]
git config --global https.proxy http://127.0.0.1:xxxx git config --global https.proxy https://127.0.0.1:xxxx
git config --global --unset http.proxy git config --global --unset https.proxy
ssh-keygen -t rsa -b 4096 -C "[email protected]"
ssh -T [email protected]
|
◇ 常用命令
* 基础命令
1 2 3 4 5 6 7
| git init
git add . git commit -m "commit message" git push origin master git push origin branch1:branch2 git push -f origin feature-branch
|
* 远程相关
1 2 3 4 5
| git clone git://example.com/myproject
git remote -v git remote add <remote-name> <remote-url> git remote rm <remote-name>
|
* 分支相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git branch git branch -r git branch -a git branch -vv
git branch -m <new-branch-name> git branch -m <old-branch-name> <new-branch-name>
git checkout <branch-name> git checkout -b <new-branch-name>
git checkout -b <local-branch-name> origin/<remote-branch-name>
git branch -d <branch-name> git branch -D <branch-name> git push origin --delete <remote-branch-name>
|
* 元信息相关
1 2 3 4 5 6 7
| git fetch origin
git fetch origin <remote-branch-name>
git branch -dr origin/<remote-branch-name> git fetch -p
|
* 合并相关
1 2 3
| git merge branch1 git pull origin/<remote-branch-name> git pull origin b:a
|
* 子模块相关
1 2 3 4
| git submodule add -b <remote-branch> https://xxx.git /<path>
git clone --recursive https://github.com/example/example.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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| git tag v1.0.0
git tag v1.0.8 ba9f9e
git push origin v1.0.8
git push origin --tags
git tag
git tag -l "v1.0*"
git show v1.0.8
git tag -d v1.0.9
git push origin :refs/tags/v1.0.9
git push origin --delete v1.0.9
git fetch --prune --prune-tags
git tag v2.0.0 -m "version 2.0.0 released"
git tag v2.0.0 -m "version 2.0.0 released" -m "rebuild with react hooks" -m "support typescript"
git tag -n3
git tag v1.0.0 v1.0.0^{} -f -m "first commit"
git push origin -f v1.0.0
|
◇ 其它情景
▷ GitHub历史版本回退
1. 查找历史版本
网站操作:
浏览 GitHub 上的提交历史记录,找到要回退的版本,复制 commit id。
本地操作:
使用git log
命令查看所有的历史版本,获取 git 的某个历史版本的 id。
id 格式如下:
1
| fae6966548e3ae76cfa7f38a461c438cf75ba965
|
2. 恢复到历史版本
1
| git reset --hard fae6966548e3ae76cfa7f38a461c438cf75ba965
|
3. 强制推到远程服务器
1
| git push -f origin master
|
▷ 克隆或打开默认分支以外的分支
1. 克隆远程仓库
1 2
| git clone git://example.com/myproject cd myproject
|
2. 查看分支
1 2 3 4 5 6 7 8 9 10 11
| $ git branch * master
$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/develop remotes/origin/feature remotes/origin/feature-im remotes/origin/master remotes/origin/newbranch
|
如果没有远程信息或信息已过时,可执行以下命令更新远程分支元信息:
如果分支数过多,建议直接指定需要的分支,避免无关信息占用本地空间:
1
| git fetch origin feature
|
3. 切换分支
有 4 种方法:
1 2 3 4 5 6 7 8 9 10 11 12
| git checkout origin/feature
git checkout -b feature origin/feature
git checkout -t origin/feature
git fetch origin feature:feature git checkout feature
|
切换后再次查看:
1 2 3
| $ git branch master * feature
|
▷ git 开发项目流程
◇ 参考内容
- GitHub历史版本回退. https://blog.csdn.net/u013201439/article/details/80140863
- Github使用之git回退到某个历史版本. https://blog.csdn.net/yxys01/article/details/78454315
- 怎么用git clone 远程的所有分支. https://www.jianshu.com/p/0fe715a7fbb3
- git 设置和取消代理. https://gist.github.com/laispace/666dd7b27e9116faece6
- 如何重命名 Git 分支. https://www.freecodecamp.org/chinese/news/git-rename-branch-how-to-change-a-local-branch-name/
- git clone 含有子模块的项目. https://www.cnblogs.com/codingbit/p/git-clone-with-submodule.html
- 使用 git tag 给项目打标签. https://segmentfault.com/a/1190000040523583