git things - HeavyYuan/A-CD-Record-Management-System GitHub Wiki

分支合并

以下场景都是项目真实场景,为了说清楚问题,以下内容通过构造这些场景来描述这些场景。 场景一:(无冲突场景)

  1. 在本地克隆master分支,git clone ...

  2. 在github网页上增加新文件testing1(或删除文件)

  3. 在本地修改任意文件,然后commit

  4. 在本地git push推到远端,推送失败。

报错信息:

To https://github.com/HeavyYuan/firstgit.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/HeavyYuan/firstgit.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

2个解决办法:

  1. git pull

  2. git fetch + git merge

git merge 这里的commit是本地缺失的远端的commit id(多条缺失,就跟多个commit id)

添加--no-commit参数可以控制本次merge不产生commit log,但是在执行git merge后需要手动commit一次当前的变更(git status查看)

git分支(branch)管理

git branch testb1 创建新分支testb1(只创建,并不switch到新分支)

git checkout -b testb2 创建新分支testb2(创建新分支,并switch到新分支)

git push origin testb1 将新分支testb1更新到远端(即在远端也见一个同名的分支)

git branch -u origin/testb1 关联本地和远端的testb1(-u <upstream> 等同 set-upstream-to=<upstream>),该动作在git checkout testb1后执行

这里的upstream = <remote>/<branch>

不关联:git pull 不能运行,因为其不知道和哪个远端分支来比较,原理上,git push也不能运行,但是实际操作中,git push的运行符合预期(待查)

不做关联时,push方法:git push origin testb1

Git: Upstream Tracking Understanding

https://mincong.io/2018/05/02/git-upstream-tracking/#git-configuration

⚠️ **GitHub.com Fallback** ⚠️