git 里面遇到的问题

Posted chen狗蛋儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git 里面遇到的问题相关的知识,希望对你有一定的参考价值。


第一步:建立git仓库(本地)
cd到你的本地项目根目录下,执行git命令

git init

第二步:将项目的所有文件添加到仓库中

git add .

如果想添加某个特定的文件,只需把.换成特定的文件名即可

第三步:将add的文件commit到仓库

git commit -m "注释语句"

第四步:去github上创建自己的Repository,创建页面如下图所示:

 

第五步:重点来了,将本地的仓库关联到github上

git remote add origin https://github.com/Interesting6/my_numeral_calculations.git

后面的https链接地址换成你自己的仓库url地址

第六步:上传github之前,要先pull一下,执行如下命令:

git pull origin master

敲回车后,会执行输出类似如下

第七步,也就是最后一步,上传代码到github远程仓库

git push -u origin master

执行完后,如果没有异常,等待执行完就上传成功了,中间可能会让你输入Username和Password,你只要输入github的账号和密码就行了

 

-----------------------------------------------------------------------------------------------------------------------------------

遇到的问题,有时我只需要上传一个文件,而这个文件在GitHub上还没有目录,在刚刚仓库(本地)的目录下打开git bash:

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git add ./functionroot/iteration_method.py 
# 添加该functionroot目录下的文件iteration_method.py 

然后提交一下

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git commit -m "solving nonlinear function in iteration method"
[master c08aed3] solving nonlinear function in iteration method
 1 file changed, 133 insertions(+)
 create mode 100644 functionroot/iteration_method.py

然后push就出错了:

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git push -u origin master
To https://github.com/Interesting6/my_numeral_calculations.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to \'https://github.com/Interesting6/my_numeral_calculations.git\'
hint: Updates were rejected because the remote contains work that you do         # 更新被拒绝,因为远程包含本地没有的工作。
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., \'git pull ...\') before pushing again.
hint: See the \'Note about fast-forwards\' in \'git push --help\' for details.

试了一下网上说的:

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git pull --rebase origin master
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

也没用,然后试了下下面的:

You may want to first merge the remote changes (e.g., \'git pull\') before pushing again.

在再次推送之前,您可能需要首先合并远程更改(例如,“git pull”)。

that is:

git pull
# Fix any merge conflicts, if you have a `README.md` locally
git push -u origin master

输入下面就通过vim进入一个文件内,我直接保存退出

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/Interesting6/my_numeral_calculations
   8fd4a0c..31dacac  master     -> origin/master
 * [new branch]      Interesting6-solve_linear_equations -> origin/Interesting6-solve_linear_equations
Merge made by the \'recursive\' strategy.
 Interpolation.html => Interpolation/Interpolation.html | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename Interpolation.html => Interpolation/Interpolation.html (100%)

然后再push一下就好了,很迷。。

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git push -u origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 1.61 KiB | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/Interesting6/my_numeral_calculations.git
   31dacac..8447fed  master -> master
Branch master set up to track remote branch master from origin.

 -------------------------------------------------------------------------------------------

下面是我对文件里面的README.md进行更新:

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git add README.md

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git commit -m "update readme"
[master 3db337d] update readme
 1 file changed, 7 insertions(+), 2 deletions(-)

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 433 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Interesting6/my_numeral_calculations.git
   8447fed..3db337d  master -> master
Branch master set up to track remote branch master from origin.

Mr.Black@DESKTOP-4Q3FM6F MINGW64 /e/Sublime/python/calculate (master)

就好了。

以上是关于git 里面遇到的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何管理在每个 git 版本中添加私有代码片段?

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

使用 Git 来管理 Xcode 中的代码片段

git 里面遇到的问题