git 常用问题和命令总结

拉取指定tag的代码

  • 查看仓库所有tag
    git tag ls
  • 切到指定tag
    git checkout tags/<tag_name>
  • 将tag切换到本地指定分支
    git checkout tags/<tag_name> -b <branch_name>

解决error: RPC failed; curl transfer closed with outstanding read data remaining问题

出现这个问题的原因一般是git仓库比较大,或者git clone网速缓慢,导致连接关闭。

解决思路是先进行浅层克隆,然后使用历史记录更新存储库

git clone http://github.com/large-repository --depth 1
cd large-repository
git fetch --unshallow

上面的方式虽然可以拉下代码,但是使用git branch -a只能看到本地master分支和远程master 分支,使用git fetch命令并不能拉取所有远程分支,这是因为配置远程fetch,使用如下命令即可解决这个问题:

//$表示输入的命令,->表示命令行的响应
$ git config --get remote.origin.fetch
-> +refs/heads/*:refs/remotes/origin/*
$ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
$ git config --get remote.origin.fetch
-> +refs/heads/*:refs/remotes/origin/*

git push的时候报the remote end hung up unexpectedly

原因

git push包含大于1m的打包对象时,Smart HTTP协议在post请求的时候使用Transfer-Encoding: chunked,也就是说推的文件里包含大文件。

解决方法

增加repo最大单个文件的缓冲区大小:
git config http.postBuffer 524288000

git远程仓库迁移到新的仓库

  • 先将旧的仓库拉本地
  • 使用如下脚本,将所有分支拉到本地
#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
    git branch --track "${branch##*/}" "$branch"
done
  • 查看所有tag和分支
git tag
git branch -a
  • 移除旧的仓库
git remote rm origin
  • 添加新的仓库
git remote add origin <url to NEW repo>
  • 将所有分支和tags推到远程仓库
git push origin --all
git push --tags

多远程仓库情况下,从某一个仓库拉取某一个tag

git fetch your_remote --tags
git checkout tags/xxx -b xxx

使用git rebase合并多次commit

首先查看git提交历史

git log --oneline

会看到如下数据

91beb50c  [REF] 重构
8d227653 [REF] 重构
26db0f5b [REF] 重构
6fc3d376 [REF] 重构
...

我们需要把这四个提交信息合并为一个commit,使用如下命令:

git rebase -i HEAD~4

表示合并从head往前数四条commit,之后会有如下弹窗

pick 6fc3d376 [REF] 重构
pick 26db0f5b [REF] 重构
pick 8d227653 [REF] 重构
pick 91beb50c [REF] 重构

# Rebase 031ab44d..91beb50c onto 031ab44d (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message

编辑该文件,将后面三个pick改为s,s表示将该commit与前一个commit合并。修改如下:

pick 6fc3d376 [REF] 重构
s 26db0f5b [REF] 重构
s 8d227653 [REF] 重构
s 91beb50c [REF] 重构

# Rebase 031ab44d..91beb50c onto 031ab44d (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message

然后wq保存退出即可。如果有冲突,解决冲突,然后git add .再执行 git rebase --contine提交。没有冲突或者冲突解决,会弹出如下界面,保存即可将commit合并。

# This is the 1st commit message:

[REF] 重构

# This is the commit message #2:

[REF] 重构

# This is the commit message #3:

[REF] 重构

wq保存退出即可合并commit了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • git常用命令 GIT常用命令备忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章阅读 8,667评论 1 26
  • Git常用语法 [TOC] Git简介 描述 ​ Git(读音为/gɪt/。)是一个开源的分布式版本控制系统,...
    君惜丶阅读 3,642评论 0 13
  • 查看、添加、提交、删除、找回,重置修改文件 git help # 显示command的help git sho...
    Swiftor阅读 2,144评论 0 2
  • 本文针对初级用户,从最简单的讲起,但是需要读者对Git的基本用法有所了解。 Git是开源的分布式版本控制系统,...
    Sachie阅读 1,178评论 0 2
  • (预警:因为详细,所以行文有些长,新手边看边操作效果出乎你的预料) 一:Git是什么? Git是目前世界上最先进的...
    axiaochao阅读 1,966评论 1 8