#### **3.1創建本地倉庫**
git init 在當前文件夾創建一個git管理的本地倉庫
#### **3.2將工作區保存到暫存區**
git add xxx add命令不是添加一個文件,而是,將修改或新增的文件從動作去添加到當前版本庫的暫存區中,表明此修改或新增的文件在下次commit提交的內容之中。
git add * 是將所有修改與新增的文件添加到暫存區。
當你的倉庫根目錄下設置了.gitignore文件并配置了忽略文件時,此時命令會報錯,如下:
```
~~~bash
$ git add *
The following paths are ignored by one of your .gitignore files:
ignore.md
Use -f if you really want to add them.
fatal: no files added
~~~
```
如果強行使用git add -f * 命令,則會添加ignore.md文件。但這樣.gitignore文件就沒有意義了。
#### **3.3將暫存區提交到版本庫**
git commit 將暫存區提交到本地倉庫的版本庫。如果沒有被git add 添加到暫存區的文件不會被提交。
-m “xxxx”后面跟著為本次提交的說明。
-a 大意是將工作區中的所有修改或刪除后的文件都提交到本地倉庫,但新增的文件并不提交。
常用commit -m"aaa" -a 直接將更改從工作區提交到版本庫,而不用經過暫存區。
#### **3.4撤銷(重置)**
git reset --hard等價于git reset --hard HEAD即撤銷工作區和暫存區中的所有修改,重置為版本庫中HEAD指向的當前版本(未被修改的部分)。
而上一個版本就是**`HEAD^`**,上上個版本就是**`HEAD^^`**,當然往上寫100個版本寫100個^比較容易數不過來,所以寫成HEAD~100.
或者也可以git reset --hard <commit_id>直接回溯到某個版本,<commit_id>指版本號(通過git log)查看,版本號沒必要寫全,前幾位就可以了。
git reset<file>或git reset HEAD<file>撤銷git add 到暫存區的修改
git checkout -- <file>撤銷工作區的修改。
#### **3.5刪除**
git rm<file>如果你已經把某個新增文件提交到版本庫了。然后你想刪除這個文件,那就用該命令將文件刪除,再提交。使用該命令后,git的狀態是:
```
~~~bash
$ git rm test.md
rm 'test.md'
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: test.md
~~~
```
說明git rm會直接更新到暫存區。
刪除文件其實還可以直接刪除。如下:
```
~~~bash
$ ls
README.md ignore2.md test2.md
$ rm test2.md
$ ls
README.md ignore2.md
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: test2.md
no changes added to commit (use "git add" and/or "git commit -a")
~~~
```
此時沒有直接更新到暫存區,可以通過git add/rm<file>來更新。同時可以通過git checkout --<file>來恢復這個文件。
#### **3.6查看狀態**
git status顯示工作區與暫存區的狀態,比如哪些文件被修改了、哪些文件沒保存在暫存區、謝謝文件沒有提交等等。若存在沒有被add的新文件(即未被git跟蹤的文件),則會提示Untracked
```
~~~bash
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .gitignore
# deleted: ignore.md
#
# 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: test.md
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# ignore2.md
~~~
```
git status -s更簡單地顯示狀態變化,如:顯示 M controllers/article.go顯示內容的每一行開頭,M表示被修改、A表示被增加、??表示未受控制
#### **3.7查看不同**
git diff readme.text 查看readme.txt這個文件中具體什么內容別修改了。其中:
git diff 查看工作區和暫存區的區別
git diff --cached 查看暫存區和版本庫之間的區別
git diff HEAD查看工作區和版本庫之間的區別
#### **3.8 查看提交日志**
git log查看倉庫版本庫中(本地倉庫和遠程倉庫的版本庫是一樣的)從最近到最遠的版本信息。換言之,即每次提交的信息。
git log --graph顯示版本以及分支圖
git reflog 查看歷史Git命令
#### **3.9隱藏工作**
當工作區的修改未提交,而我們郵箱將其隱藏起來時,就要用到git的stash功能
git stash 隱藏工作區和暫存區的修改,存儲到一個地方、
git stash list 查看存儲起來的工作現場
恢復工作現場有兩種方法:
一是用git stash apply恢復,但是恢復后,stash內容并不刪除,需要用git stash drop來刪除;另一種方式是用git stash pop,恢復的同時把stash內容也刪除了、。