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

没有任何反应

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

相关推荐
Jammingpro8 小时前
【Git版本控制】Git初识、安装、仓库初始化与仓库配置(含git init、git config与配置无法取消问题)
java·git·elasticsearch
shark15 小时前
无需放弃变更、关闭占用程序!用暂存区和 git底层命令实现 Git 变更备份
git·shell·自动化运维
_poplar_15 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
北城笑笑16 小时前
Git 10 ,使用 SSH 提升 Git 操作速度实践指南( Git 拉取推送响应慢 )
前端·git·ssh
蓁蓁啊21 小时前
GIT使用SSH 多账户配置
运维·git·ssh
相与还1 天前
IDEA和GIT实现cherry pick拣选部分变更到新分支
git·elasticsearch·intellij-idea
初圣魔门首席弟子1 天前
c++ bug 记录(merge函数调用时错误地传入了vector对象而非迭代器。)
java·c++·bug
刘志辉1 天前
git指令
git
2501_916766541 天前
【Git学习】初识git:简单介绍及安装流程
git·学习
孤独的追光者2 天前
Git 完整流程:从暂存到推送
git