git常见问题

179 阅读4分钟

git常见问题

问题描述:使用 文件提交到远程仓库后,后边有进行了其他的提交点,发现之前的提交点有多余文件,这时可以使用git checkout 命令来解决,具体使用方法如下:

首先使用 git log 查看提交日志。
然后使用 git checkout "commitId" -- "文件路径"

这样就让文件从提交状态转为未提交状态。

问题描述:如果想要修改之前的提交点的文件内容,想要回到之前的提交点进行提交。

可以使用 git rebase 命令

git rebase -i HEAD~2 

i的意思是:interactive,HEAD2为在历史的前两个提交,同理,HEAD4就是历史的前四个提交。

这样就回到之前的提交点,可以对文件进行add、rm 等操作,操作完成后使用:

git commit --amend                      # 将修改提交到本地仓库
git push origin HEAD:refs/for/分支名     # 将commit 提交到远程仓库
git rebase --continue                   # 提交完成后,没有其他操作就回到最初的提交点

问题描述:想要拉取远程仓库的文件

首先使用 
git stash save "保存内容"  # 使用该命令把自己最新写的代码暂存起来。
然后使用 
git pull --rebase        # 拉取文件,若有冲突,则解决冲突。
冲突解决完后 使用 
git stash pop            # 命令把暂存起来的代码释放。

问题描述:创建新的提交点提交文件

提交之前首先将最新开发的代码暂存起来
git stash save "开发内容"
然后拉取远程仓库的文件(因为当你在开发的时候,别人开发的代码可能已经合并了,所以要先拉取远程仓库的代码)
git pull --rebase
如果产生了冲突,就解决冲突,解决完成后,使用
git stash pop 释放暂存的代码
git status 查看文件状态
git add "添加文件名" 想要提交的文件就add
git restore .  不想添加的就restore
如果创建新的提交点,则
git commit -m "提交内容"
最后使用
git push origin HEAD:refs/for/分支名

问题描述:在当前的分支继续提交文件

首先
git status 查看文件状态
想要 add 的就添加上,想要restore 的就去掉
然后使用
git commit --amend 命令,会弹出一个编辑页面,想要修改commit -m "" 的内容就可以在这里修改.
最后
git push origin HEAD:refs/for/分支名

解决报错:Failed to connect to github.com port 443 after 21073 ms

出现fatal: unable to access ‘XXX‘: Recv failure: Connection was reset 错误解决方法

原因: 这样的问题往往是由于网络慢访问超时,这时候我们可以在终端选择使用设置代理和取消代理的命令解决。

设置代理:
git config --global https.proxy

取消代理:
git config --global --unset https.proxy
git config --global --add safe.directory D:/www/your-project

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

记一个git checkout问题error: invalid path

git config core.protectNTFS false
git config --global credential.helper 'cache --timeout=3600'

删除全局git

git config --global --unset user.name
git config --global --unset user.email

Git 连接问题:fatal: unable to access ‘github.com/...../‘: Failed to connect to git

git config --global --unset http.proxy

git config --global --unset https.proxy

git 子仓库

主仓库中添加子仓库
git submodule add <url> <path>
更新子代码代码
git submodule update --init
克隆含有子仓库的仓库
git clone --recurse-submodules <url>

主仓库中删除子仓库

1、进入包含子仓库的父仓库的根目录
2、使用以下命令将子仓库从父仓库中移除(解除关联)
git submodule deinit <submodule_path>、
3、使用以下命令从 Git 仓库中删除子仓库的记录
git rm <submodule_path>
4、执行 git commit 来提交父仓库的修改
git commit -m "Remove submodule <submodule_path>"
5、最后,删除实际的子仓库文件。你可以手动删除相关的子目录,或者使用以下命令删除子仓库目录:
rm -rf <submodule_path>

子仓库 push 提交到 gerrit 出现没有 change-id ?

由于子仓库在 clone 后,仓库的 .git 目录中,没有 hook/commit-msg 工具 所以在生成的 commit message 中,不会自动增加change-id 这会导致 commit 无法 pushgerrit上走 review 的流程

请使用以下方法增加 commit-msg tool 到子仓库的 .git 目录中:

cd `awk '{printf $2}' .git`/hooks
wget http://192.168.0.202:8002/tools/hooks/commit-msg
chmod +x commit-msg
cd -

对于没有 change-idcommit 可以使用 git commit --amend 或者 git rebase -i 重新增加 change-id 有了 change-id 后即可以顺利 pushgerrit 并走 review 流程了

拉取子仓库时,避免重复输入用户名和密码

git config --global http.postBuffer 524288000

想要删除之前的提交点,使用

git rebase -i HEAD~第几个(想要删除第三个,就是3,从1开始数),回车确认后使用编辑器,把想要删除的提交点改成drop,保存退出就成功删除了