learn git(本地仓库)
阅读原文时间:2022年01月12日阅读:2

#本地

在Windows上安装Git

在Windows上使用Git,可以从Git官网直接https://git-scm.com/downloads下载,然后按默认选项安装即可。

装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功!

安装完成后,还需要最后一步设置,在命令行输入:

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

创建版本库

$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit

初始化本地仓库

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

将文件上传到缓存区或者 git add .  或者git add -A 或者 git add --all

$ git add readme.txt

将文件从缓存区提交到版本库

$ 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
On branch master
Changes not staged for commit:
(use "git add …" to update what will be committed)
(use "git checkout -- …" to discard changes in working directory)

modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

查看某个文件(版本库或缓存区)和工作区中不同

$ git diff readme.txt
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 reset --hard HEAD^
HEAD is now at e475afc add distributed

在Git中,用HEAD表示当前版本,也就是最新的提交,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

版本回退到指定版本号

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

如果想将回退后的版本强制推送到远程仓库。git push -f origin master

远程版本强制推送当前分支

git push -f

远程版本推送所有分支

git push --all origin -u

查找版本号(如果重启机器了)

$ git reflog
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

撤销修改1(如果让这个文件回到最近一次git commitgit add时的状态)

$ git checkout -- readme.txt

撤销修改2(把暂存区的修改撤销掉(unstage),重新放回工作区)

$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt

撤销修改3(把版本库的修改撤销掉)

那只能通过版本回退了

撤销修改4*(远程的修改撤销)

祝你好运没乱动代码,没法子

删除版本库中文件(如果工作区删除了文件,需要版本库也删除)

$ git rm test.txt
rm 'test.txt'

$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt

删除git目录下的.git目录

rm -rf .git