工作中使用的一些 git 小技巧
合并冲突
git stash
git stash
git pull
git stash pop
// 修改冲突
git push
git add 后如何撤销
git status // 先看一下add 中的文件
git reset HEAD // 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了
git reset HEAD <file>... // 就是对某个文件进行撤销了
git commit 撤销
git reset --soft HEAD^
git reset --soft commit_id
// HEAD^的意思是上一个版本,也可以写成HEAD~1
// 如果你进行了2次commit,想都撤回,可以使用HEAD~2
reset 后的参数
1、–mixed: 不删除工作空间改动代码,撤销 commit,并且撤销 git add . 操作
2、–soft 不删除工作空间改动代码,撤销 commit,不撤销 git add .
3、 –hard 删除工作空间改动代码,撤销 commit,撤销 git add .,完成这个操作后,就恢复到了上一次的 commit 状态。
修改 commit 的注释
git commit –amend
新建分支并推送到远程仓库
git checkout -b branchDemo
git push origin branchDemo
仓库添加新的提交地址
// 拉取所有的分支
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
git remote -v
// 1、直接更改原仓库的地址
git remote set-url origin 新的仓库地址
git push --all // 提交所有的分支
// 2、添加多个远程仓库
git remote add backup 新的仓库地址
git pull backup
git push backup
// 3、 不更改原来的仓库地址,在 origin 后追加一个仓库地址
git remote set-url --add origin 新的仓库地址
取消本地目录下关联的远程库
git remote remove backup
github 上代码下载很慢
// 拉远程仓库某分支最近一次的commit
git clone -b ${branch} ${remotreurl} --depth=1
// git config 和 小飞机的端口号配置一致后,开启代理
http.proxy = 127.0.0.1:1081
git config --global http.proxy 127.0.0.1:1081
git config --global --unset http.proxy
// 设置 git http代理
git config --global http.https://github.com.proxy http://127.0.0.1:1081
git config --global https.https://github.com.proxy https://127.0.0.1:1081