Git
Description
Learn about git(VCS)
1.建Git仓库
1.把已有的项目纳入Git管理
$ cd 项目代码所在文件夹
$ git init
2.新建的项目直接用Git管理
$ cd 某个文件夹
$ git init your_project #会在当前路径下创建和项目名称同名的文件夹
$ cd your_project
第1种方案的结果
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ cd Project_code_is_there/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there
$ git init
Initialized empty Git repository in E:/FrontEnd/LearnGit/Project_code_is_there/.git/
初始化后有一个隐藏文件
.git
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 4
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:08 ../
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 .git/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --global --list
user.name=fireworks99
user.email=2639237361@qq.com
core.autocrlf=false
local config与global config不同时,local config优先级更高。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --global --list
user.name=fireworks99
user.email=2639237361@qq.com
core.autocrlf=false
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --local user.name 'fire'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --local user.email 'zhaobaole520@gmail.com'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
user.name=fire
user.email=zhaobaole520@gmail.com
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ pwd
/e/FrontEnd/LearnGit/Project_code_is_there
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cd ../virtual/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/virtual
$ ls
readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/virtual
$ cd ../Project_code_is_there/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp ../virtual/readme.md
cp: missing destination file operand after '../virtual/readme.md'
Try 'cp --help' for more information.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp ../virtual/readme.md .
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add readme'
On branch master
Initial commit
Untracked files:
readme.md
nothing added to commit but untracked files present
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add readme'
[master (root-commit) 6debbe5] Add readme
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit 6debbe50d02c2054b7693addc100a685c6a97cb9 (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
2.Git的工作方式
git工作方式:在工作目录中修改的东西,最好通过git add先提交到暂存区,如果差不多了再通过git commit进行统一的提交。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp ../virtual/index.html index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp -r ../virtual/images .
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
images/
index.html
nothing added to commit but untracked files present (use "git add" to track)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add index.html images
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 images/
-rw-r--r-- 1 sj 197121 2214 10月 6 21:54 index.html
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: images/gh-branch.png
new file: images/gh-logo.png
new file: index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: images/gh-branch.png
new file: images/gh-logo.png
new file: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
images/git.jpg
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add images/git.jpg
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: images/gh-branch.png
new file: images/gh-logo.png
new file: images/git.jpg
new file: index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 21:59 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add index + logo'
[master 1a103f4] Add index + logo
4 files changed, 76 insertions(+)
create mode 100644 images/gh-branch.png
create mode 100644 images/gh-logo.png
create mode 100644 images/git.jpg
create mode 100644 index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit 1a103f45637ca22689a92a0f09ac438fcb327729 (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 22:01 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ mkdir styles
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp ../virtual/styles/style.css styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 22:02 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 22:01 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cd styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/styles (master)
$ ls
style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/styles (master)
$ git status
On branch master
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: ../index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
./
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/styles (master)
$ cd ..
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 22:02 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
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: index.html
Untracked files:
(use "git add <file>..." to include in what will be committed)
styles/
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add styles
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: styles/style.css
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: index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
new file: styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add style.css and modified index.html'
[master 887d452] Add style.css and modified index.html
2 files changed, 67 insertions(+)
create mode 100644 styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit 887d45217f404f50ba646356b355ef7df842655a (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cp -r ../virtual/js/ .
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 6 22:07 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:53 ../
drwxr-xr-x 1 sj 197121 0 10月 6 22:06 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
drwxr-xr-x 1 sj 197121 0 10月 6 22:07 js/
-rw-r--r-- 1 sj 197121 0 10月 6 21:32 readme.md
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
js/
nothing added to commit but untracked files present (use "git add" to track)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add js
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit 887d45217f404f50ba646356b355ef7df842655a (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add js'
[master ddd14f3] Add js
1 file changed, 14 insertions(+)
create mode 100644 js/script.js
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
3.Git 文件重命名
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ mv readme.md readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rm readme.md
rm 'readme.md'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: readme.md -> readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git reset --hard
HEAD is now at ddd14f3 Add js
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git mv readme.md readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: readme.md -> readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 7 15:30 ./
drwxr-xr-x 1 sj 197121 0 10月 7 15:26 ../
drwxr-xr-x 1 sj 197121 0 10月 7 15:30 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
drwxr-xr-x 1 sj 197121 0 10月 6 22:07 js/
-rw-r--r-- 1 sj 197121 0 10月 7 15:29 readme
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Move readme.md to readme'
[master d7cfccc] Move readme.md to readme
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme.md => readme (100%)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 15:30:54 2020 +0800
Move readme.md to readme
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
:...skipping...
commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 15:30:54 2020 +0800
Move readme.md to readme
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
commit 1a103f45637ca22689a92a0f09ac438fcb327729
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Add index + logo
commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
原来的三步
mv readme.md readme
git add readme
git rm readme.md
只需要一步
git mv readme.md readme
命令
$ git reset --hard
:有时候,进行了错误的提交,但是还没有push到远程分支,想要撤销本次提交,可以使用git reset –-soft/hard命令。
1、二者区别:git reset –-soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可;
git reset -–hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容,撤销的commit中所包含的更改被冲掉;
4.git log的使用
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log --oneline
d7cfccc (HEAD -> master) Move readme.md to readme
ddd14f3 Add js
887d452 Add style.css and modified index.html
1a103f4 Add index + logo
6debbe5 Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log -n4 --oneline
d7cfccc (HEAD -> master) Move readme.md to readme
ddd14f3 Add js
887d452 Add style.css and modified index.html
1a103f4 Add index + logo
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -v
* master d7cfccc Move readme.md to readme
# 新建一个分支并同时切换到那个分支上,你可以运行一个带有 -b 参数(branch)的 git checkout 命令:
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git checkout -b temp
Switched to a new branch 'temp'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ ls -al
total 13
drwxr-xr-x 1 sj 197121 0 10月 7 16:06 ./
drwxr-xr-x 1 sj 197121 0 10月 7 15:50 ../
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
drwxr-xr-x 1 sj 197121 0 10月 6 22:07 js/
-rw-r--r-- 1 sj 197121 9 10月 7 16:06 readme
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ vi readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git commit -am'Add modefied'
[temp b9229fb] Add modefied
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git branch -v
master d7cfccc Move readme.md to readme
* temp b9229fb Add modefied
# git log 不加参数,只查看当前分支的信息
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git log
commit b9229fb1e4b5a6afafbc5da7701e803491e5ca93 (HEAD -> temp)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 16:07:52 2020 +0800
Add modefied
commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (master)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 15:30:54 2020 +0800
Move readme.md to readme
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
# --all 查看所有分支
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git log --all
commit b9229fb1e4b5a6afafbc5da7701e803491e5ca93 (HEAD -> temp)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 16:07:52 2020 +0800
Add modefied
commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (master)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 15:30:54 2020 +0800
Move readme.md to readme
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git log --all --graph
* commit b9229fb1e4b5a6afafbc5da7701e803491e5ca93 (HEAD -> temp)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 16:07:52 2020 +0800
|
| Add modefied
|
* commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 15:30:54 2020 +0800
|
| Move readme.md to readme
|
* commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:08:56 2020 +0800
|
| Add js
|
* commit 887d45217f404f50ba646356b355ef7df842655a
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:06:44 2020 +0800
|
| Add style.css and modified index.html
|
* commit 1a103f45637ca22689a92a0f09ac438fcb327729
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:01:32 2020 +0800
|
| Add index + logo
|
* commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ git log --oneline --all -n4 --graph
* b9229fb (HEAD -> temp) Add modefied
* d7cfccc (master) Move readme.md to readme
* ddd14f3 Add js
* 887d452 Add style.css and modified index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (temp)
$ gitk
# 图形界面查看版本历史
5.探索.git目录
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:39 ../
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 .git/
drwxr-xr-x 1 sj 197121 0 10月 6 21:58 images/
-rw-r--r-- 1 sj 197121 2260 10月 6 22:00 index.html
drwxr-xr-x 1 sj 197121 0 10月 6 22:07 js/
-rw-r--r-- 1 sj 197121 0 10月 7 16:44 readme
drwxr-xr-x 1 sj 197121 0 10月 6 22:03 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cd .git/
# .git目录
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ ls -al
total 23
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 ../
-rw-r--r-- 1 sj 197121 13 10月 7 16:07 COMMIT_EDITMSG
-rw-r--r-- 1 sj 197121 182 10月 6 21:25 config
-rw-r--r-- 1 sj 197121 73 10月 6 21:11 description
-rw-r--r-- 1 sj 197121 298 10月 7 16:28 gitk.cache
-rw-r--r-- 1 sj 197121 23 10月 7 16:44 HEAD
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 hooks/
-rw-r--r-- 1 sj 197121 722 10月 7 16:44 index
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 info/
drwxr-xr-x 1 sj 197121 0 10月 6 21:33 logs/
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 objects/
-rw-r--r-- 1 sj 197121 41 10月 7 15:29 ORIG_HEAD
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 refs/
# HEAD是一个引用,指向 refs/heads/master
# 说明当前Git仓库正在工作的分支是master
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ cat HEAD
ref: refs/heads/master
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ git branch -av
* master d7cfccc Move readme.md to readme
temp b9229fb Add modefied
# config配置文件,local>global,所以这里记录了local的配置
# 直接修改文件内容中user.name为fire1 与 命令$ git config --local user.name 'fire1'是一样的
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = fire
email = zhaobaole520@gmail.com
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ cd refs
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs (GIT_DIR!)
$ ls -al
total 4
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 ../
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 heads/
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 tags/
# 一个项目里可以有很多tags,标签,又称“里程碑”
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs (GIT_DIR!)
$ cd heads/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs/heads (GIT_DIR
$ ls -al
total 2
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 ./
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 ../
-rw-r--r-- 1 sj 197121 41 10月 7 15:30 master
-rw-r--r-- 1 sj 197121 41 10月 7 16:07 temp
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs/heads (GIT_DIR!)
$ cat master
d7cfccc0f65bbbe3d8c24a6f21838c772150060c
# 上面的hash值代表git仓库存放的一个对象
# $ git cat-file -t d7cfccc0f65bbbe3
# 上述命令查看文件类型 -t(type) hash值只指出前面一小部分就行,只要这部分没有与其他对象前部分hash值重复。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs/heads (GIT_DIR!)
$ git cat-file -t d7cfccc0f65bbbe3
commit
# 是commit类型
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs/heads (GIT_DIR!)
$ git branch -av
* master d7cfccc Move readme.md to readme
temp b9229fb Add modefied
# 可见master指向了d7cfccc0f65bbbe3d8c24a6f21838c772150060c这个commit对象
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/refs (GIT_DIR!)
$ cd ..
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git (GIT_DIR!)
$ cd objects/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects (GIT_DIR!)
$ ls -al
total 8
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:44 ../
drwxr-xr-x 1 sj 197121 0 10月 7 15:30 13/
drwxr-xr-x 1 sj 197121 0 10月 6 22:01 1a/
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 1f/
drwxr-xr-x 1 sj 197121 0 10月 6 21:33 23/
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 42/
drwxr-xr-x 1 sj 197121 0 10月 6 21:32 4b/
drwxr-xr-x 1 sj 197121 0 10月 6 22:06 50/
drwxr-xr-x 1 sj 197121 0 10月 6 22:05 5c/
drwxr-xr-x 1 sj 197121 0 10月 6 21:33 6d/
drwxr-xr-x 1 sj 197121 0 10月 6 22:06 88/
drwxr-xr-x 1 sj 197121 0 10月 6 22:08 97/
drwxr-xr-x 1 sj 197121 0 10月 6 21:59 9c/
drwxr-xr-x 1 sj 197121 0 10月 6 22:05 9d/
drwxr-xr-x 1 sj 197121 0 10月 6 22:01 a0/
drwxr-xr-x 1 sj 197121 0 10月 6 22:08 ae/
drwxr-xr-x 1 sj 197121 0 10月 6 22:01 b0/
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 b9/
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 c2/
drwxr-xr-x 1 sj 197121 0 10月 6 22:08 c5/
drwxr-xr-x 1 sj 197121 0 10月 6 21:55 cb/
drwxr-xr-x 1 sj 197121 0 10月 7 15:30 d7/
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 db/
drwxr-xr-x 1 sj 197121 0 10月 6 22:08 dd/
drwxr-xr-x 1 sj 197121 0 10月 6 22:06 de/
drwxr-xr-x 1 sj 197121 0 10月 6 21:33 e6/
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 info/
drwxr-xr-x 1 sj 197121 0 10月 6 21:11 pack/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects (GIT_DIR!)
$ cd e6
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/e6 (GIT_DIR!)
$ ls -al
total 5
drwxr-xr-x 1 sj 197121 0 10月 6 21:33 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 ../
-r--r--r-- 1 sj 197121 15 10月 7 15:28 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/e6 (GIT_DIR!)
$ git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
blob
# 类型为blob:文件对象
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/e6 (GIT_DIR!)
$ git cat-file -p e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
# -p 查看内容(这里没有输出,所以没有内容)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/e6 (GIT_DIR!)
$ cd ..
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects (GIT_DIR!)
$ cd 13
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/13 (GIT_DIR!)
$ ls -al
total 5
drwxr-xr-x 1 sj 197121 0 10月 7 15:30 ./
drwxr-xr-x 1 sj 197121 0 10月 7 16:07 ../
-r--r--r-- 1 sj 197121 176 10月 7 15:30 78caffa8c4f214e537299288bc08d3a1defaa3
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/13 (GIT_DIR!)
$ git cat-file -t 1378caffa8c4f214e537299288bc08d3a1defaa3
tree
# 类型为tree
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there/.git/objects/13 (GIT_DIR!)
$ git cat-file -p 1378caffa8c4f214e537299288bc08d3a1defaa3
040000 tree a0cc15dc64dd3332ad023955f6f84c786ef3f507 images
100644 blob 9dfbc09a91fdaad19875c1e359859438b16b4899 index.html
040000 tree 978a2b1ba7c0f07f9758f234b0b7959fe31a2e93 js
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 readme
040000 tree deca7330feed3ffb6a545bc41ca1b36a9660b3b6 styles
6.commit/tree/blob之间的关系
7.小练习:数一数tree的个数
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ pwd
/e/FrontEnd/LearnGit
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ git init watch_git_objects
Initialized empty Git repository in E:/FrontEnd/LearnGit/watch_git_objects/.git/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ cd watch_git_objects/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ ls -al
total 4
drwxr-xr-x 1 sj 197121 0 10月 7 18:47 ./
drwxr-xr-x 1 sj 197121 0 10月 7 18:47 ../
drwxr-xr-x 1 sj 197121 0 10月 7 18:47 .git/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ mkdir doc
# git并不理会空的文件夹
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ cd doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects/doc (master)
$ echo "Hello, world!" > readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects/doc (master)
$ cd ..
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
nothing added to commit but untracked files present (use "git add" to track)
# doc尚未加入暂存区,.git/object目录下没有f(file)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ find .git/objects -type f
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git add doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: doc/readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ find .git/objects -type f
.git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -t af5626b4a114
blob
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -p af5626b4a114
Hello, world!
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git commit -m'Add readme'
[master (root-commit) c92b8bb] Add readme
1 file changed, 1 insertion(+)
create mode 100644 doc/readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ find .git/objects -type f
.git/objects/6c/c12deb1d5d15beaa283ce0c552c4710a3a2048
.git/objects/8a/c93ed17177cbe3c263c29c877a93ffcbc4869b
.git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b
.git/objects/c9/2b8bb5b4c6a713eeda0150d13c7e3c192830f9
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -t 6cc12deb1d5d15
tree
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -p 6cc12deb1d5d15
040000 tree 8ac93ed17177cbe3c263c29c877a93ffcbc4869b doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -t 8ac93ed17177cbe3c
tree
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -p 8ac93ed17177cbe3c
100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -t af5626b4a114
blob
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -p af5626b4a114
Hello, world!
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -t c92b8bb5b4c6a71
commit
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git cat-file -p c92b8bb5b4c6a71
tree 6cc12deb1d5d15beaa283ce0c552c4710a3a2048
author fireworks99 <2639237361@qq.com> 1602068156 +0800
committer fireworks99 <2639237361@qq.com> 1602068156 +0800
Add readme
8.分离头指针
分离头指针:(头指针HEAD未指向一个有名分支,或者说指向一个临时无名分支)
就像java一样,一个引用指向一个new出来的对象,这个对象只被这一个引用所指,当这个引用指向其他对象时,这个对象就被java的垃圾回收机制回收了。
所以当头指针指回有名分支时,要考虑要不要给刚才的无名分支起名字,起名字后就不会被回收了。
$git checkout master
Warning: you are Leaving 1 commit behind, not connected to anyof your branches:
3d473ld Backgroud to green
If you want to keep it by creating a new branch, this may be a good time to do so with:
git branch <new-branch-name> 3d4731d
Switched to branch 'master'
头指针指向某个分支(中的某次提交——commit),所以本质上 HEAD->commit
9.删除分支
$ git branch -d branchName
若不行且风险可控$ git branch -D branchName
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git branch -av
* master c92b8bb Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git checkout -b temporary c92b8bb
Switched to a new branch 'temporary'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (temporary)
$ gitk
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (temporary)
$ git branch -av
master c92b8bb Add readme
* temporary c92b8bb Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (temporary)
$ git branch -d temporary
error: Cannot delete branch 'temporary' checked out at 'E:/FrontEnd/LearnGit/watch_git_objects'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (temporary)
$ git checkout -b master
fatal: A branch named 'master' already exists.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (temporary)
$ git checkout master
Switched to branch 'master'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git branch -d temporary
Deleted branch temporary (was c92b8bb).
10.修改commit的message
1.修改最新commit的message
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git log -1
commit c92b8bb5b4c6a713eeda0150d13c7e3c192830f9 (HEAD -> master)
Author: fireworks99 <2639237361@qq.com>
Date: Wed Oct 7 18:55:56 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git commit --amend
[master 2312de3] Add file readme
Date: Wed Oct 7 18:55:56 2020 +0800
1 file changed, 1 insertion(+)
create mode 100644 doc/readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/watch_git_objects (master)
$ git log -1
commit 2312de39f062b5bafc413578335241e7aac859bc (HEAD -> master)
Author: fireworks99 <2639237361@qq.com>
Date: Wed Oct 7 18:55:56 2020 +0800
Add file readme
2.修改之前commit的message
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master d7cfccc Move readme.md to readme
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log -3
commit d7cfccc0f65bbbe3d8c24a6f21838c772150060c (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Wed Oct 7 15:30:54 2020 +0800
Move readme.md to readme
commit ddd14f3a80df93e80982a3fd4791ec6cb193a50e
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:08:56 2020 +0800
Add js
commit 887d45217f404f50ba646356b355ef7df842655a
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:06:44 2020 +0800
Add style.css and modified index.html
# 假设要修改 'Add js' 为 'Add a js'
# 这里的hash要填其parent的hash
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rebase -i 887d45217f404f50
[detached HEAD 4678291] Add a js
Date: Tue Oct 6 22:08:56 2020 +0800
1 file changed, 14 insertions(+)
create mode 100644 js/script.js
Successfully rebased and updated refs/heads/master.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 9ee801d Move readme.md to readme
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log -n3 --graph
* commit 9ee801d013de3f1c10c711d18077bdf629194502 (HEAD -> master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 15:30:54 2020 +0800
|
| Move readme.md to readme
|
* commit 4678291e3b7c7ee4f2e23946269593f21ffc3b2a
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:08:56 2020 +0800
|
| Add a js
|
* commit 887d45217f404f50ba646356b355ef7df842655a
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:06:44 2020 +0800
|
| Add style.css and modified index.html
上述为交互页面
11.合并连续多个commit为一个
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 9ee801d Move readme.md to readme
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log --graph
* commit 9ee801d013de3f1c10c711d18077bdf629194502 (HEAD -> master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 15:30:54 2020 +0800
|
| Move readme.md to readme
|
* commit 4678291e3b7c7ee4f2e23946269593f21ffc3b2a
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:08:56 2020 +0800
|
| Add a js
|
* commit 887d45217f404f50ba646356b355ef7df842655a
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:06:44 2020 +0800
|
| Add style.css and modified index.html
|
* commit 1a103f45637ca22689a92a0f09ac438fcb327729
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:01:32 2020 +0800
|
| Add index + logo
|
* commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rebase -i 6debbe50d02c2054b7693a
[detached HEAD 42b3280] Create a complate web page
Date: Tue Oct 6 22:01:32 2020 +0800
6 files changed, 157 insertions(+)
create mode 100644 images/gh-branch.png
create mode 100644 images/gh-logo.png
create mode 100644 images/git.jpg
create mode 100644 index.html
create mode 100644 js/script.js
create mode 100644 styles/style.css
Successfully rebased and updated refs/heads/master.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log --graph
* commit c1cf8ef864de629cc9b77353dbe683b575563155 (HEAD -> master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 15:30:54 2020 +0800
|
| Move readme.md to readme
|
* commit 42b328065b9dd94a4c3242cc81d301342ccdf317
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:01:32 2020 +0800
|
| Create a complate web page
|
| Add index + logo
|
| Add style.css and modified index.html
|
| Add a js
|
* commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
合并前三个,必须pick其中一个
12.合并不连续多个commit
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log --graph
* commit c1cf8ef864de629cc9b77353dbe683b575563155 (HEAD -> master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Wed Oct 7 15:30:54 2020 +0800
|
| Move readme.md to readme
|
* commit 42b328065b9dd94a4c3242cc81d301342ccdf317
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:01:32 2020 +0800
|
| Create a complate web page
|
| Add index + logo
|
| Add style.css and modified index.html
|
| Add a js
|
* commit 6debbe50d02c2054b7693addc100a685c6a97cb9
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rebase -i 6debbe50d02c2054b
interactive rebase in progress; onto 6debbe5
Last command done (1 command done):
pick 6debbe5
Next commands to do (2 remaining commands):
squash c1cf8ef Move readme.md to readme
pick 42b3280 Create a complate web page
You are currently rebasing branch 'master' on '6debbe5'.
nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:
git commit --allow-empty
Otherwise, please use 'git reset'
Could not apply 6debbe5...
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master|REBASE-i 1/3)
$ git status
interactive rebase in progress; onto 6debbe5
Last command done (1 command done):
pick 6debbe5
Next commands to do (2 remaining commands):
squash c1cf8ef Move readme.md to readme
pick 42b3280 Create a complate web page
(use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on '6debbe5'.
(all conflicts fixed: run "git rebase --continue")
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master|REBASE-i 1/3)
$ git rebase --continue
[detached HEAD bdbfdd9] Add readme.md
Date: Tue Oct 6 21:33:58 2020 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme
Successfully rebased and updated refs/heads/master.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log --graph
* commit b53ed32f94f5c09c3639c1163fa7ddfcda07dd60 (HEAD -> master)
| Author: fire <zhaobaole520@gmail.com>
| Date: Tue Oct 6 22:01:32 2020 +0800
|
| Create a complate web page
|
| Add index + logo
|
| Add style.css and modified index.html
|
| Add a js
|
* commit bdbfdd9713d8c61dc27b95110650d38113e37e99
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme.md
Add readme
Move readme.md to readme
13.比较暂存区和HEAD两者所含文件的差异
命令:
$ git diff --cached
比较 暂存区与HEAD所含文件的差异
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
nothing to commit, working tree clean
# 说明工作区、暂存区、HEAD是一致的
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff --cached
diff --git a/index.html b/index.html
index 9dfbc09..d9cd4db 100644
--- a/index.html
+++ b/index.html
@@ -49,6 +49,7 @@
<div class="panel">
<h3>Once You Become a Contributor...</h3>
<ol>
+ <li>add</li>
<li>Clone Admin's (Your Partner) New Repository</li>
<li>Create a New Branch for this Repository</li>
<li>Make Some Change to Your New Branch</li>
@@ -74,4 +75,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git commit -m'Add the first git commmand with config'
[master 1720a74] Add the first git commmand with config
1 file changed, 2 insertions(+), 1 deletion(-)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git log
commit 1720a74980a99b3d536fb37d0c330342d1088e3f (HEAD -> master)
Author: fire <zhaobaole520@gmail.com>
Date: Thu Oct 8 09:01:08 2020 +0800
Add the first git commmand with config
commit b53ed32f94f5c09c3639c1163fa7ddfcda07dd60
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 22:01:32 2020 +0800
Create a complate web page
Add index + logo
Add style.css and modified index.html
Add a js
commit bdbfdd9713d8c61dc27b95110650d38113e37e99
Author: fire <zhaobaole520@gmail.com>
Date: Tue Oct 6 21:33:58 2020 +0800
Add readme.md
Add readme
Move readme.md to readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff --cached
# 现在已经无差异
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
14.比较工作区与暂存区两者所含文件的差异
命令
git diff
后可跟-- filename1 filename2 ...
查看具体文件的差异
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 8 08:58 ./
drwxr-xr-x 1 sj 197121 0 10月 8 09:05 ../
drwxr-xr-x 1 sj 197121 0 10月 8 09:01 .git/
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 images/
-rw-r--r-- 1 sj 197121 2279 10月 8 08:58 index.html
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 js/
-rw-r--r-- 1 sj 197121 0 10月 8 08:44 readme
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff
diff --git a/styles/style.css b/styles/style.css
index 5c6165b..6769d56 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
body{
background-color: #1D2031;
font-family: 'Ubuntu', sans-serif;
- color: white;
+ color: black;
}
body a{
@@ -63,4 +63,4 @@ div.accordion.active, div.accordion:hover {
div.panel {
padding: 0 18px 0 0;
display: none;
-}
\ No newline at end of file
+}
# index.html修改后添加到暂存区了,所以两者在index.html上无差异,在css文件上有差异
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff -- readme
# 两者在readme文件上无差异
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff -- styles/style.css
diff --git a/styles/style.css b/styles/style.css
index 5c6165b..6769d56 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
body{
background-color: #1D2031;
font-family: 'Ubuntu', sans-serif;
- color: white;
+ color: black;
}
body a{
@@ -63,4 +63,4 @@ div.accordion.active, div.accordion:hover {
div.panel {
padding: 0 18px 0 0;
display: none;
-}
\ No newline at end of file
+}
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff -- styles/style.css readme
diff --git a/styles/style.css b/styles/style.css
index 5c6165b..6769d56 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
body{
background-color: #1D2031;
font-family: 'Ubuntu', sans-serif;
- color: white;
+ color: black;
}
body a{
@@ -63,4 +63,4 @@ div.accordion.active, div.accordion:hover {
div.panel {
padding: 0 18px 0 0;
display: none;
-}
\ No newline at end of file
+}
15.Git的工作方式(理解二)
设置暂存区可以区分出哪些文件是改动过的,哪些未改动过
16.Git的工作方式(菜鸟教程)
- 工作区:就是你在电脑里能看到的目录。
- 暂存区:英文叫 stage 或 index。一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
- 版本库:工作区有一个隐藏目录 .git,这个不算工作区,而是 Git 的版本库。
- 图中左侧为工作区,右侧为版本库。在版本库中标记为 “index” 的区域是暂存区(stage/index),标记为 “master” 的是 master 分支所代表的目录树。
- 图中我们可以看出此时 “HEAD” 实际是指向 master 分支的一个”游标”。所以图示的命令中出现 HEAD 的地方可以用 master 来替换。
- 图中的 objects 标识的区域为 Git 的对象库,实际位于 “.git/objects” 目录下,里面包含了创建的各种对象及内容。
- 当对工作区修改(或新增)的文件执行 git add 命令时,暂存区的目录树被更新,同时工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中。
- 当执行提交操作(git commit)时,暂存区的目录树写到版本库(对象库)中,master 分支会做相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。
- 当执行 git reset HEAD 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响。
- 当执行 git rm —cached \
命令时,会直接从暂存区删除文件,工作区则不做出改变。 - 当执行 git checkout . 或者 git checkout — \
命令时,会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区的改动。 - 当执行 git checkout HEAD . 或者 git checkout HEAD \
命令时,会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。
17.Git clone项目
git clone 时,可以用不同的协议,包括 ssh, git, https 等,其中最常用的是 ssh,因为速度较快,还可以配置公钥免输入密码。各种写法如下:
$ git clone git@github.com:fsliurujie/test.git --SSH协议
$ git clone git://github.com/fsliurujie/test.git --GIT协议
$ git clone https://github.com/fsliurujie/test.git --HTTPS协议
18.Git 基本操作
Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比。
本章将对有关创建与提交你的项目快照的命令作介绍。
Git 常用的是以下 6 个命令:git clone、git push、git add 、git commit、git checkout、git pull
workspace:工作区
staging area:暂存区/缓存区
local repository:或本地仓库
remote repository:远程仓库
创建仓库命令
下表列出了 git 创建仓库的命令:
命令 | 说明 |
---|---|
git init |
初始化仓库 |
git clone |
拷贝一份远程仓库,也就是下载一个项目。 |
提交与修改
Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比。
下表列出了有关创建与提交你的项目的快照的命令:
命令 | 说明 |
---|---|
git add |
添加文件到仓库 |
git status |
查看仓库当前的状态,显示有变更的文件。 |
git diff |
比较文件的不同,即暂存区和工作区的差异。 |
git commit |
提交暂存区到本地仓库。 |
git reset |
回退版本。 |
git rm |
删除工作区文件。 |
git mv |
移动或重命名工作区文件。 |
提交日志
命令 | 说明 |
---|---|
git log |
查看历史提交记录 |
git blame <file> |
以列表形式查看指定文件的历史修改记录 |
远程操作
命令 | 说明 |
---|---|
git remote |
远程仓库操作 |
git fetch |
从远程获取代码库 |
git pull |
下载远程代码并合并 |
git push |
上传远程代码并合并 |
19.Git 分支管理
几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。
有人把 Git 的分支模型称为必杀技特性,而正是因为它,将 Git 从版本控制系统家族里区分出来。
创建分支命令:
git branch (branchname)
切换分支命令:
git checkout (branchname)
当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容! 所以多个分支不需要多个目录。
合并分支命令:
git merge
你可以多次合并到统一分支, 也可以选择在合并之后直接删除被并入的分支。
删除分支命令:
git branch -d branchName
20.Git 查看提交历史
Git 提交历史一般常用两个命令:
- git log - 查看历史提交记录。
- git blame \
- 以列表形式查看指定文件的历史修改记录。
git blame
如果要查看指定文件的修改记录可以使用 git blame 命令,格式如下:
$ git blame <file>
git blame 命令是以列表形式显示修改记录,如下实例:
$ git blame README
^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # Runoob Git 测试
db9315b0 (runoob 2020-08-25 16:00:23 +0800 2) # 菜鸟教程
21.Git 标签
如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。
比如说,我们想为我们的 runoob 项目发布一个”1.0”版本。 我们可以用 git tag -a v1.0 命令给最新一次提交打上(HEAD)”v1.0”的标签。
-a 选项意为”创建一个带注解的标签”。 不用 -a 选项也可以执行的,但它不会记录这标签是啥时候打的,谁打的,也不会让你添加个标签的注解。 我推荐一直创建带注解的标签。
$ git tag -a v1.0
当你执行 git tag -a 命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样。
现在,注意当我们执行 git log —decorate 时,我们可以看到我们的标签了:
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\
| * 7774248 (change_site) changed the runoob.php
* | c68142b 修改代码
|/
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 第一次版本提交
如果我们忘了给某个提交打标签,又将它发布了,我们可以给它追加标签。
例如,假设我们发布了提交 85fc7e7(上面实例最后一行),但是那时候忘了给它打标签。 我们现在也可以:
$ git tag -a v0.9 85fc7e7
$ git log --oneline --decorate --graph
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\
| * 7774248 (change_site) changed the runoob.php
* | c68142b 修改代码
|/
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 (tag: v0.9) 第一次版本提交
如果我们要查看所有标签可以使用以下命令:
$ git tag
v0.9
v1.0
指定标签信息命令:
git tag -a <tagname> -m "runoob.com标签"
PGP签名标签命令:
git tag -s <tagname> -m "runoob.com标签"
发布一个版本时,我们通常先在版本库中打一个标签(tag),这样就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。
所以,标签也是版本库的一个快照。
Git 的标签虽然是版本库的快照,但其实它就是指向某个 commit 的指针(跟分支很像对不对?但是分支可以移动,标签不能移动),所以,创建和删除标签都是瞬间完成的。
Git有commit,为什么还要引入tag?
“请把上周一的那个版本打包发布,commit号是6a5819e…”
“一串乱七八糟的数字不好找!”
如果换一个办法:
“请把上周一的那个版本打包发布,版本号是v1.2”
“好的,按照tag v1.2查找commit就行!”
所以,tag就是一个让人容易记住的有意义的名字,它跟某个commit绑在一起。
同大多数 VCS 一样,Git 也可以对某一时间点上的版本打上标签。人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做。
22.git reset(修改暂存区)
git reset 命令用于回退版本,可以指定退回某一次提交的版本。
git reset 命令语法格式如下:
git reset [--soft | --mixed | --hard] [HEAD]
—mixed 为默认,可以不用带该参数,用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变。
git reset [HEAD] [-- file1 file2 ...]
实例:
$ git reset HEAD^ # 回退所有内容到上一个版本
$ git reset HEAD^ hello.php # 回退 hello.php 文件的版本到上一个版本
$ git reset 052e # 回退到指定版本
—soft 参数用于回退到某个版本:
git reset --soft HEAD
实例:
$ git reset --soft HEAD~3 # 回退上上上一个版本
—hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交:
git reset --hard HEAD
实例:
$ git reset –hard HEAD~3 # 回退上上上一个版本
$ git reset –hard bae128 # 回退到某个版本回退点之前的所有信息。(删除之前的所有commit)
$ git reset --hard origin/master # 将本地的状态回退到和远程的一样
注意:谨慎使用 –hard 参数,它会删除回退点之前的所有信息。
HEAD 说明:
HEAD 表示当前版本
HEAD^ 上一个版本
HEAD^^ 上上一个版本
HEAD^^^ 上上上一个版本
- 以此类推…
可以使用 ~数字表示
- HEAD~0 表示当前版本
- HEAD~1 上一个版本
- HEAD^2 上上一个版本
- HEAD^3 上上上一个版本
- 以此类推…
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: readme
modified: styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add readme styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
modified: readme
modified: styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git reset HEAD
Unstaged changes after reset:
M index.html
M readme
M styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
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: index.html
modified: readme
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff --cached
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
简而言之,执行
git reset HEAD
以取消之前 git add 添加,但不希望包含在下一提交快照中
的缓存。
23.git checkout(修改工作区)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
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: index.html
modified: readme
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git add index.html
# 将较为满意的index.html提交到暂存区了
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi index.html
# 在工作区再次修改了index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff
diff --git a/index.html b/index.html
index d68781d..ea8a473 100644
--- a/index.html
+++ b/index.html
@@ -50,7 +50,7 @@
<h3>Once You Become a Contributor...</h3>
<ol>
<li>add</li>
- <li>bare repo</li>
+ <li>bare</li>
<li>Clone Admin's (Your Partner) New Repository</li>
<li>Create a New Branch for this Repository</li>
<li>Make Some Change to Your New Branch</li>
# 工作区index.html 与 暂存区index.html 不一样
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: index.html
modified: readme
modified: styles/style.css
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git checkout -- index.html
# 将工作区index.html恢复为暂存区的index.html的样子
# 之所以用“恢复”这个词,是因为暂存区的index.html本就是由工作区那个index.html添加过来的
# 也就是说,是工作区之前的index.html
# 将一个事物变为它之前曾有的模样,称为“恢复”
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff index.html
修改文件是在工作区修改,暂存区就是一个临时的存储空间,存储修改过的较为满意的文件,待此次改动整体完成后提交到版本库。
24.认识工作区与暂存区
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: readme
modified: styles/style.css
25.查看不同commit的差异
git diff commit1 commit2 [-- file1 file2 ...]
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff master temp
diff --git a/index.html b/index.html
index d9cd4db..9dfbc09 100644
--- a/index.html
+++ b/index.html
@@ -49,7 +49,6 @@
<div class="panel">
<h3>Once You Become a Contributor...</h3>
<ol>
- <li>add</li>
<li>Clone Admin's (Your Partner) New Repository</li>
<li>Create a New Branch for this Repository</li>
<li>Make Some Change to Your New Branch</li>
@@ -75,4 +74,4 @@
<script src="js/script.js"></script>
</body>
-</html>
+</html>
\ No newline at end of file
diff --git a/readme b/readme
index e69de29..db84973 100644
--- a/readme
+++ b/readme
@@ -0,0 +1 @@
+modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff master temp -- index.html
diff --git a/index.html b/index.html
index d9cd4db..9dfbc09 100644
--- a/index.html
+++ b/index.html
@@ -49,7 +49,6 @@
<div class="panel">
<h3>Once You Become a Contributor...</h3>
<ol>
- <li>add</li>
<li>Clone Admin's (Your Partner) New Repository</li>
<li>Create a New Branch for this Repository</li>
<li>Make Some Change to Your New Branch</li>
@@ -75,4 +74,4 @@
<script src="js/script.js"></script>
</body>
-</html>
+</html>
\ No newline at end of file
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git diff 1720a74 b9229fb -- index.html
diff --git a/index.html b/index.html
index d9cd4db..9dfbc09 100644
--- a/index.html
+++ b/index.html
@@ -49,7 +49,6 @@
<div class="panel">
<h3>Once You Become a Contributor...</h3>
<ol>
- <li>add</li>
<li>Clone Admin's (Your Partner) New Repository</li>
<li>Create a New Branch for this Repository</li>
<li>Make Some Change to Your New Branch</li>
@@ -75,4 +74,4 @@
<script src="js/script.js"></script>
</body>
-</html>
+</html>
\ No newline at end of file
26.删除文件
$ git rm filename
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 ./
drwxr-xr-x 1 sj 197121 0 10月 8 15:17 ../
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 .git/
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 images/
-rw-r--r-- 1 sj 197121 2279 10月 8 15:22 index.html
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 js/
-rw-r--r-- 1 sj 197121 0 10月 8 15:22 readme
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ rm readme
# 修改了工作区中的内容(但没将修改添加到暂存区,但此次修改是一项“删除”操作)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rm readme
rm 'readme'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git reset --hard HEAD
HEAD is now at 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 8 15:23 ./
drwxr-xr-x 1 sj 197121 0 10月 8 15:17 ../
drwxr-xr-x 1 sj 197121 0 10月 8 15:23 .git/
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 images/
-rw-r--r-- 1 sj 197121 2279 10月 8 15:22 index.html
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 js/
-rw-r--r-- 1 sj 197121 0 10月 8 15:23 readme
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git rm readme
rm 'readme'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: readme
27.暂存“修改”
stash 藏匿
将“修改”放在一个类似堆栈的地方,使工作区保持干净,去做更急的修改,完后再取出继续。
$ git stash apply # 复制 栈顶首元素 到工作区
$ git stash pop # 弹出 栈顶首元素 到工作区
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
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: readme
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash
Saved working directory and index state WIP on master: 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash list
stash@{0}: WIP on master: 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash apply
On branch master
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: readme
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash list
stash@{0}: WIP on master: 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git reset --hard HEAD
HEAD is now at 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash list
stash@{0}: WIP on master: 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash pop
On branch master
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: readme
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (952831ccd94b1e354bc3b91caf57ffe8a64377da)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git stash list
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
28.文件.gitignore
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 12
drwxr-xr-x 1 sj 197121 0 10月 8 16:16 ./
drwxr-xr-x 1 sj 197121 0 10月 8 16:06 ../
drwxr-xr-x 1 sj 197121 0 10月 8 16:16 .git/
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 images/
-rw-r--r-- 1 sj 197121 2279 10月 8 15:22 index.html
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 js/
-rw-r--r-- 1 sj 197121 0 10月 8 16:16 readme
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ mkdir doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ echo 'hi' > doc/readhim
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
nothing added to commit but untracked files present (use "git add" to track)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi .gitignore
# .gitignore 内容为 doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
# 可见git忽略了doc这个文件夹
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi .gitignore
# .gitignore 内容为 doc/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
# 可见git也是忽略了doc这个文件夹
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ rm -rf doc
# 删除了doc文件夹
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ echo 'I am a file' > doc
# 新建了doc文件
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ ls -al
total 14
drwxr-xr-x 1 sj 197121 0 10月 8 16:20 ./
drwxr-xr-x 1 sj 197121 0 10月 8 16:06 ../
drwxr-xr-x 1 sj 197121 0 10月 8 16:18 .git/
-rw-r--r-- 1 sj 197121 5 10月 8 16:18 .gitignore
-rw-r--r-- 1 sj 197121 12 10月 8 16:20 doc
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 images/
-rw-r--r-- 1 sj 197121 2279 10月 8 15:22 index.html
drwxr-xr-x 1 sj 197121 0 10月 8 08:45 js/
-rw-r--r-- 1 sj 197121 0 10月 8 16:16 readme
drwxr-xr-x 1 sj 197121 0 10月 8 15:22 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cat .gitignore
doc/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
doc
nothing added to commit but untracked files present (use "git add" to track)
# git没有忽略doc文件,因为.gitignore里是doc/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ vi .gitignore
# 改回 doc
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
# 忽略了doc文件
总结:
.gitignore
文件写doc
时,会忽略doc文件与doc文件夹,但若是写doc/
,只忽略doc文件夹。
29.Git的备份
备份:将数据从此处传输到彼处。
传输用到一些传输协议。
哑协议与智能协议:
直观区别:哑协议传输进度不可见;智能协议传输进度可见。
传输速度:智能协议(压缩了)比哑协议传输速度快。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol
$ pwd
/e/FrontEnd/LearnGit/protocol
# --bare在这里是指克隆不带工作区的裸仓库
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol
$ git clone --bare /e/FrontEnd/LearnGit/Project_code_is_there/.git ya.git
Cloning into bare repository 'ya.git'...
done.
# 哑协议传输进度不可见
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol
$ git clone --bare file:///e/FrontEnd/LearnGit/Project_code_is_there/.git zhineng.git
Cloning into bare repository 'zhineng.git'...
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 30 (delta 6), reused 0 (delta 0)
Receiving objects: 100% (30/30), 26.02 KiB | 1.45 MiB/s, done.
Resolving deltas: 100% (6/6), done.
# 智能协议传输进度可见
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote -v
zhineng E:/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng E:/FrontEnd/LearnGit/protocol/zhineng.git (push)
zhineng01 file:///e/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng01 file:///e/FrontEnd/LearnGit/protocol/zhineng.git (push)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote rm zhineng zhineng01
usage: git remote remove <name>
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote -v
zhineng E:/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng E:/FrontEnd/LearnGit/protocol/zhineng.git (push)
zhineng01 file:///e/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng01 file:///e/FrontEnd/LearnGit/protocol/zhineng.git (push)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote rm zhineng
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote rm zhineng01
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote -v
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote add zhineng file:///e/FrontEnd/LearnGit/protocol/zhineng.git
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol/zhineng.git (BARE:master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol/zhineng.git (BARE:master)
$ git branch -d zbl
Deleted branch zbl (was 1720a74).
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol/zhineng.git (BARE:master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol/zhineng.git (BARE:master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/protocol/zhineng.git (BARE:master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git push zhineng
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream zhineng master
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git push --set-upstream zhineng master
Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'zhineng'.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git push --set-upstream zhineng zbl
Total 0 (delta 0), reused 0 (delta 0)
To file:///e/FrontEnd/LearnGit/protocol/zhineng.git
* [new branch] zbl -> zbl
Branch 'zbl' set up to track remote branch 'zbl' from 'zhineng'.
30.一点发现
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cd ~/.ssh
sj@fireworks99 MINGW64 ~/.ssh
$ ls -al
total 33
drwxr-xr-x 1 sj 197121 0 9月 9 2019 ./
drwxr-xr-x 1 sj 197121 0 10月 8 17:58 ../
-rw-r--r-- 1 sj 197121 1823 9月 9 2019 id_rsa
-rw-r--r-- 1 sj 197121 399 9月 9 2019 id_rsa.pub
-rw-r--r-- 1 sj 197121 1197 10月 9 2019 known_hosts
sj@fireworks99 MINGW64 ~/.ssh
$ pwd
/c/Users/sj/.ssh
所以说
~
代表当前用户根目录,即/c/Users/sj
31.本地仓库同步到github
git remote add [仓库标识名] [仓库地址]
git push [仓库标识名] [—all / branchName]
git fetch [仓库标识名] [—all / branchName]
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote -v
zhineng file:///e/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng file:///e/FrontEnd/LearnGit/protocol/zhineng.git (push)
# zhineng 是本地的远端仓库
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote add github git@github.com:fireworks99/Project_code_is_there.git
# 这里的'github'只是一个同'zhineng'一样的仓库名,它代表了它后面那个地址
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git remote -v
github git@github.com:fireworks99/Project_code_is_there.git (fetch)
github git@github.com:fireworks99/Project_code_is_there.git (push)
zhineng file:///e/FrontEnd/LearnGit/protocol/zhineng.git (fetch)
zhineng file:///e/FrontEnd/LearnGit/protocol/zhineng.git (push)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git push github --all
Enumerating objects: 30, done.
Counting objects: 100% (30/30), done.
Delta compression using up to 4 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (30/30), 26.02 KiB | 1.86 MiB/s, done.
Total 30 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), done.
To github.com:fireworks99/Project_code_is_there.git
* [new branch] master -> master
* [new branch] temp -> temp
* [new branch] zbl -> zbl
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ gitk --all
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git fetch github main
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), 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), done.
From github.com:fireworks99/Project_code_is_there
* branch main -> FETCH_HEAD
* [new branch] main -> github/main
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ gitk --all
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 1720a74 Add the first git commmand with config
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/main 9d57f1f Initial commit
remotes/github/master 1720a74 Add the first git commmand with config
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git merge github/main
fatal: refusing to merge unrelated histories
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git merge --allow-unrelated-histories github/main
Merge made by the 'recursive' strategy.
LICENSE | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 LICENSE
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ gitk --all
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git push github master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 308 bytes | 308.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
1720a74..9065e0f master -> master
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ gitk --all
32.多个git客户端工作在同一分支上
(1)不同人修改了不同文件(fast-forward)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ cd ..
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ git clone git@github.com:fireworks99/Project_code_is_there.git Project_code_is_there_02
Cloning into 'Project_code_is_there_02'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 35 (delta 7), reused 32 (delta 7), pack-reused 0
Receiving objects: 100% (35/35), 27.44 KiB | 121.00 KiB/s, done.
Resolving deltas: 100% (7/7), done.
# 另一个开发人员fireworks99克隆同一个项目(Project_code_is_there)到自己本地
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ ls -al
total 112
drwxr-xr-x 1 sj 197121 0 10月 9 10:01 ./
drwxr-xr-x 1 sj 197121 0 9月 30 19:42 ../
-rw-r--r-- 1 sj 197121 91237 10月 9 09:46 Git.md
drwxr-xr-x 1 sj 197121 0 10月 8 16:40 img/
drwxr-xr-x 1 sj 197121 0 10月 9 09:17 Project_code_is_there/
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 Project_code_is_there_02/
drwxr-xr-x 1 sj 197121 0 10月 8 17:44 protocol/
drwxr-xr-x 1 sj 197121 0 10月 6 21:54 virtual/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit
$ cd Project_code_is_there_02/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (master)
$ git config --add --local user.name 'fireworks99'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (master)
$ git config --add --local user.email '2639237361@qq.com'
# fireworks99自己的git中的config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (master)
$ git config --local -l
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=git@github.com:fireworks99/Project_code_is_there.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=fireworks99
user.email=2639237361@qq.com
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (master)
$ git branch -av
* master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (master)
$ git checkout -b features/add_git_commands origin/features/add_git_commands
Switched to a new branch 'features/add_git_commands'
Branch 'features/add_git_commands' set up to track remote branch 'features/add_git_commands' from 'origin'.
# 基于远端 origin/features/add_git_commands 分支
# 创建
# 本地分支 features/add_git_commands
# 并切换到此分支上
# 这里git会把两者关联到一起,以后git push都不用带参数,含默认值
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git branch -av
* features/add_git_commands 9065e0f Merge remote-tracking branch 'github/main'
master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ vi readme
# 在工作区修改了readme文件
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git add -u
# 工作区修改的文件添加到暂存区
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git status
On branch features/add_git_commands
Your branch is up to date with 'origin/features/add_git_commands'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git commit -m'Modified readme'
[features/add_git_commands fc5c038] Modified readme
1 file changed, 1 insertion(+)
# 暂存区的内容提交到本地版本库
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 40.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
9065e0f..fc5c038 features/add_git_commands -> features/add_git_commands
# 本地版本库push到项目版本库
# 另一个开发人员fire也要修改此项目内容(index.html)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git config --local -l
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
user.name=fire
user.email=zhaobaole520@gmail.com
remote.zhineng.url=file:///e/FrontEnd/LearnGit/protocol/zhineng.git
remote.zhineng.fetch=+refs/heads/*:refs/remotes/zhineng/*
branch.master.remote=zhineng
branch.master.merge=refs/heads/master
branch.zbl.remote=zhineng
branch.zbl.merge=refs/heads/zbl
remote.github.url=git@github.com:fireworks99/Project_code_is_there.git
remote.github.fetch=+refs/heads/*:refs/remotes/github/*
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 9065e0f [ahead 2] Merge remote-tracking branch 'github/main'
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/main 9d57f1f Initial commit
remotes/github/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
From github.com:fireworks99/Project_code_is_there
* [new branch] features/add_git_commands -> github/features/add_git_commands
# 把该项目新建的分支fetch一下,作用如下:
# 1.创建并更新本地远程分支(remotes/github/features/add_git_commands)
# 2.设定当前分支的 FETCH_HEAD(为远程服务器的features/add_git_commands分支)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git branch -av
* master 9065e0f [ahead 2] Merge remote-tracking branch 'github/main'
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/features/add_git_commands fc5c038 Modified readme
remotes/github/main 9d57f1f Initial commit
remotes/github/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (master)
$ git checkout -b features/add_git_commands github/features/add_git_commands
Switched to a new branch 'features/add_git_commands'
Branch 'features/add_git_commands' set up to track remote branch 'features/add_git_commands' from 'github'.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git branch -av
* features/add_git_commands fc5c038 Modified readme
master 9065e0f [ahead 2] Merge remote-tracking branch 'github/main'
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/features/add_git_commands fc5c038 Modified readme
remotes/github/main 9d57f1f Initial commit
remotes/github/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
# 这里可以看到
# 本地的add_git_commands分支后面对应的hash值
# 与
# 远端的add_git_commands分支后面对应的hash值
# 相同(皆为fc5c038),说明他们指向相同的东西
# 此时fireworks99执行push是没问题的
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git add -u
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git commit -m'Modified index.html'
[features/add_git_commands 318cfc4] Modified index.html
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ gitk --all
本地的 fast-forward 的commit (Modified readme的那次commit)
与
远端的 fast-forward 的commit (Modified readme的那次commit)
一致
# 此时fireworks99执行push是没问题的,但倘若在此之前fire有新的push了:
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ vi readme
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git add -u
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git commit -m'Modified readme agein'
[features/add_git_commands 228e8d3] Modified readme agein
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 294 bytes | 98.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
fc5c038..228e8d3 features/add_git_commands -> features/add_git_commands
# 最后一行我们看到 fast-forward 的commit的hash 由 fc5c038 变为 228e8d3
再看fireworks99视角:
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git branch -av
* features/add_git_commands 318cfc4 [ahead 1] Modified index.html
master 9065e0f [ahead 2] Merge remote-tracking branch 'github/main'
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/features/add_git_commands fc5c038 Modified readme
remotes/github/main 9d57f1f Initial commit
remotes/github/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
# 本地的 fast-forward 的commit 还是Modified readme的那次(fc5c038)
# 他不知道 fire 提交了新的修改
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ gitk --all
本地的 fast-forward 的commit (Modified readme的那次commit)
与
远端的 fast-forward 的commit (Modified readme agein 的那次commit)
不一致
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git push github
To github.com:fireworks99/Project_code_is_there.git
! [rejected] features/add_git_commands -> features/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:fireworks99/Project_code_is_there.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.
# git push 报错
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:fireworks99/Project_code_is_there
fc5c038..228e8d3 features/add_git_commands -> github/features/add_git_commands
# 开发人员fireworks99再次fetch的时候发现fast-forward的commit变更了
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git branch -av
* features/add_git_commands 318cfc4 [ahead 1, behind 1] Modified index.html
master 9065e0f [ahead 2] Merge remote-tracking branch 'github/main'
temp b9229fb Add modefied
zbl 1720a74 Add the first git commmand with config
remotes/github/features/add_git_commands 228e8d3 Modified readme agein
remotes/github/main 9d57f1f Initial commit
remotes/github/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/github/temp b9229fb Add modefied
remotes/github/zbl 1720a74 Add the first git commmand with config
remotes/zhineng/master 1720a74 Add the first git commmand with config
remotes/zhineng/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git merge github/features/add_git_commands
Merge made by the 'recursive' strategy.
readme | 1 +
1 file changed, 1 insertion(+)
# 合并
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ vi readme
# 这里,fire对readme的两次修改我都看到了
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git push github
Enumerating objects: 9, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 567 bytes | 141.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To github.com:fireworks99/Project_code_is_there.git
228e8d3..56e46e0 features/add_git_commands -> features/add_git_commands
# 此时
# 本地的 fast-forward 的commit (Modified readme again)
# 与
# 远端的 fast-forward 的commit (Modified readme again)
# 一致
# 再次push就没问题了
(2)不同人修改了同文件不同区域(merge)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git branch -av
* features/add_git_commands 228e8d3 Modified readme agein
master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 228e8d3 Modified readme agein
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git pull
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:fireworks99/Project_code_is_there
228e8d3..56e46e0 features/add_git_commands -> origin/features/add_git_commands
Updating 228e8d3..56e46e0
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git branch -av
* features/add_git_commands 56e46e0 Merge remote-tracking branch 'github/features/add_git_commands' into features/add_git_commands
master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 56e46e0 Merge remote-tracking branch 'github/features/add_git_commands' into features/add_git_commands
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
开始两人都修改了index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git commit -am'Add non fast-forward command'
[features/add_git_commands c01bad1] Add non fast-forward command
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git commit -m'Add commit command'
On branch features/add_git_commands
Your branch is up to date with 'github/features/add_git_commands'.
Changes not staged for commit:
modified: index.html
no changes added to commit
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git commit -am'Add commit command'
[features/add_git_commands 3d073bf] Add commit command
1 file changed, 1 insertion(+)
fire先push了
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git push github
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 295 bytes | 42.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:fireworks99/Project_code_is_there.git
56e46e0..3d073bf features/add_git_commands -> features/add_git_commands
fireworks99去push时遭拒
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
To github.com:fireworks99/Project_code_is_there.git
! [rejected] features/add_git_commands -> features/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:fireworks99/Project_code_is_there.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.
fireworks99两个选择
(1)git pull
(2)git fetch + merge
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:fireworks99/Project_code_is_there
56e46e0..3d073bf features/add_git_commands -> origin/features/add_git_commands
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git branch -av
* features/add_git_commands c01bad1 [ahead 1, behind 1] Add non fast-forward command
master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 3d073bf Add commit command
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
# c01bad1 [ahead 1, behind 1] 是说当前的工作分支比远端多一个commit同时又少一个commit
$ git merge origin/features/add_git_commands 即可
(3)不同人修改了同文件同一区域(conflict)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git merge origin/features/add_git_commands
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git status
On branch features/add_git_commands
Your branch and 'origin/features/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git commit -am'Resolved conflict by hand'
[features/add_git_commands 1aa6467] Resolved conflict by hand
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git status
On branch features/add_git_commands
Your branch is ahead of 'origin/features/add_git_commands' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ gitk --all
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 586 bytes | 48.00 KiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:fireworks99/Project_code_is_there.git
3d073bf..1aa6467 features/add_git_commands -> features/add_git_commands
总结:手动修改冲突处即可。(与同事商量确认保留还是增删)
(4)一人改文件名一人改内容(git pull)
git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。
$ git pull <远程主机名> <远程分支名>:<本地分支名>
# 比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。
$ git pull origin next:master
# 如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
$ git pull origin next
# 上面命令表示,取回origin/next分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。
$ git fetch origin
$ git merge origin/next
# 在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动”追踪”origin/master分支。
# Git也允许手动建立追踪关系。
git branch --set-upstream master origin/next
# 上面命令指定master分支追踪origin/next分支。
# 如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名。
$ git pull origin
# 上面命令表示,本地的当前分支自动与对应的origin主机”追踪分支”(remote-tracking branch)进行合并。
# 如果当前分支只有一个追踪分支,连远程主机名都可以省略。
fireworks99 修改文件名
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git pull
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0
Unpacking objects: 100% (6/6), done.
From github.com:fireworks99/Project_code_is_there
3d073bf..1aa6467 features/add_git_commands -> github/features/add_git_commands
Updating 3d073bf..1aa6467
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git mv index.html index.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git status
On branch features/add_git_commands
Your branch is up to date with 'github/features/add_git_commands'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: index.html -> index.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git commit -am'Mv index.html to index.htm'
[features/add_git_commands 55718cb] Mv index.html to index.htm
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.html => index.htm (100%)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git push github
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 226 bytes | 32.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
1aa6467..55718cb features/add_git_commands -> features/add_git_commands
fire修改文件内容
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git pull
Already up to date.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ vi index.html
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git commit -am'Modified index.html add empty li tag'
[features/add_git_commands 8255c38] Modified index.html add empty li tag
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
To github.com:fireworks99/Project_code_is_there.git
! [rejected] features/add_git_commands -> features/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:fireworks99/Project_code_is_there.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.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
Unpacking objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
From github.com:fireworks99/Project_code_is_there
1aa6467..55718cb features/add_git_commands -> origin/features/add_git_commands
Merge made by the 'recursive' strategy.
index.html => index.htm | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.html => index.htm (100%)
# git pull很智能,一步到位。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_comma
$ ls -al
total 21
drwxr-xr-x 1 sj 197121 0 10月 9 14:26 ./
drwxr-xr-x 1 sj 197121 0 10月 9 14:11 ../
drwxr-xr-x 1 sj 197121 0 10月 9 14:27 .git/
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 images/
-rw-r--r-- 1 sj 197121 2367 10月 9 14:26 index.htm
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 js/
-rw-r--r-- 1 sj 197121 1068 10月 9 10:02 LICENSE
-rw-r--r-- 1 sj 197121 33 10月 9 11:00 readme
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ vi index.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 611 bytes | 61.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To github.com:fireworks99/Project_code_is_there.git
55718cb..a8d3780 features/add_git_commands -> features/add_git_commands
(5)两人将同一个文件改为不同的文件名
fireworks99 将 index.htm改为index1.htm,并更早提交。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git pull
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:fireworks99/Project_code_is_there
55718cb..a8d3780 features/add_git_commands -> github/features/add_git_commands
Updating 55718cb..a8d3780
Fast-forward
index.htm | 1 +
1 file changed, 1 insertion(+)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git mv index.htm index1.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git commit -am 'Mv index to index1'
[features/add_git_commands 7fc9418] Mv index to index1
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.htm => index1.htm (100%)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there (features/add_git_commands)
$ git push github
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 227 bytes | 28.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
a8d3780..7fc9418 features/add_git_commands -> features/add_git_commands
fire将index.htm修改为index2.htm,后提交。
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git pull
Already up to date.
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git mv index.htm index2.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git commit -am 'Mv index to index2'
[features/add_git_commands a765e76] Mv index to index2
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.htm => index2.htm (100%)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
To github.com:fireworks99/Project_code_is_there.git
! [rejected] features/add_git_commands -> features/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:fireworks99/Project_code_is_there.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.
# 遭拒绝
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From github.com:fireworks99/Project_code_is_there
a8d3780..7fc9418 features/add_git_commands -> origin/features/add_git_commands
CONFLICT (rename/rename): Rename "index.htm"->"index2.htm" in branch "HEAD" rename "index.htm"->"index1.htm" in "7fc941898258649d0a407e79532d5437aff389d7"
Automatic merge failed; fix conflicts and then commit the result.
# pull的时候发送合并冲突(让开发人员自己解决)
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ ls -al
total 25
drwxr-xr-x 1 sj 197121 0 10月 9 14:56 ./
drwxr-xr-x 1 sj 197121 0 10月 9 14:11 ../
drwxr-xr-x 1 sj 197121 0 10月 9 14:56 .git/
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 images/
-rw-r--r-- 1 sj 197121 2367 10月 9 14:56 index1.htm
-rw-r--r-- 1 sj 197121 2367 10月 9 14:56 index2.htm
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 js/
-rw-r--r-- 1 sj 197121 1068 10月 9 10:02 LICENSE
-rw-r--r-- 1 sj 197121 33 10月 9 11:00 readme
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ diff index1.htm index2.htm
# 两文件无差别
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git status
On branch features/add_git_commands
Your branch and 'origin/features/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add/rm <file>..." as appropriate to mark resolution)
both deleted: index.htm
added by them: index1.htm
added by us: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git rm index.htm
index.htm: needs merge
rm 'index.htm'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git status
On branch features/add_git_commands
Your branch and 'origin/features/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
added by them: index1.htm
added by us: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git add index1.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git status
On branch features/add_git_commands
Your branch and 'origin/features/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: index1.htm
Unmerged paths:
(use "git add <file>..." to mark resolution)
added by us: index2.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git rm index2.htm
index2.htm: needs merge
rm 'index2.htm'
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git status
On branch features/add_git_commands
Your branch and 'origin/features/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
renamed: index2.htm -> index1.htm
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ ls -al
total 21
drwxr-xr-x 1 sj 197121 0 10月 9 15:00 ./
drwxr-xr-x 1 sj 197121 0 10月 9 14:11 ../
drwxr-xr-x 1 sj 197121 0 10月 9 15:00 .git/
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 images/
-rw-r--r-- 1 sj 197121 2367 10月 9 14:56 index1.htm
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 js/
-rw-r--r-- 1 sj 197121 1068 10月 9 10:02 LICENSE
-rw-r--r-- 1 sj 197121 33 10月 9 11:00 readme
drwxr-xr-x 1 sj 197121 0 10月 9 10:02 styles/
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands|MERGING)
$ git commit -am 'Decide to mv index to index1'
[features/add_git_commands 0abb8e7] Decide to mv index to index1
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git status
On branch features/add_git_commands
Your branch is ahead of 'origin/features/add_git_commands' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ gitk --all
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git branch -av
* features/add_git_commands 0abb8e7 [ahead 2] Decide to mv index to index1
master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/HEAD -> origin/master
remotes/origin/features/add_git_commands 7fc9418 Mv index to index1
remotes/origin/main 9d57f1f Initial commit
remotes/origin/master 9065e0f Merge remote-tracking branch 'github/main'
remotes/origin/temp b9229fb Add modefied
remotes/origin/zbl 1720a74 Add the first git commmand with config
sj@fireworks99 MINGW64 /e/FrontEnd/LearnGit/Project_code_is_there_02 (features/add_git_commands)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 418 bytes | 46.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:fireworks99/Project_code_is_there.git
7fc9418..0abb8e7 features/add_git_commands -> features/add_git_commands
(6)禁止向集成分支执行变更历史的操作
就是不要动别人的分支,你要修改,就在最新的commit之上提交你的commit(在集成主线上commit)。