Git
1、Git 常用命令
命令名称 | 作用 |
---|
git config --global user.name 用户名 | 设置用户签名 | git config --global user.email 邮箱 | 设置用户签名 | git init | 初始化本地库 | git status | 查看本地库状态 | git add 文件名 | 添加到暂存区 | git commit -m “日志信息” 文件名 | 提交到本地库 | git reflog | 查看历史版本 | git reset --hard 版本号 | 版本穿梭 |
1.1、设置用户签名
首次安装必须设置用户签名:
Li@Li-911m MINGW64 ~/Desktop
$ git config --global user.name Li
Li@Li-911m MINGW64 ~/Desktop
$ git config --global user.email li@li.com
1.2、初始化本地库
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java
$ git init
Initialized empty Git repository in G:/work/coding/Git-Space/Java/.git/
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ ll -a
total 4
drwxr-xr-x 1 Li 197609 0 Mar 4 16:53 ./
drwxr-xr-x 1 Li 197609 0 Mar 4 16:53 ../
drwxr-xr-x 1 Li 197609 0 Mar 4 16:53 .git/
1.3、查看本地库状态
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
新增一个文件后:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
helloGit.text
nothing added to commit but untracked files present (use "git add" to track)
1.4、添加暂存区
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git add helloGit.text
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: helloGit.text
删除文件(只是暂存区文件的删除,并不是工作区):
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git rm --cached helloGit.text
rm 'helloGit.text'
1.5、提交本地库
将暂存区文件提交到本地库形成历史版本:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git commit -m "first commit" helloGit.text
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
[master (root-commit) a247ad1] first commit
1 file changed, 15 insertions(+)
create mode 100644 helloGit.text
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
nothing to commit, working tree clean
查看历史版本:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reflog
a247ad1 (HEAD -> master) HEAD@{0}: commit (initial): first commit
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git log
commit a247ad1c5ead23d0554a31c65720e368676beef8 (HEAD -> master)
Author: Li <li@li.com>
Date: Fri Mar 4 17:08:15 2022 +0800
first commit
2、修改文件,模拟版本迭代
修改完文件后查看工作区状态:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: helloGit.text
no changes added to commit (use "git add" and/or "git commit -a")
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git commit -m "second commit" helloGit.text
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
[master 2ea9730] second commit
1 file changed, 1 insertion(+), 1 deletion(-)
提交完之后再次查看状态:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
nothing to commit, working tree clean
查看提交日志:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reflog
2ea9730 (HEAD -> master) HEAD@{0}: commit: second commit
a247ad1 HEAD@{1}: commit (initial): first commit
我们可以看到指针指向第二个版本,查看文件:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ cat helloGit.text
hello git! hello world! 2222222222
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
再次修改文件:
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git add helloGit.text
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: helloGit.text
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git commit -m "third commit" helloGit.text
warning: LF will be replaced by CRLF in helloGit.text.
The file will have its original line endings in your working directory
[master 6b87440] third commit
1 file changed, 1 insertion(+), 1 deletion(-)
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git status
On branch master
nothing to commit, working tree clean
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reflog
6b87440 (HEAD -> master) HEAD@{0}: commit: third commit
2ea9730 HEAD@{1}: commit: second commit
a247ad1 HEAD@{2}: commit (initial): first commit
3、版本穿梭
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reflog
6b87440 (HEAD -> master) HEAD@{0}: commit: third commit
2ea9730 HEAD@{1}: commit: second commit
a247ad1 HEAD@{2}: commit (initial): first commit
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reset --hard 2ea9730
HEAD is now at 2ea9730 second commit
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ git reflog
2ea9730 (HEAD -> master) HEAD@{0}: reset: moving to 2ea9730
6b87440 HEAD@{1}: commit: third commit
2ea9730 (HEAD -> master) HEAD@{2}: commit: second commit
a247ad1 HEAD@{3}: commit (initial): first commit
Li@Li-911m MINGW64 /g/work/coding/Git-Space/Java (master)
$ cat helloGit.text
hello git! hello world! 2222222222
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
hello git! hello world!
版本穿梭会影响工作区的文件!
Git 版本穿梭,底层其实是移动 HEAD 指针:
4、Git 分支操作
|