Git常用命令

git常用命令


0.公司常用命令

命令名称作用
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.设置用户签名

签名的作用是区分不同操作者的身份。用户的签名信息是在每一版本的提交信息中能够看到,以此确认是谁做的。Git首次安装必须设置用户签名,否则无法提交代码。(PS:此签名只是为了登入Windows本地git客户端用的,与github无任何关系)

Administrator@CQ360DN-2021ESZ MINGW64 ~/Desktop
$ git config --global user.name Tuqp

Administrator@CQ360DN-2021ESZ MINGW64 ~/Desktop
$ git config --global user.email 123456789@qq.com

执行完以上操作之后,在Windows家目录,即c盘/用户/当前登入账户文件夹/.gitconfig,会出现此文件,打开后就是设置的用户签名信息了。

2.初始化本地库

本地库一般自己选择一个盘符的文件夹进行建立,然后在该文件夹下右键,点击打开"git bash",最后在git bash里面执行以下命令:

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace
$ git init
Initialized empty Git repository in D:/Git/gitspace/.git/

即可看到已经所设定的文件夹下建立了空的git库。由于在Linux和Windows中,点" . "开头的文件(文件夹)一般都是隐藏文件,因此可以执行ls命令查看:

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ ls -al
total 8
drwxr-xr-x 1 Administrator 197121 0 Mar 23 14:24 ./
drwxr-xr-x 1 Administrator 197121 0 Mar 23 14:23 ../
drwxr-xr-x 1 Administrator 197121 0 Mar 23 14:24 .git/

3.查看本地库的状态

查看本地库状态使用" git status "命令即可:

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
  • On branch master:表示当前在master这个分支下
  • No commits yet:表示当前还未提交版本
  • nothing to commit (create/copy files and use “git add” to track):表示在该目录下未创建任何文件

3.1 新增文件

为了演示效果,这里在git里面新建一个hello.txt文件,并使用vim编辑器输入一些字符。然后再查看里面就多了hello.txt文件了。然后就可以进行查看,linux命令在git是通用的,随便秀即可。

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ vim hello.txt

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ ls -la
total 9
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:44 ./
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:23 ../
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:31 .git/
-rw-r--r-- 1 Administrator 197121 50 Mar 23 14:44 hello.txt

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ cat hello.txt
hello git!
ppppppppp
ppppppppp
yypy  aaaaaaaq
qa


Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ tail -3 hello.txt
yypy  aaaaaaaq
qa

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git status
On branch master

No commits yet

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

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

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)

这个时候再查看状态即可,可以看到出现以下未被追踪的文件:

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

那么接下来就要将文件进行add添加,添加到暂存区。

3.2 将新增文件添加至暂存区

使用命令git add 文件名即可

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (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@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

可以看到会出现警告:意思就是将行末换行符由Windows状态转化为linux状态。接下来查看本地库状态:

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
        new file:   test.txt

可以看到文件由最初的红色变成了绿色,表示git已经追踪到此文件,但此文件当时只存在暂存区,是可以删除的,如果不想给hello.txt或test.txt保存历史版本,即可使用git rm --cached 命令将其删除。

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git rm --cached test.txt
rm 'test.txt'

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git rm --cached hello.txt
rm 'hello.txt'

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ ls -la
total 10
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:47 ./
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:23 ../
drwxr-xr-x 1 Administrator 197121  0 Mar 23 14:56 .git/
-rw-r--r-- 1 Administrator 197121 50 Mar 23 14:44 hello.txt
-rw-r--r-- 1 Administrator 197121  9 Mar 23 14:47 test.txt

3.3 提交本地库

将文件保存到暂存区之后,需要将暂存区的文件提交到本地库。其语法为:

git commit -m "日志信息" 文件名
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (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) 1f49b3c] first commit
 1 file changed, 6 insertions(+)
 create mode 100644 hello.txt

注意:1f49b3c即为精简版的版本号(取完整版版本号的前七位)

此时再次查看状态

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git status
On branch master
nothing to commit, working tree clean

可以发现工作树被清空了,暂存区没有文件了。

3.4 查看文件的版本号

git reflog     //查看粗略的日志
git log        //不仅可以看到日志版本,还能看到作者,提交日期等信息
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git reflog
7a4a133 (HEAD -> master) HEAD@{0}: commit: first commit
1f49b3c HEAD@{1}: commit (initial): first commit

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git log
commit 7a4a1333f46464dc0a11ea311e0f17fca2e16117 (HEAD -> master)
Author: Tuqp <123456789@qq.com>
Date:   Thu Mar 24 13:33:14 2022 +0800

    first commit

commit 1f49b3c78635978a55a43fb3e61651482e0e6558
Author: Tuqp <123456789@qq.com>
Date:   Thu Mar 24 13:31:01 2022 +0800

    first commit

3.5 修改文件

对于已经提交的文件,如果要进行修改,那么应该要注意保存文件的历史版本。其具体操作步骤为:

  • 利用vim修改文件
  • 将修改的文件添加到暂存区
  • 提交到本地库,并标注为"second commit"
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ echo "12234" >> hello.txt

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (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:   hello.txt

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

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (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@CQ360DN-2021ESZ MINGW64 /gitspace (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
[master b529e94] second commit
 1 file changed, 1 insertion(+)

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git status
On branch master
nothing to commit, working tree clean

3.6 历史版本

如果发现当前最新版的文件或代码不如之前的老版本的代码,那么可以使用版本穿梭的功能得到之前提交的各个版本的代码或文件。其具体操作为:

  • 利用"git reflog"查看各个版本的版本号
  • 复制想要查看的那个版本的版本号
  • 使用命令"git reset --hard 版本号"即可回到指定版本
Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git reflog
ceb62cd (HEAD -> master) HEAD@{0}: commit: third commit
b529e94 HEAD@{1}: commit: second commit
7a4a133 HEAD@{2}: commit: first commit
1f49b3c HEAD@{3}: commit (initial): first commit

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git reset --hard 7a4a133
HEAD is now at 7a4a133 first commit

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ git reflog
7a4a133 (HEAD -> master) HEAD@{0}: reset: moving to 7a4a133
ceb62cd HEAD@{1}: commit: third commit
b529e94 HEAD@{2}: commit: second commit
7a4a133 (HEAD -> master) HEAD@{3}: commit: first commit
1f49b3c HEAD@{4}: commit (initial): first commit

Administrator@CQ360DN-2021ESZ MINGW64 /gitspace (master)
$ cat hello.txt
hello git!
ppppppppp
ppppppppp
yypy  aaaaaaaq
qa

此时,本地库的hello.txt也变成了指定的版本。同样的操作也可以将代码或文件变成任意版本。

这里解释一下:git各个版本之间不是像windows一样复制粘贴得到副本的形式,而是在创建一个版本之后就会在内存中为该版本创建一个地址,多个版本就有多个内存地址,所以每次调用不同的版本其实就是head指针指向不同的地址而已,实现版本之间的穿梭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值