一、基本信息
用户签名的设置 全局名称和邮箱
Administrator@DESKTOP-27HOQJ5 MINGW64 ~/Desktop
$ git config --global user.name torchstXX
Administrator@DESKTOP-27HOQJ5 MINGW64 ~/Desktop
$ git config --global user.email 138448XXX@163.com
初始化本地库
$ git init
Initialized empty Git repository in G:/git-demo/.git/
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ ls -alh
total 28K
drwxr-xr-x 1 Administrator 197121 0 11月 25 22:18 ./
drwxr-xr-x 1 Administrator 197121 0 11月 25 22:18 ../
drwxr-xr-x 1 Administrator 197121 0 11月 25 22:18 .git/
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
添加暂存区
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
提交本地库
$ git commit -m "first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) b72495e] first commit
1 file changed, 4 insertions(+)
create mode 100644 hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
查看提交记录
$ git reflog
b72495e (HEAD -> master) HEAD@{0}: commit (initial): first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git log
commit b72495ea94c83460df1f02079fe8cd5f79a5707d (HEAD -> master)
Author: torchstar <13844826204@163.com>
Date: Thu Nov 25 22:42:10 2021 +0800
first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
修改文件
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git commit hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master 079e3b5] test Please enter the commit message for your changes. Lines starting
1 file changed, 3 insertions(+)
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git log
commit 079e3b5643e67df34cfb05ce3965dae4e8a69bba (HEAD -> master)
Author: torchstar <13844826204@163.com>
Date: Thu Nov 25 22:55:22 2021 +0800
test Please enter the commit message for your changes. Lines starting
commit b72495ea94c83460df1f02079fe8cd5f79a5707d
Author: torchstar <13844826204@163.com>
Date: Thu Nov 25 22:42:10 2021 +0800
first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git reflog
079e3b5 (HEAD -> master) HEAD@{0}: commit: test Please enter the commit messagefor your changes. Lines starting
b72495e HEAD@{1}: commit (initial): first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
历史版本
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git reflog
0e55f99 (HEAD -> master) HEAD@{0}: commit: thied commit
d3c33e7 HEAD@{1}: commit: second commit
079e3b5 HEAD@{2}: commit: test Please enter the commit message for your changes. Lines starting
b72495e HEAD@{3}: commit (initial): first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git log
commit 0e55f9962d7977499aa4629068955733d38dd4a1 (HEAD -> master)
Author: torchstar <13844826204@163.com>
Date: Sun Nov 28 09:53:13 2021 +0800
thied commit
commit d3c33e71fb9bf647b7da54383c88badc4126a828
Author: torchstar <13844826204@163.com>
Date: Sun Nov 28 09:51:03 2021 +0800
second commit
commit 079e3b5643e67df34cfb05ce3965dae4e8a69bba
Author: torchstar <13844826204@163.com>
Date: Thu Nov 25 22:55:22 2021 +0800
test Please enter the commit message for your changes. Lines starting
commit b72495ea94c83460df1f02079fe8cd5f79a5707d
Author: torchstar <13844826204@163.com>
Date: Thu Nov 25 22:42:10 2021 +0800
first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
版本穿梭
$ git reflog
0e55f99 (HEAD -> master) HEAD@{0}: commit: thied commit
d3c33e7 HEAD@{1}: commit: second commit
079e3b5 HEAD@{2}: commit: test Please enter the commit message for your changes. Lines starting
b72495e HEAD@{3}: commit (initial): first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git reset --hard 0e55f99
HEAD is now at 0e55f99 thied commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git reflog
0e55f99 (HEAD -> master) HEAD@{0}: reset: moving to 0e55f99
0e55f99 (HEAD -> master) HEAD@{1}: commit: thied commit
d3c33e7 HEAD@{2}: commit: second commit
079e3b5 HEAD@{3}: commit: test Please enter the commit message for your changes. Lines starting
b72495e HEAD@{4}: commit (initial): first commit
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
二、分支
创建分支
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git branch hot-fix
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git branch -v
hot-fix d3c33e7 second commit
* master d3c33e7 second commit
切换分支
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
切换完分支提交
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git add hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git commit -m 'hot fix add' hello.txt
[hot-fix 88cd8cf] hot fix add
1 file changed, 1 insertion(+)
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git reflog
88cd8cf (HEAD -> hot-fix) HEAD@{0}: commit: hot fix add
d3c33e7 (master) HEAD@{1}: checkout: moving from master to hot-fix
d3c33e7 (master) HEAD@{2}: reset: moving to d3c33e7
0e55f99 HEAD@{3}: reset: moving to 0e55f99
0e55f99 HEAD@{4}: commit: thied commit
d3c33e7 (master) HEAD@{5}: commit: second commit
079e3b5 HEAD@{6}: commit: test Please enter the commit message for your changes. Lines starting
b72495e HEAD@{7}: commit (initial): first commit
分支合并
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git merge hot-fix
Updating d3c33e7..88cd8cf
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
分支冲突解决
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git add hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git commit -m 'master adds' hello.txt
[master 5753b6f] master adds
1 file changed, 6 insertions(+)
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git add hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git commit -m 'hot-fix adds' hello.txt
[hot-fix dfddb9f] hot-fix adds
1 file changed, 3 insertions(+)
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git checkout hot-fix
Already on 'hot-fix'
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ git checkout master
Switched to branch 'master'
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master|MERGING)
$ git status
On branch master
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: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master|MERGING)
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master|MERGING)
$ git add hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master|MERGING)
$ git commit -m 'chongtujiejue'
[master 7d4e7b7] chongtujiejue
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ ls
hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)
$ vim hello.txt
Administrator@DESKTOP-27HOQJ5 MINGW64 /g/git-demo (hot-fix)