2. Git基础命令

2.1 初始化Git仓库 

创建的git目录下

git]# git init

# 创建文件并提交

git]# echo 111 > 1.txt

git]# git status -s

?? 1.txt

git]# git add 1.txt

git]# git status -s

A  1.txt

git]# git commit -m 'Add 1.txt'

[master (root-commit) 1259163] Add 1.txt

 1 file changed, 1 insertion(+)

 create mode 100644 1.txt

git]# git status

On branch master

nothing to commit, working tree clean

# 克隆现有的仓库

git]# git clone https://github.com/luwei0915/06_Linux

# 克隆的时候重命名

# git]# git clone https://github.com/luwei0915/06_Linux mylinux

2.2 忽略文件

# 忽略所有以 .o 或 .a 结尾的文件

*.[oa]

# 忽略所有名字以波浪符(~)结尾的文件

*~

# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件

!lib.a

# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO

/TODO

# 忽略任何目录下名为 build 的文件夹

build/

# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt

doc/*.txt

# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件

doc/**/*.pdf

2.3 diff比较

git]# git status -s

AM 2.txt

git]# git diff  (版本库有的文件)

diff --git a/2.txt b/2.txt

index c200906..9bd69d1 100644

--- a/2.txt

+++ b/2.txt

@@ -1 +1,2 @@

 222

+333

# 查看已暂存的将要添加到下次提交里的内容(版本库没有的文件)

git]# git diff --staged

diff --git a/3.txt b/3.txt

new file mode 100644

index 0000000..55bd0ac

--- /dev/null

+++ b/3.txt

@@ -0,0 +1 @@

+333

2.4 跳过git add 直接 git commit

git]# git commit -a -m "Add 4.txt"

2.5 删除文件

git]# git rm (-f) 3.txt

rm '3.txt'

git]# git status -s

D  3.txt

git]# git commit -m 'del 3.txt'

# 删除版本库或者暂存区的文件,但是想保留在本地

git]# git rm --cached 2.txt

rm '2.txt'

git]# git status -s

D  2.txt

?? 2.txt

git]# git commit -m 'del 2.txt'

git]# git status -s

?? 2.txt

2.6 重命名文件git]# git mv 2.txt 3.txt

git]# git status -s

R  2.txt -> 3.txt

git]# git commit -m 'Move 2.txt to 3.txt'

2.6 查看日志

git]# git log

commit da183e634c25ced8d126a1d676797377c26e8c61 (HEAD -> master)

Author: testuser <testuser@example.com>

Date:   Thu Aug 13 15:15:26 2020 +0800

    Move 2.txt to 3.txt

commit 341d7afad40b850a3224504de03e0e9107423edf

Author: testuser <testuser@example.com>

Date:   Thu Aug 13 15:14:22 2020 +0800

    Add 2.txt

# 只看最新的两条日志

# git log -p -2

# 一些汇总信息

# git log --stat

# 还有short,full 和 fuller 选项

git]# git log --pretty=oneline

da183e634c25ced8d126a1d676797377c26e8c61 (HEAD -> master) Move 2.txt to 3.txt

341d7afad40b850a3224504de03e0e9107423edf Add 2.txt

499d44337a0f6c852e3771695bfee343c3c93986 del 2.txt

# 自定义日志输出格式

 git]# git log --pretty=format:"%h %s" --graph

* da183e6 Move 2.txt to 3.txt

* 341d7af Add 2.txt

* 499d443 del 2.txt

# 列出最近两周的所有提交

# git log --since=2.weeks

# git log --pretty="%h - %s" --author='tom' --since="2020-07-01" --before="2020-08-01" --no-merges

2.7 撤销操作

# 撤销commit 的  -m 说明信息

git]# git commit --amend

# 取消暂存的文件 取消 add

git]# git reset HEAD 4.txt (--hard)

git]# git status -s

?? 4.txt

# 撤销已经提交版本库又修改的文件(本地修改错误,从版本库还原)

git]# git status -s

 M 3.txt

git]# git checkout -- 3.txt

# git checkout -- <file> 是一个危险的命令。 你对那个文件在本地的任何修改都会消失——Git 会用最近提交的版本覆盖掉它。

2.8 远程仓库的使用

# 克隆远程仓库

git]# git clone https://github.com/luwei0915/06_Linux

# 查看远程仓库名称

06_Linux]# git remote

origin

# 显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL

06_Linux]# git remote -v

origin  https://github.com/luwei0915/06_Linux (fetch)

origin  https://github.com/luwei0915/06_Linux (push)

# 添加远程仓库

06_Linux]#  git remote add pb https://github.com/paulboone/ticgit

06_Linux]# git remote -v

origin  https://github.com/luwei0915/06_Linux (fetch)

origin  https://github.com/luwei0915/06_Linux (push)

pb      https://github.com/paulboone/ticgit (fetch)

pb      https://github.com/paulboone/ticgit (push)

# 获取远程仓库 pb 代码 (拉取所有你还没有的数据)

06_Linux]# git fetch pb

# 推送当前分支到远程仓库

06_Linux]# git push origin master

# 查看远程仓库

06_Linux]# git remote show origin

* remote origin

  Fetch URL: https://github.com/luwei0915/06_Linux

  Push  URL: https://github.com/luwei0915/06_Linux

  HEAD branch: master

  Remote branch:

    master tracked

  Local branch configured for 'git pull':

    master merges with remote master

  Local ref configured for 'git push':

    master pushes to master (up to date)

# 重命名远程仓库

06_Linux]# git remote rename pb paul

# 删除远程仓库

06_Linux]# git remote remove paul

2.9 打标签(版本控制)

# 查看标签

git]# git tag

# 打标签

git]# git tag -a v1.1 -m "Version 1.1 at 2020-08-01"

git]# git tag (-l)

v1.1

git]# git tag -a v1.2 -m 'Modify 3.txt at Version 1.2'

git]# git tag -l 'v1.*'

v1.1

v1.2

# 查看标签明细

git]# git show v1.2

tag v1.2

Tagger: testuser <testuser@example.com>

Date:   Thu Aug 13 15:59:43 2020 +0800

Modify 3.txt at Version 1.2

commit a17d7c5161053e6f534afe21a48609287506c206 (HEAD -> master, tag: v1.2)

Author: testuser <testuser@example.com>

Date:   Thu Aug 13 15:59:05 2020 +0800

    Modify 3.txt

diff --git a/3.txt b/3.txt

index c200906..55bd0ac 100644

--- a/3.txt

+++ b/3.txt

@@ -1 +1 @@

-222

+333

# 打轻量标签 不加 -a

# git tag v1.5

# 以前的版本打标签

git]# git tag -a v1.0 125916314b4983825dad424e9eda5ac4275481e5 -m 'Add version 1.0'

# 共享标签 推送标签到远程仓库

git]# git push origin v1.2

# 删除标签

git]# git tag -d v1.0

Deleted tag 'v1.0' (was 4b3651f)

# 删除远程标签

git]# git push origin --delete v1.2

2.10 Git别名 取消别名

git]# git config --global alias.co checkout

git]# git config --global --unset alias.co

原文地址:https://www.cnblogs.com/luwei0915/p/13819255.html