Git基础指令

mkdir 创建一个空目录

$ mkdir learngit
$ cd learngit
$ pwd  # pwd命令用于显示当前目录。在我的Mac上,这个仓库位于/Users/michael/learngit。
/c/Users/usth1/Desktop/learngit #我把文件放在了windows桌面。

git init 初始化目录

$ git init
Initialized empty Git repository in C:/Users/usth1/Desktop/learngit/.git/ # 目录中会出现一个 .git 目录

git add 把文件添加到仓库

$ git add readme.txt

git commit 提交文件到仓库

$ git commit -m "wrote a readme file" 
# -m后面输入的是本次提交的说明,可以输入任意内容,能从历史记录里方便地找到改动记录。
[master (root-commit) 94a9bb7] wrote a readme file
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt
# commit可以一次提交很多文件,所以你可以多次add不同的文件。例如:
# $ git add file1.txt
# $ git add file2.txt file3.txt
# $ git commit -m "add 3 files."

git status 查看仓库当前状态

$ git status # git status 命令可以让我们掌握仓库当前的状态
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")
# 上面的命令输出告诉我们 readme.txt 被修改过了,但还没有准备提交的修改。

git diff 查看文件修改内容

$ git diff # 能够查看具体的修改内容。
diff --git a/readme.txt b/readme.txt
index ce01362..7a34254 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
-hello
+hello,
+great !

git log 查看版本历史纪录

$ git log #查看历史纪录。
commit d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b (HEAD -> master)
Author: Therosebud <isweilianxin@gmail.com>
Date:   Wed Jun 13 14:44:53 2018 +0800

    add great!

commit 94a9bb77cf435f93e0ce50c55da758b5af3b574b
Author: Therosebud <isweilianxin@gmail.com>
Date:   Wed Jun 13 14:26:30 2018 +0800

    wrote a readme file

$ git log --pretty=oneline # 缩减打印内容,下面的长串数字为十六进制 commit ID 。
d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b (HEAD -> master) add great!  # HEAD 表示当前版本。
94a9bb77cf435f93e0ce50c55da758b5af3b574b wrote a readme file

git reset 版本回退

$ git reset --hard HEAD^ # 恢复到之前的版本。
HEAD is now at 94a9bb7 wrote a readme file
# HEAD 表示当前版本,上一个版本就是HEAD^,上上个版本为 HEAD^^,往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
$ git log # 查看当前版本,的确恢复到了上一个版本。
commit 94a9bb77cf435f93e0ce50c55da758b5af3b574b (HEAD -> master)
Author: Therosebud <isweilianxin@gmail.com>
Date:   Wed Jun 13 14:26:30 2018 +0800

    wrote a readme file

# 再次回到最新编辑的文本时,需要找到之前的 append GPL 的 commit ID ,例如 d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b 。
$ git reset --hard d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b
# 版本号没必要写全,前几位就可以了,Git会自动去找,然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。
$ git log # 检查历史纪录,当前版本已经恢复到了 " add great! " 这个版本了
commit d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b (HEAD -> master)
Author: Therosebud <isweilianxin@gmail.com>
Date:   Wed Jun 13 14:44:53 2018 +0800

    add great!

commit 94a9bb77cf435f93e0ce50c55da758b5af3b574b
Author: Therosebud <isweilianxin@gmail.com>
Date:   Wed Jun 13 14:26:30 2018 +0800

    wrote a readme file

git reflog 查询记录的每一次命令

$ git reflog
d563786 (HEAD -> master) HEAD@{0}: reset: moving to d5637865eda11c8dd9d57a48f0d31c1eaaeaa58b
94a9bb7 HEAD@{1}: reset: moving to HEAD^
d563786 (HEAD -> master) HEAD@{2}: commit: add great!
94a9bb7 HEAD@{3}: commit (initial): wrote a readme file

GPL GNUGeneral Public License

GPL是GNU General Public License的缩写,是GNU通用公共授权非正式的中文翻译。它并非由自由软件基金会所发表,亦非使用GNU通用公共授权的软件的法定发布条款─只有GNU通用公共授权英文原文的版本始具有此等效力。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值