git, how to use git

Git的工作流程(借鉴bilibili上的图,推荐视频:https://www.bilibili.com/video/BV1r3411F7kn?vd_source=2feb8409e6cd627f97f4bbcb14e4b1c3):

0.upstream与origin

公司里实际code管理时,一般是upstream是所有员工的公共仓库,这里有最新的。每个人fork一个自己的仓库,就是origin, 然后git clone到本地工作区。

如果origin/main分支与upstream/main分支不一致,解决办法:

强制覆盖本地(在本地已经没有要保存的,或者git stash之后)

$ git fetch upstream
$ git reset --hard upstream/main

这时候,直接把本地的(现在已经与upstream一致)git push到origin,大概率会出现(rejected, errot),因为origin可能有些更改,不在local;

这时候需要把origin上的分支,拉到本地,有冲突解决冲突

$ git fetch origin
$ git merge origin/main
// slove conflict
$ git push origin main

整个流程相当于把upstream上的分支和origin上的分支分别拉到本地,然后合并到你想要的code,然后把本地的更新到origin上。

对于本地来说,upstream是远程仓库,origin也是远程仓库。

一般工作中,因为origin仓库是从upstream仓库fork而来,会用网页版的gitlab/github中merge request把origin中的code, 提交申请合并到所有员工共享的upstream上,就把你的工作成果上传了。

1.自己从原仓库yyy, fork了一个自己的仓库xxx;

把fork的原仓库作为upstream;

$ git remote add upstream git@gitlab.icelab.yyy.git

把自己的本地master_***分支关联到yyy;

$ git remote -v
origin  git@gitlab.icelab.xxx.git (fetch)
origin  git@gitlab.icelab.xxx.git (push)
upstream        git@gitlab.icelab.yyy.git (fetch)
upstream        git@gitlab.icelab.yyy.git (push)
$ git branch --set-upstream-to=upstream/master master_***

2.force write local; here pull from xxx, origin = xxx, upstream = yyy;

$ git fetch --all
$ git reset --hard origin/master 
$ git pull

3.create new branch.

$ git checkout upstream/*** -b my_***

4.你fork的仓库xxx, 并不会自动和原仓库yyy同步,若需同步,需要下面两个步骤;

4.1.把原仓库pull到本地,例如pull到本地的master分支

$ git pull upstream master
or you pull any branch you want 
$ git fetch upstream
$ git merge upstream/***
//*** is the name of your branch

4.2.把本地仓库推送到fork的仓库xxx

$ git push origin ***
//*** is master or the name of your branch in 4.1.
//all operation, you know upstream is yyy, origin is xxx.

5.fork的origin仓库与upstream差距太大,会提交失败;

the fork’s master branch should occasionally be kept up to sync with the master branch of the main repo. This can be done by running the following commands on the fork repo:

git checkout master
git fetch upstream
git pull upstream master
git push origin master

6.当本地分支与远程库有冲突时

//一般先git stash save "xxx"保存本地的修改;
$ git reset HEAD^
$ git stash apply
//解决冲突,重新提交git add, git commit;
//结合下面这两个命令看冲突的地方;
$ git status
% git reflog

7.打patch

git diff HEAD~10 fm/.../***.cpp > foo.patch

git diff upstream/main origin/main > 2024052902fm.patch  // 把本地的变动与远程对比;

8.git log --author devname fm/plugins/imm/fmp_pldm.cpp

9.git revert:  想要revert那一次的commit id:

$ git revert fdfdfdsfdsf1e1da4b06fc32ae6a8a47287d2d

这个相当于提交了一次,git commit.
需要一个命令,把commit的内容, push到远端,$ git push origin pen-test

10.基于tag, 创建分支。

$ git checkout -b xxx/ryyyy-tag

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值