git常用指令及bug解决(更新自用)

目录

一、基本指令

1、下载指定分支内容

bash 复制代码
git clone -b branchname https://git.***.com/your/path/***.git

2、分支相关

(1)查看目前所在分支

bash 复制代码
git branch

(2)查看远程所有分支及状态

bash 复制代码
git branch -a

(3)切换分支

bash 复制代码
git checkout branch_name

(4)删除远程分支

一般删除远程分支同时也要删除本地分支,分别运行一下命令:

bash 复制代码
git branch -d branchname

3、在指定分支上提交更改后的最新代码

(1)首先查看本低与远程代码状态,是否有修改

bash 复制代码
git status

如果没有任何改变,则会看到

bash 复制代码
nothing to commit, working directory clean

如果有改变建议先查看一下本地代码和远程有哪些差异,使用difftool工具:

bash 复制代码
git difftool

挨个打开查看与远程库中的差异,右侧是本低自己的代码,左侧是远端代码,更改后保存关闭即可。

(2)确认所有代码修改的部分并保存后,开始提交,首先确认索要提交到的分支,按照(1)中命令查看或者切换到想要到的分支,比如我想提交到master分支,则分别执行如下代码:

bash 复制代码
git branch //查看当前所在分支是不是自己想要提交的
git checkout master //如果不是,切换到所需的分支

(3)添加所要提交的文件到缓存

bash 复制代码
git add . //添加所有文件
git add filename.*** //添加部分文件
git add flodername//添加整个文件夹

(4)写提交的信息,就是本次提交修改了什么或者原因,类似于备注

bash 复制代码
git commit -m "备注信息"

(5)提交缓存中所有的代码和备注信息到远程仓库

bash 复制代码
git push origin yourbranchname

这里面origin意思是推送本低代码到远程端同名的分支下,如果远程端不存在同名分支则会自动创建这个分支,所以一定要确认你当前所在的文件夹是在想要提交的分支下,而且origin后面的分支名要写对,否则会导致远端新建分支,弄乱git库。

4、git对比两份代码的差异

(1)对比本地代码和对应的远程代码差异

bash 复制代码
git difftool

使用这个工具需要安装一下相关的库

(2)对比两个不同仓库代码的差异

首先,将两个仓库repo1,repo2都克隆到本地电脑上(两个文件夹,可以为不同路径),可以使用git clone命令(地址去github clone里面粘贴):

bash 复制代码
git clone -b branchA https://git.***.com/your/path/***.git
git clone -b branchB https://git.***.com/your/path/***.git

然后,进入其中一个仓库的目录,使用git remote -v可以看到对应的origin拉区地址。

bash 复制代码
origin	https://github.com/***.git (fetch)
origin	https://github.com/***.git (push)

使用git remote add命令将另一个仓库添加为远程仓库(只不过这个远程仓库并不是github上的地址,而是另一个仓库所在的本地文件夹的路径)。

bash 复制代码
cd /path/to/repo1
git remote add repo2 /path/to/repo2

这个repo2 可以是原本仓库的名字,也可以自己另外起一个名字,只是用来对比,对比结束可以删除。

添加完成后,利用git remote -v可以发现,原本只有本文件夹下程序的远程地址,现在添加上了另一个库文件夹的地址。

bash 复制代码
origin	https://github.com/***.git (fetch)
origin	https://github.com/***.git (push)
repo2	/path/to/filename (fetch)
repo2	/path/to/filename (push)

再使用git fetch命令从远程仓库中获取最新的提交信息。

bash 复制代码
git fetch repo2

这时可以看到更新后的信息和对应的分支信息,能够看到新加入的branch为: * [new branch] your_repostory_btanch_name -> repo2/your_repostory_btanch_name

最后,使用git diff命令来比较两个仓库之间的差异。

bash 复制代码
git diff HEAD repo2/your_repostory_branch_name

其中,HEAD表示当前仓库的最新提交,repo2/your_repostory_btanch_name表示另一个仓库的分支。这个命令将会输出两个仓库之间的差异信息。

二、报错解决

1、git pull报错

解决过程:

git pull拉取最新的代码报错

fatal: unable to access '*****': Failed connect to github.com:443; Connection refused

首先查看拉取地址:git remote -v

bash 复制代码
origin	https://github.com/***.git (fetch)
origin	https://github.com/***.git (push)

(1)看到目前是http方式下载的,有人说可以转换另一种下载方式,尝试转换ssh下载:

bash 复制代码
git remote set-url https://github.com/***.git

再用git remote -v查看发现已经转换完成:

bash 复制代码
origin	git@github.com:***.git (fetch)
origin	git@github.com:***.git (push)

重新git pull拉取,继续报错:

bash 复制代码
ssh_exchange_identification: read: Connection reset by peer
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

(2)尝试重启或更改代理:

bash 复制代码
git config --global http.proxy 
git config --global https.proxy

没有任何反应

网上还有很多解决办法,可以一一尝试,因为我的代码远端没有更新,目前解决到这就算了

相关推荐
云端奇趣1 小时前
探索 3 个有趣的 GitHub 学习资源库
经验分享·git·学习·github
F_D_Z5 小时前
【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
网络·git·网络协议·ssl
佛系小嘟嘟8 小时前
Android-由switch-case和view.getId()引起的bug:错误:需要常量表达式 的解决办法
android·bug
未来可期LJ8 小时前
【测试-BUG篇】软件测试的BUG知识你了解多少呢?
软件测试·bug
软件测试很重要8 小时前
追梦无Bug的软件世界
bug
等风来不如迎风去10 小时前
【git】main|REBASE 2/6
git
艾伦~耶格尔10 小时前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea
云兮杜康10 小时前
IDEA中用过git之后文件名颜色全变红
java·git·intellij-idea
睡不醒的小泽10 小时前
git merge 和 git rebase
git
艾伦~耶格尔10 小时前
Git 下载及安装超详教程(2024)
git·gitee·码仓