跳转至

Git 基本操作

约 148 个字 53 行代码 预计阅读时间 1 分钟

初始化

git init

初始化裸仓库(可用于建立中心仓库):

git --bare init

添加文件

git add 文件路径

如果想添加所有文件,可以写:

git add .

提交

git commit -m "提交说明"

查询

查看状态

git status

一些输出示例:

1
2
3
4
5
6
7
8
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
On branch master
nothing to commit, working tree clean

查看更改

git diff 文件路径
1
2
3
4
5
6
7
8
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

查看提交日志

git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:06:15 2018 +0800

    append GPL

commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

如果需要每个提交一行输出:

git log --pretty=oneline
1
2
3
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file

查看执行过的命令

git reflog
1
2
3
4
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file

版本转换

git reset --hard 提交ID的前若干个字符或特殊标记

特殊标记

  • HEAD:当前版本
  • ^:向上一个版本,如当前版本的上一个版本为 HEAD^,上上个版本为 HEAD^^,以此类推
  • ~数字:向上若干个版本,如当前版本的上 100 个版本为 HEAD~100