Git使用指南

Git使用指南

配置全局信息

用户信息配置

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

创建初始仓库

创建一个版本库文件夹

  md F:\learngit

初始化仓库

  $ cd f:/learngit

  $ git init

  $ touch readme.txt

常用命令

查看状态 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")  

添加文件到仓库  git add filename 没有提示就预示着没有问题

  $ git add readme.txt

提交文件到仓库 git commit -m "提示语"

  $ git commit -m "[* update readme.txt file]"

  1 file changed,1insertion(+),1deletion(-)

关于提示语的信息,日常开发中会这样写

[+ 增加了什么文件]

[- 删除了什么文件]

[* 编辑了什么文件]

查看历史提交记录 git log

$ git log
commit 542e3f3649fb5e0cc7b1a94d8576de8d8ffdf054 (HEAD -> master)
Author: Your Name <xxxx@126.com>
Date:   Tue Apr 9 17:12:30 2019 +0800

  [* update readme.txt file ]

commit 6f8a5e1d807bd01769086c90e7cdc57630b52fb0
Author: Your Name <xxx@126.com>
Date:   Tue Apr 9 16:55:13 2019 +0800

  [+ 添加readme文件]

记录每一次命令 git relog 

$ git reflog
c86b138 (HEAD -> master) HEAD@{0}: reset: moving to c86b1386936499665d85c287447516a0ea9b4752
542e3f3 HEAD@{1}: reset: moving to HEAD^
c86b138 (HEAD -> master) HEAD@{2}: commit: [* updte readme.txt]
542e3f3 HEAD@{3}: reset: moving to 542e3f3649fb5e0cc7b1a94d8576de8d8ffdf054
6f8a5e1 HEAD@{4}: reset: moving to HEAD^
542e3f3 HEAD@{5}: commit: [* update readme.txt file ]
6f8a5e1 HEAD@{6}: commit (initial): [+ 添加readme文件]  

回滚道某个版本

# git 中代表当前版本的是HEAD 上一个版本是HEAD^ 上两个版本就是HEAD^^
$ git reset --hard HEAD^
HEAD is now at 6f8a5e1 [+ 添加readme文件]

 

撤销修改

# 在add后
git reset HEAD file  # HEAD 指最新版本
git checkout -- file # 丢掉工作区的代码  

删除文件

# 创建一个 test.txt的文件
$ touch test.txt
# 将 test.txt 文件 添加到工作区
$ git add test.txt
# 将 test.txt 文件 添加到版本库
$ git commit -m'[+ add test.txt file]'
# 直接删除文件管理器中的 test.txt 文件
$ rm test.txt
# 删除版本库中的 test.txt 文件
$ git rm test.txt
# 提交到版本库中
$ git commit -m'[- delete test.txt file]'  

误删除文件

# 创建一个 test.txt的文件
$ touch test.txt
# 将 test.txt 文件 添加到工作区
$ git add test.txt
# 将 test.txt 文件 添加到版本库
$ git commit -m'[+add test.txt file]'
# 直接删除文件管理器中的 test.txt 文件
$ rm test.txt
# 恢复 test.txt
$ git checkout -- test.txt