Git(1)---git fork 在master上的简单尝试,暂未涉及分支

1. 使用git fork一个远程A的代码仓库的副本到自己的远程仓库

在gitlab或者github上面有fork操作按钮

2. 拉取自己的远程仓库到自己的本地

$ git clone https://github.com/xxx/yyy.git
Cloning into 'dingding'...
remote: Counting objects: 52, done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 52 (delta 4), reused 52 (delta 4), pack-reused 0
Receiving objects: 100% (52/52), 1.20 MiB | 40.00 KiB/s, done.
Resolving deltas: 100% (4/4), done.

我第一次clone代码使用的地址是https所以每次push代码都需要输入用户名和密码验证身份 如下:

$ git push origin master
Username for 'https://github.com': xxx@163.com
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 561 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/xxx/yyy.git
   9623903..ca025de  master -> master

为了方便 现在把拉取远程代码的仓库地址换成ssh的

$ git remote set-url git@github.com:xxx/yyy.git  // 命令错误
usage: git remote set-url [--push] <name> <newurl> [<oldurl>]
   or: git remote set-url --add <name> <newurl>
   or: git remote set-url --delete <name> <url>

    --push                manipulate push URLs
    --add                 add URL
    --delete              delete URLs


$  git remote set-url --push origin git@github.com:xxx/yyy.git

3. 修改后提交到自己的远程仓库

添加本地修改到暂存区

$ git add --all
warning: LF will be replaced by CRLF in .idea/yyy.iml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in .idea/modules.xml.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in .idea/workspace.xml.
The file will have its original line endings in your working directory.

提交本地修改到本地分支

$ git commit -m "conflict fixed"
[master 26c2de4] conflict fixed
 4 files changed, 283 insertions(+), 1 deletion(-)
 create mode 100644 .idea/datura_lj.iml
 create mode 100644 .idea/modules.xml
 create mode 100644 .idea/workspace.xml

查看代码状态

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

没有问题后,把本地代码更新到远程仓库

$ git push origin master
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (16/16), 4.58 KiB | 0 bytes/s, done.
Total 16 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 1 local object.
To github.com:xxx/yyy.git
   ca025de..5ae59c1  master -> master

拉取A的远程代码

// 先查看下远程代码仓库

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/xxx/yyy.git
  Push  URL: https://github.com/xxx/yyy.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

// 添加上游代码仓库

$ git remote add upstream https://github.com/aaa/yyy.git

// 或者使用git remote -v查看远程代码仓库,现在已经添加成功

$ git remote -v
origin  https://github.com/xxx/yyy.git (fetch)
origin  https://github.com/xxx/yyy.git (push)
upstream        https://github.com/Datura900607/datura_lj.git (fetch)
upstream        https://github.com/Datura900607/datura_lj.git (push)

// 拉取上游代码仓库的代码

$ git pull upstream master
From https://github.com/aaa/yyy
 * branch            master     -> FETCH_HEAD
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

xxx@xxx MINGW64 /e/yyy(master|MERGING)

查看代码状态

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   readme.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .idea/workspace.xml

no changes added to commit (use "git add" and/or "git commit -a")

保存修改的本地代码到缓存区,commit本地代码要写备注

$ git add --all
warning: LF will be replaced by CRLF in .idea/workspace.xml.
The file will have its original line endings in your working directory.

$ git commit -m "fixed form fork"
[master 5ae59c1] fixed form fork

把本地最新代码同步到自己的远程仓库

4. 发送pull request将自己修改的代码合并到A的远程代码仓库

在gitlab或者github上发起pull request 或者new merge request 等待A仓库的管理员进行merge

5. B从A fork一个远程A的代码仓库的副本到B的远程仓库,修改代码后push到B的远程仓库

6. 当我需要同步B的最新代码,拉取B的远程代码到自己的本地

git 命令窗清屏

$ clear

添加B的远程代码仓库,并重命名bbb

$ git remote add bbb https://github.com/bbb/yyy.git

xxx@xxx MINGW64 /e/yyy(master)
// 查看已经 添加成功
$ git remote -v
origin  https://github.com/xxx/yyy.git (fetch)
origin  git@github.com:xxx/yyy.git (push)
upstream        https://github.com/aaa/yyy.git (fetch)
upstream        https://github.com/aaa/yyy.git (push)
bbb https://github.com/bbb/yyy.git (fetch)
bbb https://github.com/bbb/yyy.git (push)

从B的远程拉取最新代码 git fetch , git merge

$ git fetch bbb master
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 2), reused 6 (delta 2), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/bbb/yyy
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> bbb/master
// 查看修改的地方

$ git diff
diff --git a/readme.txt b/readme.txt
index 80aafc0..3229bc6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,3 @@
 Git is a distributed version control system.  => Git<E6><98><AF><E4><B8><80>
<E4><B8><AA><E5><88><86><E5><B8><83><E5><BC><8F><E7><89><88><E6><9C><AC><E6><8E>                                                                                                                                                                                               <A7><E5><88><B6><E7><B3><BB><E7><BB><9F>
 Git is free software distributed under the GPL.
-console.log('changed by xxx')
\ No newline at end of file
+# console.log('changed by xxx')
\ No newline at end of file

// git fetch 并没有merge

$ git merge
Already up-to-date.

直接使用git pull

$ git pull bbb master
From https://github.com/bbb/yyy
 * branch            master     -> FETCH_HEAD
Auto-merging readme.txt
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.
error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.

不添加B的远程代码仓库,直接拉取

$ git pull https://github.com/bbb/yyy.git master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 4 (delta 1), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/xmc123/datura_lj
 * branch            master     -> FETCH_HEAD
Auto-merging readme.txt
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

7. 关于重置ssh-key

重置ssh-key

$ ssh-keygen -t rsa -C "your github email@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa):
/c/Users/xxx/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/xxx/.ssh/id_rsa.
Your public key has been saved in /c/Users/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Qg09NpPxBdtL3/n8t4za/ssqgf0mhpPSGePUxIy4ZCs special_yjl@163.com
The key's randomart image is:
+---[RSA 2048]----+
|      ...o...    |
|       oB. +     |
|      ..o+* o    |
|     . + . = o ..|
|      + S = . ...|
|     E + = +   ..|
|      . + * o   o|
|       . B +.o+ o|
|        . o.*=o*=|
+----[SHA256]-----+

8. 我还会回来的......

    [后续--git fork 涉及分支](http://www.jianshu.com/p/7ec8e2e5b3dc)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,387评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,845评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,091评论 0 246
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,308评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,662评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,795评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,008评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,743评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,466评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,687评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,181评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,531评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,177评论 3 239
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,126评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,902评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,862评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,734评论 2 274

推荐阅读更多精彩内容

  • 多种多样的工作流使得在项目中实施Git时变得难以选择。这份教程提供了一个出发点,调查企业团队最常见的Git工作流。...
    JSErik阅读 4,311评论 2 8
  • 能不能好好说话?不能的时候是因为内心先产生了不舒服的感受,这种感受的由来是别人撞击了你内在的价值观。...
    舒骅阅读 308评论 0 0
  • 有些故事有些人注定孤独,孤独到故事的结局,孤独到死后,唯希望孤独的魂魄来世可以被人好好疼爱,不再凄凉
    困了的野猫阅读 96评论 0 0
  • 没错,是我没有忍住。满街都是情侣的日子里,躲过了四下无人的街道,也躲不过自己的内心,偌大的一个北京,却没有你的身影...
    托塔啊李天王阅读 365评论 0 0