在GitHub 上有一个代码库,但是我想在一台从来没有操作过的电脑上,想拉取这个Github 的代码,那该怎么操作呢?
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 ..." 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 ..." to update what will be committed)
(use "git checkout -- ..." 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