git

z8g 于 2020-06-30 发布

git

add与commit

$ cd learngit/
$ pwd
/Code/study/git/learngit
$ git init
Initialized empty Git repository in /Code/study/git/learngit/.git/
$ subl readme.txt
Git is a version control system.
Git is free software.
$ git add readme.txt

此时隐藏目录新增文件: .git/object/d8/036c15d7630cc6056052a2ec506483a7ce2d0a

7801 4bca c94f 5230 3561 70cf 2c51 c82c
5648 5428 4b2d 2ace cccf 5348 cecf 2b29
cacf 5128 ae2c 2e49 cdd5 e382 2a48 2b4a
4d55 28ce 4f2b 294f 2c4a d503 0096 2815
a9
$ git commit -m "wrote a readme file"

修改readme.txt为:

Git is a distributed version control system.
Git is free software distributed under the GPL.
$ git add readme.txt
$ git commit -m 'append GPL'
[master 8cbe6a4] append GPL
 1 file changed, 2 insertions(+), 2 deletions(-)
B000000098782a:learngit zhaoxuyang03$ git log
$ git log
commit 8cbe6a4f513510574dad837979efdb315080f829 (HEAD -> master)
Author: zhaoxuyang03 <zhaoxuyang03@baidu.com>
Date:   Tue Jun 30 10:42:09 2020 +0800

    append GPL

commit 9368594d524ebbb501133b2f56f1bb354a290e20
Author: zhaoxuyang03 <zhaoxuyang03@baidu.com>
Date:   Tue Jun 30 10:39:49 2020 +0800

    wrote a readme file
$ git log --pretty=oneline
8cbe6a4f513510574dad837979efdb315080f829 (HEAD -> master) append GPL
9368594d524ebbb501133b2f56f1bb354a290e20 wrote a readme file

版本回退

回退版本:

$ git reset HEAD^
Unstaged changes after reset:
M	readme.txt
B000000098782a:learngit zhaoxuyang03$ git log
$ git log --pretty=oneline
9368594d524ebbb501133b2f56f1bb354a290e20 (HEAD -> master) wrote a readme file
$ git reset --hard 8cbe6a4f513510574dad837979efdb315080f829
HEAD is now at 8cbe6a4 append GPL
git log --pretty=oneline
8cbe6a4f513510574dad837979efdb315080f829 (HEAD -> master) append GPL
9368594d524ebbb501133b2f56f1bb354a290e20 wrote a readme file
$ git reflog
8cbe6a4 (HEAD -> master) HEAD@{0}: reset: moving to 8cbe6a4f513510574dad837979efdb315080f829
9368594 HEAD@{1}: reset: moving to HEAD^
8cbe6a4 (HEAD -> master) HEAD@{2}: commit: append GPL
9368594 HEAD@{3}: commit (initial): wrote a readme file

工作区与暂存区

工作区(Working Directory):

$ pwd
/Code/study/git/learngit

版本库(Repository):

cd .git && ls 
COMMIT_EDITMSG	ORIG_HEAD	description	index		logs		refs
HEAD		config		hooks		info		objects

暂存区(stage或index):

WorkDirectoryAndRepository01

$ echo "LICENSE" > LICENSE
$ echo "Git has a mutable index called stage." >> readme.txt 
$ 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.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	LICENSE

no changes added to commit (use "git add" and/or "git commit -a")

Untracked files 表示从没有被添加,Changes not staged for commit 表示没提交

$ git add .
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   LICENSE
	modified:   readme.txt

WorkDirectoryAndRepository02

管理修改

$ cat diff.txt 
line 1
line 2
line 3

diff.txt 的内容修改为:

line 1
line 2
line 3
line 4
$ git add diff.txt 
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   diff.txt


继续修改 diff.txt 的内容:

line 1
line 2
line 3
line 4
line 5
$ git commit -m 'update 2 diff.txt'
[master b44d169] update 2 diff.txt
 1 file changed, 2 insertions(+), 1 deletion(-)

$ 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:   diff.txt

no changes added to commit (use "git add" and/or "git commit -a")

查看 diff.txt 的改动:

$ git diff HEAD -- diff.txt
diff --git a/diff.txt b/diff.txt
index 0f8a25a..a751413 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1,4 +1,5 @@
 line 1
 line 2
 line 3
-line 4
\ No newline at end of file
+line 4
+line 5
\ No newline at end of file
$ git add .
$ git commit -m 'diff'
[master 7cdaf10] diff
 1 file changed, 2 insertions(+), 1 deletion(-)
$ git diff HEAD -- diff.txt

撤销修改

撤销工作区的修改:

git checkout -- <file>
echo -n 'line 5' > diff.txt 
$ 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:   diff.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git checkout -- diff.txt

$ git status
On branch master
nothing to commit, working tree clean

$ cat diff.txt 
line 1
line 2
line 3
line 4

撤销暂存区的修改:

$ echo -n 'line' >> diff.txt
$ git add diff.txt 
$ git checkout -- diff.txt 
$ cat diff.txt 
line 1
line 2
line 3
line 4

删除文件

echo "test content" > test.txt
$ git add test.txt
$ git commit -m 'add test.txt'
[master d39a880] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ git rm test.txt 
rm 'test.txt'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    test.txt

添加远程库

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令 git push -u origin master 第一次推送master分支的所有内容

此后,每次本地提交后,只要有必要,就可以使用命令 git push origin master推送最新修改

创建与合并分支

$ git checkout -b dev
Switched to a new branch 'dev'

相当于:

$ git branch dev
$ git checkout dev
$ git add .

$ git commit -m 'delete cli'
[dev f9a1b64] delete cli
 2 files changed, 17 insertions(+), 36 deletions(-)
 delete mode 100644 cli.bat

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ git merge dev
Updating 5902724..f9a1b64
Fast-forward
 _posts/2020-06-30-git.md | 17 +++++++++++++++++
 cli.bat                  | 36 ------------------------------------
 2 files changed, 17 insertions(+), 36 deletions(-)
 delete mode 100644 cli.bat

$ git branch -d dev
Deleted branch dev (was f9a1b64).

$ git branch
* master

Git鼓励大量使用分支:

查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 或者 git switch <name> 创建+切换分支:git checkout -b <name> 或者 git switch -c <name> 合并某分支到当前分支:git merge <name> 删除分支:git branch -d <name>

解决冲突

<<<<<<< HEAD
222
=======
111
>>>>>>> dev
$ git log --graph --pretty=oneline --abbrev-commit
*   80ba8c7 (HEAD -> master, origin/master) diff
|\
| * 50fb8f0 (origin/dev, dev) 111
* | eb0b942 222
|/  
*   17097d0 Merge branch 'feature_1'
|\
| * d8d1c71 readme
* | fb23baa readMe
|/  
* d30e006 git