2、Git常用命令

Git常用命令

  • 1. 设置用户签名
  • 2. 初始化本地库
  • 3. 查看本地库状态
  • 4. 添加暂存区
  • 5. 提交本地库
  • 6. 修改文件
  • 7. 历史版本

命令名称 作用
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. 基本语法
1.	git config --global user.name 用户名
2.	git config --global user.email 邮箱名
  1. 设置完成后查看:
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/GitToGitHub (master)
$ cat ~/.gitconfig
[filter "lfs"]
        clean = git-lfs clean -- %f
        smudge = git-lfs smudge -- %f
        process = git-lfs filter-process
        required = true
[user]
        name = XXX
        email = [email protected]
[core]
        excludesfile = C:/Users/L/git.ignore
[color]
        ui = auto
[http]
        sslVerify = false
[https]
        sslVerify = false

说明:签名的作用是区分不同操作者身份,用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git首次安装必须设置一下用户签名,否则无法提交代码。

Note:这里设置用户签名和将来登录GitHub(或者其他代码托管中心)的账号没有任何关系。可以在C盘用户文件夹中的.gitconfi文件中查看配置信息

2. 初始化本地库

  1. 基本语法
git init

在windows系统中定位到相应的文件夹资源窗口中,然后右键打开Git Bash就确定了库所在的目录。利用git init命令进行初始化,其目录下会创建.git的隐藏文件夹。

  1. 案例实操
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/GitToGitHub (master)
$ git init
Reinitialized existing Git repository in E:/Git-space/Test/GitToGitHub/.git/

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/GitToGitHub (master)
$ ll -a
total 6
drwxr-xr-x 1 L 197121   0 Jul 24 21:59 ./
drwxr-xr-x 1 L 197121   0 Jul 24 22:07 ../
drwxr-xr-x 1 L 197121   0 Dec 25 18:33 .git/
-rw-r--r-- 1 L 197121 367 Jul 24 21:59 githubURL.txt
-rw-r--r-- 1 L 197121 190 Jul 24 21:59 hello.txt
  1. 结果查看
    2、Git常用命令_第1张图片

3. 查看本地库状态

  1. 基本语法
git status
  1. 案例实操
  • 首次查看
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
  • 新增文件:在版本管理文件夹中添加hello.txt文件
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ vim hello.txt

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ tail -n 1 hello.txt
11111
  • 再次查看
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

4. 添加暂存区

  1. 基本语法:git add 文件名

  2. 案例

  • 将工作区的文件添加到暂存区
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (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
  • 查看状态(没有文件需要提交)
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   hello.txt
  • 删除暂存区文件(利用git rm --cached < file >),然后再查看状态
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        hello.txt

创建的文件还在文件夹中,只是在暂存区中将其删除。

5. 提交本地库

  1. 基础语法
git commit -m”日志信息” 文件名
git reflog
git log
  1. 操作实例
  • 将暂存区的文件提交到本地库
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (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) ad5c7ee] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
  • 查看状态(检测到暂存区有新文件)

查看一般日志信息

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git reflog
ad5c7ee (HEAD -> master) HEAD@{0}: commit (initial): first commit

查看详细日志信息

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git log
commit ad5c7ee0af3f3d9cb9193da5420102b6cd7dd7e8 (HEAD -> master)
Author: XXX <[email protected]>
Date:   Tue Dec 26 10:44:06 2023 +0800

    first commit

6. 修改文件

修改hello.txt文件中的内容,然后通过wim查看。

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ vim hello.txt

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ cat hello.txt
11111
222222
  1. 查看状态(检测到工作区有文件被修改)
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   hello.txt
  1. 将修改的文件再次添加到暂存区
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git commit -m "second 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
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master a0bd3a9] second commit
 1 file changed, 1 insertion(+)
  1. 查看状态(工作区的修改添加到了暂存区)
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

7. 历史版本

  1. 查看历史版本
  • 代码
1.	git reflog 查看版本信息 
2.	git log    查看版本详细信息
  • 实例
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git reflog
a0bd3a9 (HEAD -> master) HEAD@{0}: commit: second commit
ad5c7ee HEAD@{1}: commit (initial): first commit
  1. 版本穿梭
  • 代码
1.	git reset –-hard 版本号
  • 实例
    实际上移动的HEAD指针
    2、Git常用命令_第2张图片
    HEAD文件中保存有指针当前指向的分支名称。
    Refs/heads文件夹中的master文件保存了指向的当前版本信息。
    hello.txt中的内容信息也会跟随版本的变化而变化。
L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git reset --hard ad5c7ee
HEAD is now at ad5c7ee first commit

L@DESKTOP-C3ML9EI MINGW64 /e/Git-space/Test/gitTest (master)
$ git reflog
a0bd3a9 (HEAD -> master) HEAD@{0}: reset: moving to a0bd3a9
ad5c7ee HEAD@{1}: reset: moving to ad5c7ee
a0bd3a9 (HEAD -> master) HEAD@{2}: reset: moving to HEAD
a0bd3a9 (HEAD -> master) HEAD@{3}: commit: second commit
ad5c7ee HEAD@{4}: commit (initial): first commit

你可能感兴趣的:(软件版本管理,git)