git实操

今天尝试用gitee进行两台电脑上的笔记的同步,在实验室尝试将笔记内容上传到gitee,结果因为git基本功不扎实又对git进行了系统的学习,记录一下在实际操作中碰到的分支问题和我自己的理解

1.本地在经过两次提交后和推到远端后

1
2
3
4
5
6
7
8
9
10
11
12
13

$ git log
commit 78a5b31d3ba4e4faabc7f3acd7ad138fc8de4b12 (HEAD -> master, origin/master)
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 10:51:30 2020 +0800

修改内容

commit 87c8dc246891d19ac931e7d736a4d0cd18e7876e
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 09:05:20 2020 +0800

全部提交

2.我手贱尝试了下git reset --hard HEAD^

1
2
3
4
5
6
7

$ git log
commit 87c8dc246891d19ac931e7d736a4d0cd18e7876e (HEAD -> master)
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 09:05:20 2020 +0800

全部提交

果然修改内容的版本没了

3.然后我又对文件“git和github教程”进行了修改

1
2
3
4

$ git commit -m "对git有了更系统的认识"
[master 9659097] 对git有了更系统的认识
1 file changed, 117 insertions(+), 57 deletions(-)

推到远端毫无疑问的报错了

1
2
3
4
5
6
7
8
9

$ git push origin master
To gitee.com:biongd/NOTEBOOK.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:biongd/NOTEBOOK.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因:

本地:全部提交–内容修改–对git有了更系统的认识,即A–C

远程:全部提交–内容修改,即A–B

关键是B和C的共有文件夹“git和github教程”发生改变了啊!!!合并必然报错啊!!!但是懵懂的我并不知道是这个原因。。。。。。。。。。。。

4.然后习惯性的执行了git pull

1
2
3
4
5
6
7
8
9
10
11
12

$ git pull --rebase origin master
From gitee.com:biongd/NOTEBOOK
* branch master -> FETCH_HEAD
error: could not apply 9659097... 对git有了更系统的认识
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 9659097... 对git有了更系统的认识
Auto-merging git和github教程.md
CONFLICT (content): Merge conflict in git和github教程.md

没疑问的报了冲突。

可以看到本地分支的变化

1
2
3
4

原来:terahertz@DESKTOP-R5AO72E MINGW64 /d/biongd/blog/source/_posts (master)

现在:terahertz@DESKTOP-R5AO72E MINGW64 /d/biongd/blog/source/_posts (master|REBASE 1/1)

解释:

由于执行rebase导致,(master|REBASE 1/1)表示分支当前的冲突等待处理

可以执行的操作

1
2
3

1.git rebase --continue 手动解决冲突后继续执行
2.git rebase --abort 放弃解决冲突

但是作为一个傻白甜的萌新怎么可能知道这些,我学着用git diff查看冲突内容,然后按q退出了

之后原本的文件就多了许多的提示冲突的内容。

之后的状态就变成了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

terahertz@DESKTOP-R5AO72E MINGW64 /d/biongd/blog/source/_posts (master|REBASE 1/1)
$ git status
interactive rebase in progress; onto 78a5b31
Last command done (1 command done):
pick 9659097 对git有了更系统的认识
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on '78a5b31'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: git和github教程.md

5.回溯+重新提交

灵机一动,通过git reset HEAD来回溯commit不就可以找回文件了吗,而且还能将本地的“内容修改”,也就是例子中的B找回来,

这样本地分支和远程分支的路径也就不会冲突了。可以直接push。

事实证明这是科学的,也是可行的,但是前提是从(master|REBASE 1/1)状态退出

但是我木有,我直接在这种状态下push,结果。。。。。

还是一样的错误!!!!

然后我又一次:git pull

1
2
3
4
5
6
7
8
9
10
11
12

$ git pull --rebase origin master
From gitee.com:biongd/NOTEBOOK
* branch master -> FETCH_HEAD
fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase. If that is the
case, please try
git rebase (--continue | --abort | --skip)
If that is not the case, please
rm -fr ".git/rebase-merge"
and run me again. I am stopping in case you still have something
valuable there.

你看,人家都把路给我指明了(–continue | –abort | –skip)

但是我硬生生的选择了rm -fr “.git/rebase-merge”

OK,接下来在仓库中再次执行git pull :(杠上了)

1
2
3
4
5

$ git pull
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

没错,我有仓库但是没分支啦。。HEAD处于游离态。。

执行git status也是:

1
2
3
4
5
6

$ git status
Not currently on any branch.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: git和github教程.md

6.重新回到分支

1
2
3

$ git checkout -b master
fatal: A branch named 'master' already exists.

想新建并回到master分支,但人家提醒有过了,看来分支并不会消除,只是HEAD丢了啊

重新给当前仓库定位分支:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

$ git checkout master
Warning: you are leaving 3 commits behind, not connected to
any of your branches:

54d4213 尝试
d3df77b 对git有了更系统的认识
78a5b31 修改内容

If you want to keep them by creating a new branch, this may be a good time
to do so with:

git branch <new-branch-name> 54d4213

Switched to branch 'master'
Your branch is up to date with 'origin/master'.

到这我已经懒得去调整他的提交顺序了,因为对分支的含义已经理解的差不多了

于是,重建新的远程仓库+重新提交,一切都非常完美,本地master和origin/master都回到了正规。

7.思考

这也意味着一个事实,我的每一次修改都必须提交并推到远端。否则在下次进行修改时就会显示:

1
2
3
4

$ git pull --rebase origin master
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

也就是你对工作区的修改没有提交导致的报错

当然,可以在一天的工作完成后准备回寝室的时候统一git add . + 提交 + push

8.把远程的文件pull到本地

回到了寝室,直接把_post文件夹下的文件全部清空,然后进行git初始化仓库。直接

1
git pull origin master

可以看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

$ git log
commit 15d7ed4ecd2963baa495e4cb7a3bf8497767344c (HEAD -> master, origin/master)
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 16:55:00 2020 +0800

2020-07-22

commit 96590977e10c759b233e32e140e72757129f781d
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 13:12:32 2020 +0800

对git有了更系统的认识

commit 87c8dc246891d19ac931e7d736a4d0cd18e7876e
Author: Biongd <xv06125231@126.com>
Date: Wed Jul 22 09:05:20 2020 +0800

全部提交