參考資料:Git教程
- 創(chuàng)建一個(gè)空目錄
#添加文件夾
$ mkdir learngit
↓
#索引到該文件夾
$ cd learngit
↓
#查看添加結(jié)果
$ pwd
--->/c/Users/Xiaoji/learngit
- 通過(guò)
git init
命令把這個(gè)目錄變成Git可以管理的倉(cāng)庫(kù)
$ git init
--->Initialized empty Git repository in C:/Users/Xiaoji/learngit/.git/
- 把文件添加到倉(cāng)庫(kù)
#編寫一個(gè)readme.txt文件,內(nèi)容如下:
Git is a version control system.
Git is free software.
#!一定要放到learngit目錄下(子目錄也行),因?yàn)檫@是一個(gè)Git倉(cāng)庫(kù)招拙,放到其他地方Git再厲害也找不到這個(gè)文件
↓
#用命令git add告訴Git讯壶,把文件添加到倉(cāng)庫(kù):
$ git add readme.txt
↓
# 用命令git commit告訴Git裹赴,把文件提交到倉(cāng)庫(kù):
$ git commit -m 'wrote a readme.txt' #-m后面輸入的是本次提交的說(shuō)明缝其,可以輸入任意內(nèi)容
--->
[master (root-commit) 37424cf] wrote a readme.txt
1 file changed, 2 insertions(+) #1 file changed:1個(gè)文件被改動(dòng),2 insertions:插入了兩行內(nèi)容
create mode 100644 readme.txt
tips:
#可以多次add不同的文件搜变,commit可以一次提交很多文件
$ git add <file>:$ git add file2.txt file3.txt
$ git commit -m "add 2 files."
- 修改文件內(nèi)容并提交:
# 繼續(xù)修改readme.txt文件缅茉,改成如下內(nèi)容:
Git is a distributed version control system.
Git is free software.
#運(yùn)行g(shù)it status命令看看結(jié)果:
$ 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被修改過(guò)了,但還沒(méi)有準(zhǔn)備提交的修改
# 用git diff這個(gè)命令看看具體修改了什么內(nèi)容
$ git diff
--->
diff --git a/readme.txt b/readme.txt
index d8036c1..013b5bc 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system. #添加了一個(gè)distributed單詞
Git is free software.
\ No newline at end of file
# 提交修改后的文件
$ git add readme.txt
$ git commit -m "add distributed"
# 再次查看結(jié)果:
$ git status
--->
On branch master
nothing to commit, working tree clean #Git告訴我們當(dāng)前沒(méi)有需要提交的修改视乐,而且洛搀,工作目錄是干凈(working tree clean)的
tips:
git diff #是工作區(qū)(work dict)和暫存區(qū)(stage)的比較
git diff --cached #是暫存區(qū)(stage)和分支(master)的比較
git diff HEAD -- file #比較的是工作區(qū)中的文件與版本庫(kù)中文件的差異
- 查看修改的歷史記錄
$ git log
--->
commit 10e3ff13cb1552ae791c84392b8e6eaed78e55f5 (HEAD -> master)
Author: Xiaoji <Xiaoji@Xiaoji>
Date: Thu Aug 16 18:24:59 2018 +0800
append GPL
commit 98f655214d25482fe581f6bfa5d73b83dfa849f4
Author: Xiaoji <Xiaoji@Xiaoji>
Date: Thu Aug 16 18:15:12 2018 +0800
add distributed
commit 37424cf2fb099229724cd19dfd68f63d4d73167d
Author: Xiaoji <Xiaoji@Xiaoji>
Date: Thu Aug 16 17:58:38 2018 +0800
wrote a readme.txt
-----------------------------------------------------------------------------------------------------
#簡(jiǎn)單版歷史記錄
$ git log --pretty=oneline
--->
10e3ff13cb1552ae791c84392b8e6eaed78e55f5 (HEAD -> master) append GPL
98f655214d25482fe581f6bfa5d73b83dfa849f4 add distributed
37424cf2fb099229724cd19dfd68f63d4d73167d wrote a readme.txt
- 退回到上一個(gè)版本
#HEAD表示當(dāng)前版本,HEAD^上一個(gè)版本,HEAD^^上上一個(gè)版本
#HEAD~100往上100個(gè)版本
$ git reset --hard HEAD^
--->
HEAD is now at 98f6552 add distributed
- 反悔了上面的退回操作,需要重新找回退回前的版本
# 1佑淀、有之前要退回到commit id:
$ git reset --hard 10e3ff
--->
HEAD is now at 10e3ff1 append GPL
# 2留美、暫時(shí)沒(méi)有之前的commit id:
$ git reflog
--->
10e3ff1 (HEAD -> master) HEAD@{0}: reset: moving to 10e3ff
98f6552 HEAD@{1}: reset: moving to HEAD^
10e3ff1 (HEAD -> master) HEAD@{2}: commit: append GPL
98f6552 HEAD@{3}: commit: add distributed
37424cf HEAD@{4}: commit (initial): wrote a readme.txt
↓
↓
↓
$ git reset --hard 10e3ff
--->
HEAD is now at 10e3ff1 append GPL
- 撤回修改
a. 文件修改后反悔了,但這時(shí)文件還沒(méi)有git add
渣聚,需要撤回
$ git checkout -- readme.txt #把readme.txt文件在工作區(qū)的修改全部撤銷,結(jié)果是回到和版本庫(kù)一模一樣的狀態(tài)独榴;
b. 修改的文件已經(jīng)git add
,但還沒(méi)有git commit
奕枝,需要撤回
# 1、先從暫存區(qū)撤回修改
$ git reset HEAD readme.txt
--->
Unstaged changes after reset:
M readme.txt
↓
↓
↓
# 1.5瓶堕、查看狀態(tài)
$ git status
--->
On branch master
Changes not staged for commit: #注意not staged
(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")
↓
↓
↓
# 2隘道、撤回修改到最新的commit狀態(tài)
$ git checkout -- readme.txt
- 刪除文件,撤回刪除
a. 版本庫(kù)的文件已從系統(tǒng)中刪除郎笆,需要在git中確認(rèn)
# 將刪除修改提交到暫存區(qū)
$ git rm test.txt
--->
rm 'test.txt'
↓
↓
↓
# 向git提交確認(rèn)刪除
$ git commit -m 'remove test.txt'
--->
[master c262779] remove test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
b. 版本庫(kù)的文件刪除錯(cuò)誤谭梗,還沒(méi)有git rm
,撤回:
$ git checkout -- test.txt
c. 版本庫(kù)的文件刪除錯(cuò)誤宛蚓,已經(jīng)git add``````git commit
激捏,撤回:
$ git reset --hard 62adb #62adb為版本號(hào)(修改記錄號(hào))
- 與github建立連接,將本地倉(cāng)庫(kù)推送到github
# 在github創(chuàng)建一個(gè)遠(yuǎn)程倉(cāng)庫(kù)后凄吏,將本地倉(cāng)庫(kù)與之相連接
$ git remote add origin https://github.com/junzhu-github/learngit.git
↓
↓
↓
# 把本地庫(kù)的所有內(nèi)容推送到遠(yuǎn)程庫(kù)上
$ git push -u origin master #第一次推送時(shí)远舅,要加上了-u參數(shù)
--->
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 2 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (19/19), 1.45 KiB | 105.00 KiB/s, done.
Total 19 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/junzhu-github/learngit.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
# 此后,每次本地提交后痕钢,推送最新修改使用:
$ git push origin master
# 查看遠(yuǎn)程庫(kù)的信息
$ git remote
--->
origin
# or 需要更詳細(xì)信息
$ git remote -v
--->
origin git@github.com:junzhu-github/gitskills (fetch)
origin git@github.com:junzhu-github/gitskills (push)
- 從遠(yuǎn)程庫(kù)克隆
# 在github新建倉(cāng)庫(kù)gitskills后图柏,克隆到本地
$ git clone git@github.com:junzhu-github/gitskills
--->
Cloning into 'gitskills'...
The authenticity of host 'github.com (198.19.0.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,198.19.0.112' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
- git分支的創(chuàng)建、使用任连、刪除
# 創(chuàng)建并切換到分支
$ git checkout -b dev
--->
Switched to a new branch 'dev'
#equal to
$ git branch dev
$ git checkout dev
--->
Switched to branch 'dev'
↓
↓
↓
# 查看當(dāng)前分支
$ git branch
--->
* dev
master
↓
↓
↓
# 提交文件修改 # 蚤吹!改變的只是分支上的文件內(nèi)容,master的文件未改變
$ git add README.md
$ git status
--->
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
↓
↓
↓
# 把dev分支的工作成果合并到master分支上随抠,master文件內(nèi)容改變
$ git merge dev
--->
Updating 482d191..1a83f8d
Fast-forward
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
↓
↓
↓
# 合并完成后裁着,就可以放心地刪除dev分支了
$ git branch -d dev
--->
Deleted branch dev (was 1a83f8d).
↓
↓
↓
# 刪除后,查看branch拱她,就只剩下master分支了
$ git branch
--->
* master
↓
↓
↓
# 切換回master分支
$ git checkout master
--->
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
- 忽略特殊文件
a. 在Git工作區(qū)的根目錄下創(chuàng)建一個(gè)特殊的.gitignore文件二驰,然后把要忽略的文件名填進(jìn)去,Git就會(huì)自動(dòng)忽略這些文件
b. 把.gitignore也提交到Git
tips: 使用Windows的童鞋注意了椭懊,如果你在資源管理器里新建一個(gè).gitignore文件诸蚕,它會(huì)非常弱智地提示你必須輸入文件名步势,但是在文本編輯器里“保存”或者“另存為”就可以把文件保存為.gitignore了。
- 配置別名
# 增加別名
$ git config --global alias.st status #--global是針對(duì)當(dāng)前用戶起作用的背犯,如果不加坏瘩,那只針對(duì)當(dāng)前的倉(cāng)庫(kù)起作用
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 刪除別名
git config --global --unset alias.ci
查看配置文件
# 當(dāng)前倉(cāng)庫(kù)的local配置文件
$ cat .git/config
# 用戶配置文件
$ cat .gitconfig