The open source community must know the knowledge points——git submit pr

The open source community will

1 Fork the warehouse and submit it to the open source community after submitting the pr

1.1 fork open source warehouse

①Log in to github, find open source warehouse A, and click fork
insert image description here

In this way, a warehouse B with the same name will be created under your own github account (the warehouse name can be modified)

② Then modify locally and submit to the B warehouse under your github account

1.2 Merge multiple local commits into one

Generally speaking, open source communities submit PRs that need to be merged into one commit

  1. For example, I now have 4 commits:
85d5d8fa468b06bb9a62fafde01d80cbb7396682 # 我改的

621ca4121f971d9604e395556763551427d799d9 # 我改的

f744d2e91916ab7831f3a7695d1d1825916db164 # 我改的

5c135e49e683563fa470d7f5c281050ec1d73af9 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的
  1. I now want to merge my commits into one
8403afe13664d6bb7f5a5557716a030e9389a944 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的
  1. Specific operation method

1.2.1 Method 1: Merge commitID

First roll back the content from the version library to the temporary storage area, and then resubmit the content of the work area

  • Idea: Use git reset --soft to roll back the versions of the version library and the temporary storage area, while retaining the changes in the work area, and then resubmit the contents of the work area.
# 查看前10个commit【找到别人最后一次提交的位置】
git log -10
# 从版本库恢复文件到暂存区,不改动工作区的内容
git reset --soft 295ac3b842b4ecb6eff1c9954a281a4606a8bc84	# 别人改的commitID
# add已经跟踪的文件
git add -u
# 提交
git commit -m "修改信息"
# 强制push以替换远程仓的commitID
git push --force

If the push fails and Reject appears, you need to enable the option to force the branch to be merged and cancel the branch protection.

  • Settings -> Repository -> Protected Branches -> Protected branch (find the branch) -> Unprotect

1.2.2 Method 2: git rebase

# 查看前10个commit
git log -10
# 将4个commit压缩成一个commit
git rebase -i HEAD~4	
# add已经跟踪的文件
git add -u
# 提交
git commit -m "修改信息"
# 强制push以替换远程仓的commitID
git push --force

Note: A progress git rebasewill be 临时created 新分支. If you make a mistake, you can git checkout the original branch name to switch back to the original branch and then re-git rebase.

Specific demonstration of git rebase:

I want to merge the first four commits into the last one. As follows:

①git log View historical submission information
insert image description here
②git rebase -i HEAD~n

Use git rebase -i HEAD~5 to compress 5 commits into 1, or git rebase -i 51efaef517abdbf674478de6073c12239d78a56a (the id of the first commit)

Vim editor, press i to edit, change the pick of the last 4 commits to fixup, and keep the first pick. Press the esc key, enter: wq to save and exit.

  • pick: use commit.

  • reword: Use commit to modify the commit information.

  • squash: Use commit to merge the commit information into the previous commit.

  • fixup: use commit, discard commit information.
    insert image description here
    After the operation, it was found that the commits were merged into one.
    insert image description here
    ③git push --force submit
    insert image description here

1.3 submit pr

A: represents the warehouse of the open source community, B: represents the warehouse that you forked

  1. Create a pull request in the B warehouse that you forked down:
    insert image description here
  2. fill in pr information
    insert image description here
  3. Fill in the comment information
    insert image description here

2 Synchronize your own fork warehouse

  1. Clone the fork project into the local warehouse
  2. Configuring a remote for a fork
  3. Syncing a fork
# 1. git clone 自己fork的仓库(自己的仓库B)
git clone 自己仓库的地址(B)
# 2. 查看仓库的远程状态
git remote -v 
# 3. 添加仓库的上游
git remote add upstream 开源仓库的git地址(A)
# 4. 查看是否配置成功
git remote -v
# 5. 同步开源社区的修改
## 从上游拉取最新代码
git fetch upstream
## 切换到本地主分支
git checkout master
## 将从上游拉取的代码合并到本地
git merge upstream/master
# 6. 同步到自己fork的仓库B
git push origin master

Renderings:
insert image description here

3 Realize pr through goland

Reference:
https://blog.csdn.net/Spade_/article/details/108698036
https://blog.csdn.net/u012435142/article/details/89491388
https://www.jianshu.com/p/021bb953ee8d
https ://blog.csdn.net/xiaobinqt/article/details/116277126

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/131378343