git常见问题

665 阅读5分钟

问题:

git clone url 报错

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

出现以上错误有以下原因

1.缓存区溢出curl的postBuffer的默认值太小,需要增加缓存

使用git命令增大缓存(单位是b,524288000B也就500M左右)

git config --global http.postBuffer 524288000

查看是否生效

git config --list

此时重新克隆即可

2.网络下载速度缓慢

修改下载速度

git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999

3.以上两种方式依旧无法clone下,尝试以浅层clone,然后更新远程库到本地

git clone http://xxx.git --depth=1
git fetch --unshallow

参考连接

改变本地仓库目录后,使用git pull拉报"unable to update local ref"错误。

除了重新clone一份代码外,可以用如下解决方案: 

 1、切换到之前clone代码目录下,执行命令 git gc --prune=now 或者 git gc 

 2、再执行命令 git pull 

3、有问题再执行后再做pull操作 git remote prune origin

git pull报错,error: cannot lock ref导致拉流失败

  1. 使用git命令删除相应refs文件,git update-ref -d refs/remotes/XXX,或者手动删除文件

  2. 简单粗暴强行git pull,执行git pull -p

Sourcetree反复要求输入密码

Mac的Sourcetree在连接的第二个gitlab的时候,反复要求输入密码。即使记住密码,加入钥匙串设置,也一样无法解决这个问题。

终端执行以下命令后,退出钥匙串,退出软件重开即,输入密码时,选择始终允许。

git config --global credential.helper osxkeychain

常用命令:

git status    可以查看自己写了什么东西

git add .     点代表所有文件,放入暂存区中

git commit -m "这里填写本次commit的描述"

git push origin <分支名>    然后将代码push到你的远端分支上(有可能报错,根据错误hint来修改)

git branch #查看本地分支

git branch -r #查看远程分支

git branch -a #查看所有分支

git branch -a | grep name #查找分支

git checkout -b 本地分支 origin/远程分支

git pull origin 远程分支

git branch --set-upstream-to origin/远程分支名  本地分支名

合并分支

git checkout <分支名>    首先切换到你的分支

git pull origin <分支名>    然后将你的远端分支的代码pull下来(因为有可能不止一人在你的分支上修改,所以要保证你的分支上的代码也是最新的)

git checkout main    然后切换到主分支

git merge <分支名>    将你的分支合并到main分支

git push origin main    再将你的main分支的代码修改push到远端的main分支

git checkout <分支名>    最后切换到你的分支继续工作

#删除远端分支
git push origin --delete (branch_name)

#查看本地分支关联的远程分支
git branch -vv

#分支重命名
git branch -m oldName newName

#修改当前项目的用户名和邮箱
git config --local user.name “userName"
git config --local user.email email@example.com

#删除本地版本库上那些失效的远程追踪分支:
git remote prune origin --dry-run




获取指定 分支 或 tag

查看分支:git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/branch_1
remotes/origin/branch_2

查看tag:git tag
tag_1
tag_2

切换到指定branch:
git checkout origin/branch_2

切换到指定tag:
git checkout tag_1

也可以在clone时指定tag或branch:
git clone -b branch_1 https://github.com/abcd/name.git
git clone -b tag_2 https://github.com/abcd/name.git

submodule

子模块使用

# 自动更新所有子模块
git pull --recurse-submodules

# 自动更新所有子模块
git pull
git submodule update --init --recursive

# 在主模块 push 之前,检查子模块是否 push,包括嵌套子模块
git push --recurse-submodules=check
# 在主模块 push 之前,自动 push 子模块,包括嵌套子模块
git push --recurse-submodules=on-demand

# clone含有子模块的仓库,并初始化和更新子模块
git clone --recurse-submodules {https/ssh}

# 添加子模块
git submodule add {http/ssh}

修改提交记录

git rebase -i HEAD~3
git commit --amend
git commit --amend --author="作者<zuozhe@qq.com>"
git commit --amend --message="描述"
git commit --amend --message="描述" --author="作者<zuozhe@qq.com>"

# 修改本地的,服务器的不行
git commit -amend -m "new message"

# 你能在命令行中看到你的历史操作,复制你要恢复操作最前面的hash值
git reflog

# 恢复的历史记录最前面的hash值
git reset --hard 78901234

worktree

# 添加一个 worktree。
git worktree add filePath
# 移除一个 worktree
git worktree remove filePath
# 查看目前所有的 worktree
git worktree list

stash 存储

# 保存当前未commit的代码git stash# 保存当前未commit的代码并添加备注git stash save "备注的内容"# 列出stash的所有记录git stash list# 删除stash的所有记录git stash clear# 应用最近一次的stashgit stash apply
git stash apply stash@{0-n}

# 应用最近一次的stash,随后删除该记录git stash pop
git stash pop stash@{0-n}
# 删除最近的一次stashgit stash drop
git stash drop stash@{0-n}

reset 重置Head

# 重置到上一次,暂存区有保留
git reset --soft HEAD^

# 重置到上一次,暂存区无保留
git reset --hard HEAD^
# 重置到提交,期间的提交都会在暂存区保留
git reset --soft 1a900ac29eba73ce817bf959f82ffcb0bfa38f75

cherry-pick 摘取提交

# 摘取其他分支的提交到当前分支
git cherry-pick 1a900ac29eba73ce817bf959f82ffcb0bfa38f75

# 摘取其他分支的多个提交到当前分支
git cherry-pick commit1 commit2

# 摘取其他分支的连续多个提交到当前分支
git cherry-pick commit1^..commit2

# 冲突解决后继续
git cherry-pick --continue

# 中断摘取,会保留暂存区的内容
git cherry-pick --quit

# 彻底放弃摘取
gits cherry-pick --abort

revert 记录可视化的回退

# 回退提交
git revert 21dcd937fe555f58841b17466a99118deb489212

# 回退合入,-m 1 表示保留主分支代码,防止分不清楚主次
git revert -m 1 21dcd937fe555f58841b17466a99118deb489212
# 查看log
git log

# 查看操作log,便于reset后找回
git reflog

代理

// 命令行设置代理
git config --global https.github.com.proxy socks5://127.0.0.1:7890

// 配置文件设置代理
[https "github.com"]
	proxy = socks5://127.0.0.1:7890

// 全局设置
export all_proxy=socks5://127.0.0.1:7890



//http 和https
添加代理:
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy http://127.0.0.1:1080

取消代理:
git config --global --unset http.proxy 
git config --global --unset https.proxy 

终端

// 开启
export ALL_PROXY=socks5://127.0.0.1:1080
export http_proxy=http://127.0.0.1:1080
export https_proxy=https://127.0.0.1:1080

// 关闭
unset ALL_PROXY
unset http_proxy
unset https_proxy