git 使用笔记

首先设置个人信息,打开git bash

$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"

新建git目录,打开git bash

$mkdir learngit

$cd d:/learngit/

$pwd

d/learngit/

通过git init 把该目录变成git管理仓库

$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/

把一个文件放到Git仓库只需要两步, 第一步,用命令git add告诉Git,把文件添加到仓库:

$ git add readme.txt

第二步,用命令git commit告诉Git,把文件提交到仓库:

$ git commit -m "wrote a readme file"
[master (root-commit) eaadf4e] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt

补充:

git status命令可以让我们时刻掌握仓库当前的状态

git diff顾名思义就是查看difference

git log命令显示从最近到最远的提交日志, 加上--pretty=oneline参数为简略显示

$ 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
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file

回退到上一个版本add distributed

$ git reset --hard HEAD^
HEAD is now at e475afc add distributed

回退到任意一个版本git reset --hard 版本号(可以只输入前几位,能够区分即可)

$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL

重返未来,可以用git reflog查看历史命令, 以便确定要回到哪个版本号.

原文地址:https://www.cnblogs.com/feng-hao/p/11386294.html