0
点赞
收藏
分享

微信扫一扫

git实战案例之分支操作

闲嫌咸贤 2022-01-26 阅读 57
javagit2d


git实战案例之分支操作

0. 背景

在GitHub 上有一个代码库,但是我想在一台从来没有操作过的电脑上,想拉取这个Github 的代码,那该怎么操作呢?

1. 主要步骤

  • ​git init​​​ 这个步骤的主要功能是,初始化当前的文件夹为一​​git​​ 文件,方便后期被追踪。
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git init
Reinitialized existing Git repository in D:/Java_Project/dayProgram/.git/

因为笔者的当前文件夹已经初始化过了,所以再次执行这个命令的时候,就出现了​​Reinitialized existing....​

同时,可以看到在每行记录下末尾都标志了一个​​(master)​​,这个标志用于表示当前的分支

  • ​git remote add origin xxxx​​​ 这个命令是将当前的这个文件夹添加到远程指定的项目上。【但是我的疑问是,指定了这个项目,但是会指定一个具体的分支么?还是说默认指向 ​​mster​​ 分支?】
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git remote add origin https://github.com/LittleLawson/DayProgram.git
fatal: remote origin already exists.

因为我的这个文件已经指向了这个项目地址,所以这里会报错:​​remote origin already exists!!!​

  • ​git branch​​ 查看当前分支,有多种不同的用法

    • ​git branch -r​​ 查看远程分支

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git branch -r
origin/master
  • ​git branch -a​​ 查看所有分支
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git branch -a
* master
remotes/origin/master
  • ​git checkout​​​ 创建新的分支。
    创建新的分支的操作常用于提交代码,这样就会避免多个时刻提交代码带来的冲突。
    如下,我有一些修改的代码,需要提交,那么我就需要创建一个新的分支,就命名成​​Develop​​,需要注意语法的格式,如下:​​git checkout -b [new branch name] origin/[existing branch]​
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git checkout -b Develop origin/master
Switched to a new branch 'Develop'
A .idea/vcs.xml
M .idea/workspace.xml
M src/main/java/multi/thread/consume/Consumer.java
M src/main/java/multi/thread/consume/ConsumerOne.java
M src/main/java/multi/thread/entrance/Main.java
M src/main/java/multi/thread/produce/Producer.java
M src/main/java/multi/thread/produce/ProducerOne.java
M src/main/java/multi/thread/threadpool/ConsumeThread.java
M src/main/java/multi/thread/threadpool/ProduceThread.java
M target/classes/multi/thread/consume/ConsumerOne.class
M target/classes/multi/thread/entrance/Main.class
M target/classes/multi/thread/produce/Producer.class
M target/classes/multi/thread/produce/ProducerOne.class
M target/classes/multi/thread/threadpool/ConsumeThread.class
M target/classes/multi/thread/threadpool/ProduceThread.class
Branch 'Develop' set up to track remote branch 'master' from 'origin'.

可以看到当前的 ​​branch​​ 从 ​​master​​ 切到了 ​​develop​​。​​Branch 'Develop' set up to track remote branch 'master' from 'origin'.​​这句话的意思就是,​​Develop​​这个分支开始跟踪远程的​​master​​分支。

再次检查分支:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git branch
* Develop
master

可以看到这次当前分支从​​master​​​ 移到了​​Develop​​​了。但是通过​​git branch -r​​​查看远程分支,看到还是只有一个​​master​​分支

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git branch -r
origin/master

查看当前分支状态:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git status
On branch Develop
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: .idea/vcs.xml
modified: .idea/workspace.xml
modified: src/main/java/multi/thread/consume/Consumer.java
modified: src/main/java/multi/thread/consume/ConsumerOne.java
modified: src/main/java/multi/thread/entrance/Main.java
modified: src/main/java/multi/thread/produce/Producer.java
modified: src/main/java/multi/thread/produce/ProducerOne.java
modified: src/main/java/multi/thread/threadpool/ConsumeThread.java
modified: src/main/java/multi/thread/threadpool/ProduceThread.java
modified: target/classes/multi/thread/consume/ConsumerOne.class
modified: target/classes/multi/thread/entrance/Main.class
modified: target/classes/multi/thread/produce/Producer.class
modified: target/classes/multi/thread/produce/ProducerOne.class
modified: target/classes/multi/thread/threadpool/ConsumeThread.class
modified: target/classes/multi/thread/threadpool/ProduceThread.class

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: .idea/workspace.xml

注意到,这里的​​Develop​​分支里面的状态为空,所以需要添加东西进去:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git add -A
warning: LF will be replaced by CRLF in .idea/workspace.xml.
The file will have its original line endings in your working directory

提交到本地缓存:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git commit -m "develop"
[Develop e178879] develop
15 files changed, 286 insertions(+), 170 deletions(-)
create mode 100644 .idea/vcs.xml
rewrite target/classes/multi/thread/consume/ConsumerOne.class (91%)
rewrite target/classes/multi/thread/entrance/Main.class (75%)
rewrite target/classes/multi/thread/produce/ProducerOne.class (90%)
rewrite target/classes/multi/thread/threadpool/ConsumeThread.class (100%)
rewrite target/classes/multi/thread/threadpool/ProduceThread.class (100%)

​git push origin Develop:master​​​ //推送本地的​​Develop​​​(冒号前面的)分支到远程​​origin​​​的​​master​​(冒号后面的)分支(没有会自动创建)

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git push origin Develop:master
Enumerating objects: 64, done.
Counting objects: 100% (64/64), done.
Delta compression using up to 4 threads
Compressing objects: 100% (26/26), done.
Writing objects: 100% (35/35), 7.28 KiB | 621.00 KiB/s, done.
Total 35 (delta 13), reused 0 (delta 0)
remote: Resolving deltas: 100% (13/13), completed with 12 local objects.
To https://github.com/LittleLawson/DayProgram.git
d7717da..e178879 Develop -> master
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git push origin --delete Develop
error: unable to delete 'Develop': remote ref does not exist
error: failed to push some refs to 'https://github.com/LittleLawson/DayProgram.git'

因为是将本地的​​Develop​​​分支推送到远端的​​master​​​分支,而不是创建一个新的​​Develop​​​分支,所以远端并不存在一个新的​​Develop​​​分支,所以删除远端​​Develop​​​分支会报​​remote ref does not exist​​。同时使用如下命令查看的时候,仍然只有一个远端分支:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git branch -r
origin/master
  • ​git branch -d Develop​​ 删除分支
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git branch -d Develop
error: Cannot delete branch 'Develop' checked out at 'D:/Java_Project/dayProgram'

但是你会看到这个命令执行却报错了,原因是:我在当前分支删除这个分支,肯定是不可以的,我需要切换到其它分支再执行这个操作。

  • ​git checkout xxx​​ 切换分支
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (Develop)
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

因为刚才推送了一个版本到远端的​​master​​​上,所以这里会报一个hint,意思就是说:你的分支落后于远端的​​origin/master​​​的一次commit,(但是)可以使用​​fast-forwarded​​​这个方法去更新。(使用​​git pull​​去更新你的本地分支)。

解决上述的提示有两种方法,分别如下:

  • ​git merge [branch]​​​ 这里的​​git merge Develop​​的功能就是将​​Develop​​分支合并到 ​​master​​上。
LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git merge Develop
Updating d7717da..e178879
Fast-forward
.idea/vcs.xml | 6 +
.idea/workspace.xml | 357 +++++++++++++---
src/main/java/multi/thread/consume/Consumer.java | 2 +-
.../java/multi/thread/consume/ConsumerOne.java | 31 +-
src/main/java/multi/thread/entrance/Main.java | 17 +-
src/main/java/multi/thread/produce/Producer.java | 4 +-
.../java/multi/thread/produce/ProducerOne.java | 29 +-
.../multi/thread/threadpool/ConsumeThread.java | 5 +-
.../multi/thread/threadpool/ProduceThread.java | 5 +-
.../classes/multi/thread/consume/ConsumerOne.class | Bin 1561 -> 1927 byt
target/classes/multi/thread/entrance/Main.class | Bin 1395 -> 1432 byt
target/classes/multi/thread/produce/Producer.class | Bin 1017 -> 1016 byt
.../classes/multi/thread/produce/ProducerOne.class | Bin 1482 -> 1842 byt
.../multi/thread/threadpool/ConsumeThread.class | Bin 706 -> 589 bytes
.../multi/thread/threadpool/ProduceThread.class | Bin 731 -> 590 bytes
15 files changed, 286 insertions(+), 170 deletions(-)
create mode 100644 .idea/vcs.xml

可以看到在合并的过程中,输出了一堆的​​Fast-forward​​信息。

在合并​​Develop​​分支到​​master​​之后,就可以开始删除​​Develop​​分支了:

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git branch -d Develop
Deleted branch Develop (was e178879).

LittleLawson@DESKTOP-PA2BQ2D MINGW64 /d/Java_Project/dayProgram (master)
$ git branch
* master



举报

相关推荐

0 条评论