Git常用命令(二)

2021-04-15  本文已影响0人  小田BSP

1、git clone

功能:克隆git仓。

格式:git clone url

用法:

## clone rockpi代码,下载完成后,代码存储在rockchip-bsp文件夹
git clone --recursive https://github.com/radxa/rockchip-bsp.git

## clone rockpi代码,下载完成后,代码存储在rockpi文件夹
git clone --recursive https://github.com/radxa/rockchip-bsp.git rockpi

clone完成后,已经存在git仓。

如果本地代码没有git仓,可使用git init命令初始化空的git仓。

2、git init

功能:初始化本地仓,命令执行完后生成.git文件夹。用于新建本地git仓,进行代码管理。

格式:git init

用法:

root@ubuntu:/home/run/code/libdrm-2.4.89# git init
Initialized empty Git repository in /home/run/code/libdrm-2.4.89/.git/

3、git status

功能:检查当前文件状态。

格式:git status

用法:

root@ubuntu:/home/run/code/libdrm-2.4.89# git status
On branch master

No commits yet

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

        Makefile.am
        Makefile.in
        Makefile.sources
        README
        aclocal.m4
        amdgpu/
        ...

注:由于是本地新建git仓,此时文件属于Untracked状态。

4、git add

功能:跟踪文件。

格式:git add

用法:

root@ubuntu:/home/run/code/libdrm-2.4.89# git add .
root@ubuntu:/home/run/code/libdrm-2.4.89# git status
On branch master

No commits yet

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

        new file:   Makefile.am
        new file:   Makefile.in
        new file:   Makefile.sources
        new file:   README
        new file:   aclocal.m4
        new file:   amdgpu/Makefile.am

注:

1)git add filename:跟踪名为filename的文件。

2)git add -u:跟踪被修改或删除文件,不包括新增文件。

3)git add .:跟踪所有变化,等同git add -A

## 1.删除git仓中的README,新增加readme.txt
root@ubuntu:/home/run/code/test/libdrm-2.4.89# rm README
root@ubuntu:/home/run/code/test/libdrm-2.4.89# touch readme.txt
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    README

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

        readme.txt

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

## 2. git add . :跟踪所有变化,和 git add -A 相同
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git add .
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    README
        new file:   readme.txt

## 3. 回退到步骤1
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git reset HEAD README
Unstaged changes after reset:
D       README
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git reset HEAD readme.txt
Unstaged changes after reset:
D       README
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    README

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

        readme.txt

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

## 4. git add -u
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git add -u
root@ubuntu:/home/run/code/test/libdrm-2.4.89# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    README

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

        readme.txt    ## 新增文件没有被跟踪

5、git commit

功能:提交更新。

格式:git commit

用法:

root@ubuntu:/home/run/code/libdrm-2.4.89# git commit -m "Init code"
[master (root-commit) bfa9cc1] Init code
 337 files changed, 156347 insertions(+)
 create mode 100644 Makefile.am
 create mode 100644 Makefile.in
 create mode 100644 Makefile.sources
 create mode 100644 README
 create mode 100644 aclocal.m4
 create mode 100644 amdgpu/Makefile.am

注:

1)git commit:启动文本编辑器,输入提交说明。文本编辑器使用git config指定,可参考前一篇文章。

2)git commit -m “...”:在-m后输入提交信息,直接提交。

3)git commit --amend:对上次提交的信息进行修改。

例:

新建本地libdrm-2.4.89 代码git仓方法如下:

root@ubuntu:/home/run/code/libdrm-2.4.89# git init
root@ubuntu:/home/run/code/libdrm-2.4.89# git add .
root@ubuntu:/home/run/code/libdrm-2.4.89# git commit -m "Init code"

6、.gitignore

功能:忽略文件

git仓目录下编辑.gitignore文件,忽略不需要纳入git仓管理的文件,例:编译后生成的.o等文件。

## 1.新建1.o测试文件,git status查看文件状态
root@ubuntu:/home/run/code/libdrm-2.4.89# touch 1.o
root@ubuntu:/home/run/code/libdrm-2.4.89# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    README
        new file:   readme.txt

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

        1.o

## 2.编辑.gitignore
root@ubuntu:/home/run/code/libdrm-2.4.89# vi .gitignore
root@ubuntu:/home/run/code/test/libdrm-2.4.89# cat .gitignore
*.o

## 3.查看文件状态时,没有1.o文件
root@ubuntu:/home/run/code/libdrm-2.4.89# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    README
        new file:   readme.txt

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

        .gitignore
上一篇 下一篇

猜你喜欢

热点阅读