科技解说
每天一篇好文

关于 Git 和 Github 的一些常见问题 避坑指南

在我们使用 Git 连接 Github 进行代码部署的时候,肯定都遇到了很多的问题,以下是我整理的一些小小细节,希望对大家有所帮助:

一、error: src refspec xxx does not match any / error: failed to push some refs to

二、GitHub 22 端口被占用,改用 443 端口连接

三、git push 时 报error: src refspec main does not match any.

四、git提示“warning: LF will be replaced by CRLF”的解决办法

五、! [rejected] main -> main (fetch first) error: failed to push some refs to

六、git push报错:Updates were rejected because the tip of your current branch is behind

七、git提交失败:Updates were rejected because the tip of your current branch is behind

八、Git 推送本地仓库到远程时出现错误“error: src refspec master does not match any”

以上参考解决方法写得很详细,但是我一步步运行后还是出现类似错误:
“error: src refspec master does not match any
error: failed to push some refs to ‘e.coding.net:poowicat/vuepress_blog_rescoure
/blog-source.git’

于是回想起来,我之前把分支改成了master——>mian
然后按照参考博客解决方法重新执行一遍,把git push origin master改成git push origin main于是成功解决问题!!!

实际上

git init
这一步之后创建了一个名为.git的文件夹,不过它在默认状态下是隐藏的,系统将隐藏文件夹显示出来,可以看到有这样一个文件夹。
github上传项目方法:
git@e.coding.net:poowicat/vuepress_blog_rescoure/blog-source.git
在你的电脑上装好git

大致流程是:

1、在github上创建项目

2、使git clone git@e.coding.net:poowicat/vuepress_blog_rescoure/blog-source.git 克隆到本地

3、编辑项目

4、git add .(将改动添加到暂存区)

5、git commit -m“提交说明”

6、git push origin main 将本地更改推送到远程master分支(这里我已经将我的本project命名为main分支。

这样你就完成了向远程仓库的推送。

如果在github的remote上已经有了文件,会出现错误。此时应当先pull一下,即:

git pull origin master

然后再进行:

git push origin master

修改项目内容有更新在远程上

  1. git commit -m “修正图片路径”
  2. git pull
  3. git push

九、git配置config文件

1.Git有一个工具被称为git config,它允许你获取和设置变量;这些变量可以控制Git的外观和操作的各个方面。这些变量以等级的不同可以被存储在三个不同的位置:

(1) /etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’–system’ 给 git config,它将明确的读和写这个文件。

(2) ~/.gitconfig 文件 :具体到你的用户。你可以通过传递–global 选项使Git明确的读或写这个特定的文件。

(3) .git/config位于git目录的config文件,特定指向该单一的库。如果git config 时不加–system 也不加–global选项,那么只作用于当前的git版本库,配置产生的修改都体现在.git/config文件中

三个config文件是逐级覆盖的关系,具体的覆盖非具体的。

2.例子
(1) 用户标识配置
$ git config –global user.name “John Doe” //user.name就是对[user]下的name进行配置
$ git config –global user.email johndoe@example.com

$ cat ~/.gitconfig
[user]
email = johndoe@example.com
name = John Doe

这里的修改是针对这个用户的所有git版本库的,若不加–global可以就是只针对某一个具体的版本库起作用,修改体现在.git/config下。

3.更多例子
$ git config –global core.editor emacs 指定你的编辑器
$ git config –global merge.tool vimdiff 指定你的比较工具(Your Diff Tool)
$ git config –list 检查你的设置(Checking Your Settings)
$ git help config 获取帮助(Getting help)

4.分别移除各个等级的一个配置项
git config –unset user.name
git config –unset –global user.name
git config –unset –system user.name

5.移除一组配置项
git config –remove-section color

6. git config get user.name  获取一个属性的值,当然也可以直接cat上面的config文件。

7.我的.gitconfig文件

# cat ~/.gitconfig 
[user]
        name = xxx
        email = xxx@xxx.com
[alias]
        st = status
        ci = commit
[commit]
[core]
        editor = vim
[color]
        ui = auto
[push]
        default = matching
[alias] 
        st = status    
        co = checkout
        ci = commit
        br = branch
        lo = log --oneline
        la = log --author
        lg = log --graph
        dc = diff --cached
        cp = cherry-pick
        dir = rev-parse --git-dir

补充:

bc = branch –contains  //git branch –contains <hash> 查看包含hash提交的分支。

8. 若变更服务器了,可以使用scp命令来同步git配置(包括私钥) 和其它一些配置

(1) 假设两台机器IP分别为:A.104.238.161.75,B.43.224.34.73,你要从A服务器变更为B服务器,那么需要将A服务器上与git相关的配置文件拷贝到B服务器相同目录下,执行:

scp -r ~/{.ssh,.netrc,.gitconfig} <your job number>@43.224.34.73:<home path>

 

赞(0) 打赏
未经允许不得转载:篇微 » 关于 Git 和 Github 的一些常见问题

评论 抢沙发

评论前必须登录!

立即登录   注册

登录

找回密码

注册