Git的使用

Git

本文章是记录Git日常使用的问题集合

问题

  • 合并不同的git仓库
问题描述

该需求是在做supermarket项目时,为了管理不同的maven module,不同的module独立使用git管理,但是当需要提交到github时,无法直接使用git push提交到一个仓库,需要在github上建立多个仓库,这不是当初建立module的本意。

此时需要将以前的module git仓库合并到一个新的仓库,并保留git log

解决方法
# 当前branch: master

# 将本地仓库../eclipse/supermarket/ 作为远程仓库 pom
git remote add pom ../eclipse/supermarket/
# 获取远程仓库pom数据 到当前branch FETCH_HEAD,不会修改HEAD
git fetch pom
# 创建并切换branch 并使用指定的commit作为branch start_point(默认使用HEAD)
git checkout -b pom pom/master
# 切换到要合并的branch
git checkout master
# 合并branch
git merge pom

由于是不同的仓库合并,git merge pom可能会提示fatal: refusing to merge unrelated histories,使用下面的命令修复:

git merge --allow-unrelated-histories pom

另外merge很可能产生冲突,注意手动修复

  • 推送本地非master branch到远程仓库
问题描述

当合并目前的supermarket项目到dev branch时,需要push到github上,执行git push出错:没有上游分支。

当前github上并没有创建一个dev分支

$ git push
fatal: The current branch dev has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin dev
解决方法

推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:

$ git remote -v
common  ../eclipse/supermarket/supermarket-common/ (fetch)
common  ../eclipse/supermarket/supermarket-common/ (push)
origin  git@github.com:NavyD/supermarket.git (fetch)
origin  git@github.com:NavyD/supermarket.git (push)
pom ../eclipse/supermarket/ (fetch)
pom ../eclipse/supermarket/ (push)
rest    ../eclipse/supermarket/supermarket-restful-web/ (fetch)
rest    ../eclipse/supermarket/supermarket-restful-web/ (push)

# 推送到远程dev分支
$ git push origin dev 
Enumerating objects: 650, done.
Counting objects: 100% (650/650), done.
Delta compression using up to 4 threads
Compressing objects: 100% (268/268), done.
Writing objects: 100% (650/650), 161.04 KiB | 636.00 KiB/s, done.
Total 650 (delta 258), reused 581 (delta 248)
remote: Resolving deltas: 100% (258/258), done.
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/NavyD/supermarket/pull/new/dev
remote: 
To github.com:NavyD/supermarket.git
 * [new branch]      dev -> dev
  • 删除远程分支
描述

当在feature-mybatis-generator分支上push合并完成后,删除本地分支git branch -d feature-mybatis-generator,但是远程分支未被删除

解决方法

使用命令git push repository --delete branch_name

$ git push origin --delete feature-mybatis-generator

输出:

To github.com:NavyD/supermarket.git
 - [deleted]         feature-mybatis-generator
参考

3.5 Git 分支 - 远程分支

  • 本地仓库添加关联的远程仓库
描述

在本地创建的git仓库,需要关联一个新的github远程仓库,同步备份使用

解决

首先在github上创建一个新的git仓库,名称可以与本地仓库不一致,得到git链接如git@github.com:xxx/repo-name-xxx.git

为本地仓库添加远程仓库链接,在本地仓库运行

$ git remote add origin git@github.com:gxx628/touchGit-remote.git

尝试推送到远程库。如果直接运行git push则会报错fatal: The current branch master has no upstream branch.

$ git push -u origin master 

产生一个错误:

$ git push -u origin master
To github.com:NavyD/useful-annotation.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:NavyD/useful-annotation.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 integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

运行git pull --rebase origin master解决

$ git pull --rebase origin master              
warning: no common commits
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), 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), done.
From github.com:NavyD/useful-annotation
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 忽略不需要的文件
Applying: 修改leetcode相关注解Retention->RUNTIME
Applying: 修改solution runtime memory beats -> beatRate
Applying: 修改了部分属性
Applying: 添加Solution.submissionUrl
Applying: 支持源码打包
Applying: 修改文档submissionUrl

推送远程库

 $ git push -u origin master
Enumerating objects: 64, done.
Counting objects: 100% (64/64), done.
Delta compression using up to 4 threads
Compressing objects: 100% (33/33), done.
Writing objects: 100% (63/63), 7.11 KiB | 1.78 MiB/s, done.
Total 63 (delta 19), reused 0 (delta 0)
remote: Resolving deltas: 100% (19/19), done.
To github.com:NavyD/useful-annotation.git
   a63a3eb..88c9959  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

参考

史上最简单的git教程|第七篇:创建远程仓库、远程仓库关联本地、克隆仓库

Git 基础 - 远程仓库的使用

git push错误failed to push some refs to的解决

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