Git常用命令

克隆

把一个仓库从远程克隆到本地

git clone url

url:必须参数,要克隆的仓库远程地址

[dzlua@win10:~]$ git clone https://gitee.com/dzlua/dmake.git
Cloning into 'dmake'...
remote: Enumerating objects: 53, done.
remote: Counting objects: 100% (53/53), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 53 (delta 27), reused 0 (delta 0)
Unpacking objects: 100% (53/53), done.
[dzlua@win10:~/dmake]$ 

拉取

把本地仓库版本更新到和远程仓库版本一致

git pull

[dzlua@win10:~]$ cd dmake           # 必须进入到仓库所在目录
[dzlua@win10:~/dmake (master)]$ git pull
Already up to date.                 # 表示代码为最新的了
[dzlua@win10:~/dmake (master)]$     # (master)表示仓库分支为master分支

查看状态

查看本地仓库状态

git status

[dzlua@win10:~/dmake (master)]$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
# 以上表示当前仓库没有任何改变
[dzlua@win10:~/dmake (master)]$ 

编辑一个文件,比如新建一个test.md

# 向test.md文件写入“this is test”,如果没有该文件则先创建该文件
[dzlua@win10:~/dmake (master)]$ echo this is test > test.md

# 可以看到有test.md文件了
[dzlua@win10:~/dmake (master)]$ ls
dmake.sh*  install.sh*  LICENSE  README.md  test.md

# 可以看到文件内容是正确的
[dzlua@win10:~/dmake (master)]$ cat test.md
this is test

[dzlua@win10:~/dmake (master)]$ 

此时擦看状态,发现有变更了,此时test.md为红色

[dzlua@win10:~/dmake (master)]$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        test.md

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

[dzlua@win10:~/dmake (master)]$ 

暂存

把仓库中的 一些/所有 文件变动暂存起来

git add something

something:必须参数,暂存的文件/目录

  • 暂存某个文件 git add test.md
  • 暂存目录下文件 git add dir/test.md
  • 暂存目录下所有文件 git add dir
  • 暂存当前目录所有 git add .
  • . 代表当前目录,..代表上级目录
[dzlua@win10:~/dmake (master)]$ git add .    # 因为在仓库根目录,所以是暂存所有
[dzlua@win10:~/dmake (master)]$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

        new file:   test.md

[dzlua@win10:~/dmake (master)]$ 

暂存后test.md变为了绿色

提交

把仓库暂存起来变动文件提交到本地仓库变更中,这样就有版本记录了。

git commit -m "message"

message:必须参数,写一条注释,方便查看

[dzlua@win10:~/dmake (master)]$ git commit -m "这是一个测试"
[master 528d21c] 这是一个测试
1 file changed, 1 insertion(+)
create mode 100644 test.md

[dzlua@win10:~/dmake (master)]$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

nothing to commit, working tree clean

[dzlua@win10:~/dmake (master)]$ 

Your branch is ahead of 'origin/master' by 1 commit.

说明本地仓库与远程仓库有1个不同。

历史记录

查看仓库的历史记录

git log

[dzlua@win10:~/dmake (master)]$ git log
commit 528d21c20c567b1246550b1714e55c292d5fcd6c (HEAD -> master)
Author: dzlua <505544956@qq.com>
Date:   Wed Jul 17 17:39:39 2019 +0800

    这是一个测试

commit 99bdde1702dba4f006bdc85a2b83cf86230c0d0e (origin/master, origin/HEAD)
Author: dzlua <505544956@qq.com>
Date:   Wed Jul 10 10:52:31 2019 +0800

    fixed bug of cmd br
    if bulid err, don't run it

commit 9f9a62fb8e0a14ba2d50de408d236a98aa2322a5
Author: dzlua <505544956@qq.com>
Date:   Wed Jul 10 10:11:18 2019 +0800

    add cmd dmake br
    build and run
:

提示:按q退出记录显示,按回车显示更多

上边显示中:

  • (origin/master, origin/HEAD):表示远程仓库的最新版本在这个位置
  • (HEAD -> master):表示本地仓库的最新版本在这个位置

他们之间相差一条记录(可看到同git statusYour branch is ahead of 'origin/master' by 1 commit.是一致的)。即:本地仓库比远程仓库要新一个版本。

推送

把本地仓库的变更推送到远程

git push

[dzlua@win10:~/dmake (master)]$ git push
...
[dzlua@win10:~/dmake (master)]$ 

tag标签

  1. 查看tag标签

    git tag

    [dzlua@win10:~/dmake (master)]$ git tag
    v1.0
    v3.2
    [dzlua@win10:~/dmake (master)]$ 
    
  2. 新建标签

    git tag -a v3.3 -m "my version 3.3"

    [dzlua@win10:~/dmake (master)]$ git tag -a v3.2 -m "my version 3.2"
    [dzlua@win10:~/dmake (master)]$ git tag
    v1.0
    v3.2
    v3.3
    [dzlua@win10:~/dmake (master)]$ 
    
  3. 推送标签

    默认情况下,git push 并不会把标签传送到远端服务器上,只有通过显式命令才能分享标签到远端仓库

    git push origin v3.3 # 推送v3.3到远程仓库

    git push origin --tags # 推送所有标签到远程仓库

原文地址:https://www.cnblogs.com/dzlua/p/11201766.html