本文授权自 MagicBoy
1. pull request 同步原仓库
-
创建分支:
git branch test/update-readme
-
切换分支:
git checkout test/update-readme
前两行代码可合并为 git checkout -b test/update-readme
- 查看远程分支状态:
git remote -v
origin git@github.com:dengjiawen8955/pr-test.git (fetch)
origin git@github.com:dengjiawen8955/pr-test.git (push)
-
添加一个新的upstream 分支 指向「原仓库」地址:
git remote add upstream https://github.com/bmft-team/pr-test.git
-
确认是否添加成功:
git remote -v
origin git@github.com:dengjiawen8955/pr-test.git (fetch)
origin git@github.com:dengjiawen8955/pr-test.git (push)
upstream https://github.com/bmft-team/pr-test.git (fetch)
upstream https://github.com/bmft-team/pr-test.git (push)
- 从「原仓库」拉取最新的代码:
git fetch upstream
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.33 KiB | 47.00 KiB/s, done.
From https://github.com/bmft-team/pr-test
* [new branch] master -> upstream/master
- 查看自己所在的分支:
git branch
master
* test/update-readme
- 合并 upstream 分支到 master 分支:
git merge upstream/master
Updating e2b016c..774d228
Fast-forward
API.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 API.md
- 将 master 分支合并到 test/update-readme 分支(此时你的分支为test/update-readme):
git merge master
6 7 8 9 步骤可以合并为一步 git pull upstream upstream的x分支
含义: 将远程upstream仓库的x分支合并到当前分支
- 提交代码
git add .
git commit -m "update README.md and sync the source repository"
git push origin test/update-readme
ps:
git branch -a //查看本地分支及远程分支
git branch -D featuer //删除本地 featuer 分支
2. git 配置全局设置
配置http代理
git config --global http.proxy 127.0.0.1:7890
git config --global https.proxy 127.0.0.1:7890
取消配置http代理
git config --global --unset http.proxy
git config --global --unset https.proxy
配置socks5代理
git config --global http.proxy socks5 127.0.0.1:7890
git config --global https.proxy socks5 127.0.0.1:7890
3. 更改 github 账号后 push 出现的问题
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 16 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 301.45 KiB | 30.14 MiB/s, done.
Total 9 (delta 1), reused 1 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Thunderinmoonlight/diSE.git
! [remote rejected] main -> main (permission denied)
error: failed to push some refs to 'https://github.com/Thunderinmoonlight/diSE.git'
切换账号信息
git config --global user.name "xxx" //切换用户名
git config --global user.email "xxx" //切换邮箱
git config user.name //查看用户名
配置凭据以使用完整的存储库路径(而不是仅使用默认设置-域)
git config --global credential.useHttpPath true
之后出现的弹窗按要求操作即可完成push
4. git 回滚操作
git log //查看提交历史,找出要回滚到的commit-id
git reset --hard commit-id
或者 git reset --hard HEAD~1 //将最近1次的提交回滚
--hard 清空所有修改,删除本地数据
--soft 将之前提交的内容恢复到暂存区,不会修改本地文件
--mixed 将之前提交的内容恢复到未暂存状态,不会修改本地文件 (默认)
此时本地代码落后于远程代码,如果需要将回滚后的本地代码Push到远程代码仓,需要使用–force以强制Push到远程代码仓
git push --force
其他的用到再做笔记
参考文章:
https://blog.youkuaiyun.com/jarvan5/article/details/111387361
https://blog.youkuaiyun.com/QH_JAVA/article/details/77969010
https://blog.youkuaiyun.com/qq_34821328/article/details/115126422
如有侵权,请联系作者删除