Git教程-命令行版本

add->commit->push

1 今天我们开始上班了,boss让我从仓库里clone代码,然后开发新的需求。

   gitee新建仓库。

   本地任意位置,右键git bash here:git clone https://gitee.com/wangchuan12137/sb.git,cd sb

 

2 我们可以每天通过git pull 或者git fetch +git merge获取服务器上远程仓库的最新代码。

   但是,也可以隔几天,或者干脆在push你的工作到服务器之前,再获取更新。

  因为假如你没有获取最新代码就push,会报错的。

  下面我们先工作吧

  上班第一天:打开README.md,在末尾输入“第一天的工作”,之后 用git add . 缓存,然后下班。

  上班第二天:打开README.md,在末尾输入“第二天的工作”,之后 用git add . 缓存,然后下班。

  第三天:推送/前两天的工作吧。

 git commit -m"需求一"

此时,本地master分支有两个提交,服务器就一个提交,我现在想push“需求一”到服务器上。

 push的报错

1 如果我们第一天克隆仓库之后,没有其他人push的话,那么此时我们push是不会报错的。

   但是如果有其他人已经更新过服务器的代码,我们再推送会报错。

修改gitee的README.md文件,在末尾添加“我是傻逼”,2次提交,gitee第二次提交相当于小组里的其他人更新了服务器。

 此时,我们在本地仓库不是最新代码的前提下push,会报错。

$ git push
To https://gitee.com/wangchuan12137/sb.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/wangchuan12137/sb.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.

2 网上的方法都是先pull/fetch +merge,然后再push。(之前说,不用每天获取更新,只需要在push之前更新一次即可。)

为了彻底理解合并(merge),我认为应该分三种情况讨论:

法一(多继承,AC不是AB的子链,四次提交&非线性)

基于AB+AC进行合并,BC都是D的父亲结点。

 

 0 接前面的步骤 git push 报错

1 git pull

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 987 bytes | 164.00 KiB/s, done.
From https://gitee.com/wangchuan12137/sb
   339554a..d083474  master     -> origin/master
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

2 打开README.md,手动修改冲突的地方。

   同事希望把 README改成:很长的一段+我是傻逼。

   我希望把README改成:很长的一段+第一天的工作。+第二天的工作。

经过友好协商,我们决定把README.md文件修改为:

很长的一段+第一天的工作。+第二天的工作+我是傻逼。

3 git add .

   git commit -m"conflict fixed"

   git push

4  此时服务器和本地,都可以看到有四次提交过程。

conflict fixed是基于 update README.md(其他同事)和需求一(我),手动修改两个人的冲突,得到新的提交结点。

用git log --graph --pretty=oneline --abbrev-commit,可以看到这种多重继承的关系。

git log --graph --pretty=oneline --abbrev-commit
*   66545fa (HEAD -> master, origin/master, origin/HEAD) conflict fixed
|\
| * 5ff0e6c update README.md.
* | b003005 需求一
|/
* b2f9a21 Initial commit

法二(单继承,A是AB子链,硬回退版本,word备份,三次提交&线性)

 

0 接前面的步骤 git push 报错

1 本地master硬回退一个版本:git reset --hard  HEAD^

   同时用word备份丢弃的东西:即第一天工作。第二天工作。

$ git reset --hard HEAD^
HEAD is now at 05a9ca1 Initial commit

道命@LAPTOP-E8JVCIJ6 MINGW64 /f/Project/Git/sb (master)
$ git log
commit 05a9ca17cd0f1df43cd58d3ffb01499612bc026e (HEAD -> master, origin/master, origin/HEAD)
Author: 鱼啊鱼川啊川 <1453620966@qq.com>
Date:   Wed Apr 5 06:58:04 2023 +0000

    Initial commit

2 git pull,因为A是AB的子链,所以git pull顺利地fast-forward

$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1020 bytes | 204.00 KiB/s, done.
From https://gitee.com/wangchuan12137/sb
   05a9ca1..4c7fffd  master     -> origin/master
Updating 05a9ca1..4c7fffd
Fast-forward
 README.md | 2 ++
 1 file changed, 2 insertions(+)

3 此时本地分支,和远程分支一样,都是AB。

   丢弃的改动,相当于C,因为有在word里备份 ,所以可以重新进行工作。

打开新的README.md,在末尾添加word备份的东西。(和同事协商,怎么在他提交版本的基础上,加入我开发的内容。)

之后git add.,git commit -m"需求一",git push。

4 此时服务器和本地,都看到有三次提交。而且三次提交是线性的。

git log --graph --pretty=oneline --abbrev-commit

 

$ git log --graph --pretty=oneline --abbrev-commit
* 67acfb4 (HEAD -> master, origin/master, origin/HEAD) 需求一
* 4c7fffd update README.md.
* 05a9ca1 Initial commit

法三(单继承,A是AB子链,混合回退版本,堆栈备份,三次提交&线性)

 

 

0 接前面的步骤 git push 报错

1 本地master混合回退一个版本:git reset --mixed HEAD^

2 用堆栈备份:git stash。

 “第一天的工作内容,第二天的工作内容”被备份到堆栈里,txt里看不到了。

   

3 git  pull,下载服务器的最新更新。

   

 4 git stash pop,弹出堆栈里的备份时,会和服务器的更新发生冲突。

$ git stash pop
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
On branch master
Your branch is up to date with 'origin/master'.

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.

 5 依然是和同事友好协商后,决定最终版本的readme.md:

(同一个地方,有两种不同的修改,冲突了,最终决定两种都保留)

6 git add . ,git commit -m"需求一",git push。

  

7 此时服务器和本地,都看到有三次提交。而且三次提交是线性的。

git log --graph --pretty=oneline --abbrev-commit

$ git log --graph --pretty=oneline --abbrev-commit
* 575560b (HEAD -> master, origin/master, origin/HEAD) 需求一
* 7087080 update README.md.
* 1c9de3b Initial commit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值