Git的使用

2019-02-22  本文已影响0人  navyd

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很可能产生冲突,注意手动修复

问题描述

当合并目前的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的解决

上一篇下一篇

猜你喜欢

热点阅读