学习视频:https://www.bilibili.com/video/av24441039
学习资料:
https://pan.baidu.com/s/18K0J7bclW8HApckKq9FiOw
提取码:bw2n
1、本地库初始化
-
命令:
git init
-
操作:在文件夹下右键->git bash->git init
-
效果:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git init Reinitialized existing Git repository in E:/GitRepositories/00.test/.git/ Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ ll .git/ total 7 -rw-r--r-- 1 Administrator 197121 130 12月 26 22:10 config -rw-r--r-- 1 Administrator 197121 73 12月 26 22:08 description -rw-r--r-- 1 Administrator 197121 23 12月 26 22:08 HEAD drwxr-xr-x 1 Administrator 197121 0 12月 26 22:08 hooks/ drwxr-xr-x 1 Administrator 197121 0 12月 26 22:08 info/ drwxr-xr-x 1 Administrator 197121 0 12月 26 22:08 objects/ drwxr-xr-x 1 Administrator 197121 0 12月 26 22:08 refs/ Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $
-
注意: .git目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改。
2、设置签名
3、查看状态,提交,更新
-
命令:
git status git add git commit good.txt--进入vim进行描述 git commit -m "描述" good.txt
-
一次提交操作:
创建文件:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ vim good1.txt
查看:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) good1.txt nothing added to commit but untracked files present (use "git add" to track)
将文件添加到暂存区域:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git add good1.txt warning: LF will be replaced by CRLF in good1.txt. The file will have its original line endings in your working directory
查看:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: good1.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
将文件提交到本地库:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git commit -m "new file good1.txt" good1.txt warning: LF will be replaced by CRLF in good1.txt. The file will have its original line endings in your working directory [master 07150a2] new file good1.txt 1 file changed, 1244 insertions(+) create mode 100644 good1.txt
-
将good1.txt修改后操作:
查看:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (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: good1.txt no changes added to commit (use "git add" and/or "git commit -a")
将修改的文件加入暂存区:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git add good1.txt warning: LF will be replaced by CRLF in good1.txt. The file will have its original line endings in your working directory
将修改的文件提交到工作区
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git commit -m "modify good1.txt" good1.txt warning: LF will be replaced by CRLF in good1.txt. The file will have its original line endings in your working directory [master 4fc9548] modify good1.txt 1 file changed, 1 insertion(+)
查看
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git status On branch master nothing to commit, working tree clean
4、基本操作
查看状态
提交
更新
查看日志:git log
git log
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git log
commit 4fc9548d0b1d5fb97decd4cb1b9f82c3918bf501 (HEAD -> master)
Author: 00test <276418033@qq.com>
Date: Thu Dec 26 23:48:26 2019 +0800
modify good1.txt
commit 07150a24d094a7e72920b72c5e68404a3cb55ea7
Author: 00test <276418033@qq.com>
Date: Thu Dec 26 23:41:35 2019 +0800
new file good1.txt
commit 9e35b905d8726272ac4226d662199cdd15706f4b
Author: 00test <276418033@qq.com>
Date: Thu Dec 26 23:14:10 2019 +0800
My first commit new file good.txt
git log --pretty=oneline
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git log --pretty=oneline
4fc9548d0b1d5fb97decd4cb1b9f82c3918bf501 (HEAD -> master) modify good1.txt
07150a24d094a7e72920b72c5e68404a3cb55ea7 new file good1.txt
9e35b905d8726272ac4226d662199cdd15706f4b My first commit new file good.txt
git log --oneline
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git log --oneline
4fc9548 (HEAD -> master) modify good1.txt
07150a2 new file good1.txt
9e35b90 My first commit new file good.txt
git reflog
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git reflog
4fc9548 (HEAD -> master) HEAD@{0}: commit: modify good1.txt
07150a2 HEAD@{1}: commit: new file good1.txt
9e35b90 HEAD@{2}: commit (initial): My first commit new file good.txt
前进后退
- 本质
-
基于索引值操作(推荐)
- 命令
git reflog git reset --hard 索引
先使用: git reflog 查询版本的索引
然后再使用: git reset --hard 索引
后退:退回到之前的版本,指针向下
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git reflog 7f36ad2 (HEAD -> master) HEAD@{0}: commit: modify good.txt:c 6530c31 HEAD@{1}: commit: modify good.txt:b c70cb17 HEAD@{2}: commit: modify good.txt:a 4fc9548 HEAD@{3}: commit: modify good1.txt 07150a2 HEAD@{4}: commit: new file good1.txt 9e35b90 HEAD@{5}: commit (initial): My first commit new file good.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ cat good.txt aaaaaaaa bbbbbbbb cccccccc Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git reset --hard 6530c31 HEAD is now at 6530c31 modify good.txt:b Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ cat good.txt aaaaaaaa bbbbbbbb
前进:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git reflog 6530c31 (HEAD -> master) HEAD@{0}: reset: moving to 6530c31 7f36ad2 HEAD@{1}: commit: modify good.txt:c 6530c31 (HEAD -> master) HEAD@{2}: commit: modify good.txt:b c70cb17 HEAD@{3}: commit: modify good.txt:a 4fc9548 HEAD@{4}: commit: modify good1.txt 07150a2 HEAD@{5}: commit: new file good1.txt 9e35b90 HEAD@{6}: commit (initial): My first commit new file good.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ cat good.txt aaaaaaaa bbbbbbbb Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git reset --hard 7f36ad2 HEAD is now at 7f36ad2 modify good.txt:c Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ cat good.txt aaaaaaaa bbbbbbbb cccccccc
-
使用^符号:只能后退
- 命令:git reset --hard HEAD^^
- :一个符号表示后退一步;n个表示后退n步。
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git log --oneline
7f36ad2 (HEAD -> master) modify good.txt:c
6530c31 modify good.txt:b
c70cb17 modify good.txt:a
4fc9548 modify good1.txt
07150a2 new file good1.txt
9e35b90 My first commit new file good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git reset --hard HEAD^^
HEAD is now at c70cb17 modify good.txt:a
- 使用~符号
- 命令:git reset --hard HEAD~1
- ~符号后面的数字表示后退几步
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git reset --hard HEAD~1
HEAD is now at 4fc9548 modify good1.txt
reset命令的三个参数对比
- –soft:撤销当前commit到本地库的操作。
- 仅在本地库移动HEAD指针
- –mixed:撤销当前commit到本地库的操作并重置暂存区。
- 在本地库移动HEAD指针
- 重置暂存区
- –hard:
- 在本地库移动HEAD指针
- 重置暂存区
- 重置工作区
git reset –mixed
:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,保留本地源码,只是回退commit和index信息。此时stage区状态没有保留。
git reset –soft
:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可。如果commit后,改错了文件,此时执行这个命令,stage会回退到提交之前的状态,修改后再次commit即可。
git reset –hard
:彻底回退到某个版本,本地的源码也会变为上一个版本的内容。
永久删除文件并找回
-
创建一个新文件并提交到本地库:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ vim deleteFile.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) deleteFile.txt nothing added to commit but untracked files present (use "git add" to track) Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git add deleteFile.txt warning: LF will be replaced by CRLF in deleteFile.txt. The file will have its original line endings in your working directory Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git commit -m"new deleteFile.txt" deleteFile.txt warning: LF will be replaced by CRLF in deleteFile.txt. The file will have its original line endings in your working directory [master 0a1b3cb] new deleteFile.txt 1 file changed, 3 insertions(+) create mode 100644 deleteFile.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git status On branch master nothing to commit, working tree clean
-
删除工作区的deleteFile.txt文件
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ rm deleteFile.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ ll
total 17
-rw-r--r-- 1 Administrator 197121 30 12月 27 16:07 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
- 将删除操作提交到本地库
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (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: deleteFile.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git add deleteFile.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git commit -m"delete file" deleteFile.txt
[master bc71e8c] delete file
1 file changed, 3 deletions(-)
delete mode 100644 deleteFile.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git status
On branch master
nothing to commit, working tree clean
- 再找回删除的文件:直接回退到删除之前的版本即可
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git reflog
bc71e8c (HEAD -> master) HEAD@{0}: commit: delete file
0a1b3cb HEAD@{1}: commit: new deleteFile.txt
74ba4b6 HEAD@{2}: commit: modify
6530c31 HEAD@{3}: reset: moving to 6530c31
376c6e1 HEAD@{4}: commit: modify
c70cb17 HEAD@{5}: reset: moving to c70cb17
21928ca HEAD@{6}: reset: moving to HEAD
21928ca HEAD@{7}: commit: modify
6530c31 HEAD@{8}: reset: moving to 6530c31
7f36ad2 HEAD@{9}: reset: moving to 7f36ad2
4fc9548 HEAD@{10}: reset: moving to HEAD~1
c70cb17 HEAD@{11}: reset: moving to HEAD^^
7f36ad2 HEAD@{12}: reset: moving to 7f36ad2
6530c31 HEAD@{13}: reset: moving to 6530c31
6530c31 HEAD@{14}: reset: moving to 6530c31
7f36ad2 HEAD@{15}: commit: modify good.txt:c
6530c31 HEAD@{16}: commit: modify good.txt:b
c70cb17 HEAD@{17}: commit: modify good.txt:a
4fc9548 HEAD@{18}: commit: modify good1.txt
07150a2 HEAD@{19}: commit: new file good1.txt
9e35b90 HEAD@{20}: commit (initial): My first commit new file good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git reset --hard 0a1b3cb
HEAD is now at 0a1b3cb new deleteFile.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ ll
total 18
-rw-r--r-- 1 Administrator 197121 18 12月 27 16:48 deleteFile.txt
-rw-r--r-- 1 Administrator 197121 30 12月 27 16:07 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
删除文件并找回总结:
- 前提:删除前,文件存在时的状态提交到了本地库。
- 操作:git reset --hard 指针位置
- 删除操作已经提交到本地库:指针位置指向历史记录
- 删除操作尚未提交到本地库:指针位置使用HEAD
比较文件
-
命令:git diif apple.txt
- 作用:将工作去区中的文件和暂存区的文件比较。
-
命令:git diif 本地库历史版本 文件名称
- 作用:将暂存区的文件和本地库中某个版本的文件比较
不带文件名则比较多个文件。
添加新文件并提交到本地库。
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ vim apple.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git add apple.txt
warning: LF will be replaced by CRLF in apple.txt.
The file will have its original line endings in your working directory
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git commit -m"new apple.txt" apple.txt
warning: LF will be replaced by CRLF in apple.txt.
The file will have its original line endings in your working directory
[master 8481101] new apple.txt
1 file changed, 4 insertions(+)
create mode 100644 apple.txt
修改工作区中的文件,并查看修改的内容:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ vim apple.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git diff apple.txt
warning: LF will be replaced by CRLF in apple.txt.
The file will have its original line endings in your working directory
diff --git a/apple.txt b/apple.txt
index 3e56e74..1e1259c 100644
--- a/apple.txt
+++ b/apple.txt
@@ -1,4 +1,4 @@
apple
-apple
+apple@@@@@@@@@@@
apple
分支
查看分支:
-
命令: git branch -v
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git branch -v * master 8481101 new apple.txt
创建分支:
-
命令:git branch hot_fix
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git branch hot_fix
然后再查看已创建的分支:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git branch -v hot_fix 8481101 new apple.txt * master 8481101 new apple.txt
切换分支:
-
命令:git checkout hot_fix
可以按Tab键补全
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master) $ git checkout hot_fix Switched to branch 'hot_fix'
合并分支:
- 命令:git merge hot_fix
先切换到hot_fix分支,修改hot_fix分支下的文件,然后提交:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git checkout hot_fix
Switched to branch 'hot_fix'
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ ll
total 19
-rw-r--r-- 1 Administrator 197121 23 12月 27 21:38 apple.txt
-rw-r--r-- 1 Administrator 197121 18 12月 27 16:48 deleteFile.txt
-rw-r--r-- 1 Administrator 197121 30 12月 27 21:16 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ cat apple.txt
apple
apple
apple
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ vim apple.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ cat apple.txt
apple@@@@@
apple edit by hot_fix
apple
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git add apple.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git commit apple.txt
[hot_fix 4cc5901] modify apple.txt by hot _fix
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git status
On branch hot_fix
nothing to commit, working tree clean
切换到master分支,然后将hot_fix分支下的内容合并到master
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git checkout master
Switched to branch 'master'
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ ll
total 19
-rw-r--r-- 1 Administrator 197121 23 12月 27 21:43 apple.txt
-rw-r--r-- 1 Administrator 197121 18 12月 27 16:48 deleteFile.txt
-rw-r--r-- 1 Administrator 197121 30 12月 27 21:16 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ cat apple.txt
apple
apple
apple
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git merge hot_fix
Updating 8481101..4cc5901
Fast-forward
apple.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ cat apple.txt
apple@@@@@
apple edit by hot_fix
apple
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git status
On branch master
nothing to commit, working tree clean
解决合并时产生的冲突
- 冲突的表现:
-
冲突的解决:
- 第一步:编辑文件,删除特殊符号
- 第二步:把文件修改到满意的程度。
- 第三步:git add 文件名
- 第四步:git commit -m “”
在master和hot_fix下修改同一文件:good.txt;并且修改同一行且修改内容不一样。
修改master下的good.txt文件:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git branch -v
hot_fix 4cc5901 modify apple.txt by hot _fix
* master 4cc5901 modify apple.txt by hot _fix
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ ll
total 19
-rw-r--r-- 1 Administrator 197121 44 12月 27 21:45 apple.txt
-rw-r--r-- 1 Administrator 197121 18 12月 27 16:48 deleteFile.txt
-rw-r--r-- 1 Administrator 197121 30 12月 27 21:16 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ cat good.txt
aaaaaaaa
bbbbbbbb
cccccccc
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ vim good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git add good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git commit good.txt
[master 43d4ee5] modify by master
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ cat good.txt
aaaaaaaa
bbbbbbbb
cccccccc edit by master
修改hot_fix下的good.txt文件:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (master)
$ git checkout hot_fix
Switched to branch 'hot_fix'
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ ll
total 19
-rw-r--r-- 1 Administrator 197121 44 12月 27 21:45 apple.txt
-rw-r--r-- 1 Administrator 197121 18 12月 27 16:48 deleteFile.txt
-rw-r--r-- 1 Administrator 197121 30 12月 27 22:00 good.txt
-rw-r--r-- 1 Administrator 197121 16165 12月 26 23:43 good1.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ cat good.txt
aaaaaaaa
bbbbbbbb
cccccccc
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ vim good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ cat good.txt
aaaaaaaa
bbbbbbbb
cccccccc edit by hot_fix
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git add good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git commit -m "modify good.txt by hot_fix" good.txt
[hot_fix a8f8f57] modify good.txt by hot_fix
1 file changed, 1 insertion(+), 1 deletion(-)
将master分支的内容合并到hot_fix:
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git merge master
Auto-merging good.txt
CONFLICT (content): Merge conflict in good.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ cat good.txt
aaaaaaaa
bbbbbbbb
<<<<<<< HEAD
cccccccc edit by hot_fix
=======
cccccccc edit by master
>>>>>>> master
合并出错:解决冲突。
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ vim good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ git status
On branch hot_fix
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: good.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ git add good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ git status
On branch hot_fix
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix|MERGING)
$ git commit
[hot_fix 1fcaadd] Merge branch 'master' into hot_fix
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ git status
On branch hot_fix
nothing to commit, working tree clean
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/00.test (hot_fix)
$ cat good.txt
aaaaaaaa
bbbbbbbb
cccccccc edit by hot_fix
cccccccc edit by master
5、本地库和远程库交互
1、创建一个本地库
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test
$ git init
Initialized empty Git repository in E:/GitRepositories/01.test/.git/
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ vim good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git commit -m "new good.txt" good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory
[master (root-commit) 2bd2906] new good.txt
1 file changed, 4 insertions(+)
create mode 100644 good.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git status
On branch master
nothing to commit, working tree clean
2、在GitHub上创建一个远程库
3、在本地库为远程库创建别名
- 查看别名:git remote -v
- 为远程库设置别名:git remote add 别名 远程库地址
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git remote -v
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git remote add test01remote https://github.com/zwd93/test01.git
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git remote -v
test01remote https://github.com/zwd93/test01.git (fetch)
test01remote https://github.com/zwd93/test01.git (push)
4、推送操作
- 命令: git push 远程库别名 分支名
- 完整的把远程库下载到本地
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git push test01remote master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 219 bytes | 109.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/zwd93/test01.git
* [new branch] master -> master
5、克隆
-
命令:git clone 远程库地址
- 完整的把远程库下载到本地
- 创建远程库地址别名
- 初始化本地库
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/02.test $ git clone https://github.com/zwd93/test01.git Cloning into 'test01'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/02.test $ cd test01/ Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/02.test/test01 (master) $ ll total 1 -rw-r--r-- 1 Administrator 197121 25 12月 27 23:35 good.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/02.test/test01 (master) $ git remote -v origin https://github.com/zwd93/test01.git (fetch) origin https://github.com/zwd93/test01.git (push)
6、另外的gitHub账户将远程库克隆下来,并修改提交推送
需要创建该远程仓库的用户邀请该用户,该用户才可以向远程仓库中推送。否则回报403错误
7、远程库的拉取(更新本地库)
-
命令:pull = fetch + merge
- git fetch 远程库别名 分支名
- git merge 远程库别名/分支名
- git pull 远程库别名 分支名
git fetch test01remote master将远程库的文件拉取到本地库,但是此时并没有修改工作区中的文件,此时可以使用 git checkout 远程库别名/分支名 切换分支,查看本地库中的文件,没问题之后再使用merge合并。
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git fetch test01remote master remote: Enumerating objects: 8, done. remote: Counting objects: 100% (8/8), done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0 Unpacking objects: 100% (6/6), done. From https://github.com/zwd93/test01 * branch master -> FETCH_HEAD 82979c3..afdc251 master -> test01remote/master Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git merge test01remote/master Updating 82979c3..afdc251 Fast-forward other.txt | 2 +- other1.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 other1.txt
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git fetch test01remote master remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/zwd93/test01 * branch master -> FETCH_HEAD afdc251..dd5eaf9 master -> test01remote/master Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git status On branch master nothing to commit, working tree clean Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git checkout test01remote/master Note: checking out 'test01remote/master'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at dd5eaf9 modify good.txt by other Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test ((dd5eaf9...)) $ ll total 3 -rw-r--r-- 1 Administrator 197121 39 12月 28 16:19 good.txt -rw-r--r-- 1 Administrator 197121 37 12月 28 15:57 other.txt -rw-r--r-- 1 Administrator 197121 28 12月 28 15:57 other1.txt Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test ((dd5eaf9...)) $ cat good.txt aaaaa edit by other bbbbbb cccccc Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test ((dd5eaf9...)) $ git checkout master Previous HEAD position was dd5eaf9 modify good.txt by other Switched to branch 'master' Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ cat good.txt aaaaa bbbbbb cccccc Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ git merge test01remote/master Updating afdc251..dd5eaf9 Fast-forward good.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master) $ cat good.txt aaaaa edit by other bbbbbb cccccc
Administrator@FKW7JIF862Y1F92 MINGW64 /e/GitRepositories/01.test (master)
$ git pull test01remote master
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/zwd93/test01
* branch master -> FETCH_HEAD
2bd2906..82979c3 master -> test01remote/master
Updating 2bd2906..82979c3
Fast-forward
other.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 other.txt
8、团队合作时冲突解决
- 要点:如果不是基于GitHub远程库的最新版本所做的修改,此时推送会发生冲突。
- 解决:发生冲突后,将远程库的最新版本拉取下来(pull,fetch,merge),然后打开冲突文件,按照分支冲突的解决办法解决。
9、在eclipse中使用git
LINUX命令
vim:创建或编辑文件
i
:进入编辑
ESC
:输入命令。
:set nu
:显示编辑行号
:wq
:退出vim