git學習筆記


git學習

1. 創建倉庫並提交

初始化全局配置

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

創建版本庫

root@kali:~/Documents# mkdir test
root@kali:~/Documents# cd test/
root@kali:~/Documents/test# git init
Initialized empty Git repository in /root/Documents/test/.git/
root@kali:~/Documents/test# ls -la
total 12
drwxr-xr-x 3 root root 4096 Jul 13 10:40 .
drwxr-xr-x 4 root root 4096 Jul 13 10:40 ..
drwxr-xr-x 7 root root 4096 Jul 13 10:40 .git
root@kali:~/Documents/test# 

看到,多了一個.git文件夾,輕易不要修改

把文件添加到版本庫

root@kali:~/Documents/test# echo "this is first commit" >> README.md
root@kali:~/Documents/test# git add README.md 
root@kali:~/Documents/test# git commit -m "first commit"
[master (root-commit) 5399ca6] first commit
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 README.md
root@kali:~/Documents/test# 

commit 可以一次提交多個文件,所以可以經過多次add單個文件后一起commit

git add其實是將修改加到緩存區,而commit是將緩存區的修改提交給倉庫

查看改變

root@kali:~/Documents/test# echo "second commot" >> README.md 
root@kali:~/Documents/test# 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# 

可以看到README.md已經被修改,但是還不知道被做了哪些修改

查看具體修改內容

root@kali:~/Documents/test# git diff README.md
diff --git a/README.md b/README.md
index 2ed441a..b8254f4 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 this is first commit
+second commot
root@kali:~/Documents/test# 

可以看到,修改內容是在第二行加了second commot

再次提交到倉庫

root@kali:~/Documents/test# git add README.md

無輸出

git commit提交

root@kali:~/Documents/test# git commit -m "add second"
[master 22bae0c] add second
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)

查看當前狀態

root@kali:~/Documents/test# git status
On branch master
nothing to commit, working tree clean
root@kali:~/Documents/test# 

git告訴我們當前沒有需要提交的內容

2. 版本回退

查看歷史版本

root@kali:~/Documents/test# git log
commit 5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master)
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 11:03:42 2018 -0400

    add third

commit 22bae0cda96e33739fcf0a7e7b2a459130784839
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:59:07 2018 -0400

    add second

commit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:50:11 2018 -0400

    first commit
root@kali:~/Documents/test# 
root@kali:~/Documents/test# git log --pretty=oneline  # 單行輸出,美觀直觀
5ccca74437dd2943368adbd7b46776e45e87e76d (HEAD -> master) add third
22bae0cda96e33739fcf0a7e7b2a459130784839 add second
5399ca651eeb6c295332cd117be0ed5d72c4dfa8 first commit
root@kali:~/Documents/test# 

可以看到有三次提交

commit后的字符串是commit id

回滾到上一版本

git中,HEAD表示當前版本,HEAD是上一個版本,HEAD^是上上一版本,HEAD~100是上100個版本

root@kali:~/Documents/test# git reset --hard HEAD^
HEAD is now at 22bae0c add second
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
root@kali:~/Documents/test# git log
commit 22bae0cda96e33739fcf0a7e7b2a459130784839 (HEAD -> master)
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:59:07 2018 -0400

    add second

commit 5399ca651eeb6c295332cd117be0ed5d72c4dfa8
Author: root <root@localhost.localdomain>
Date:   Fri Jul 13 10:50:11 2018 -0400

    first commit
root@kali:~/Documents/test# 

返回原版本

root@kali:~/Documents/test# git reflog  # git命令歷史紀錄,可以找到commit id
5ccca74 (HEAD -> master) HEAD@{0}: reset: moving to 5ccca
22bae0c HEAD@{1}: reset: moving to HEAD^
5ccca74 (HEAD -> master) HEAD@{2}: commit: add third
22bae0c HEAD@{3}: commit: add second
5399ca6 HEAD@{4}: commit (initial): first commit
root@kali:~/Documents/test# git reset --hard 5ccca
HEAD is now at 5ccca74 add third
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test# 

3. 工作區和緩存區

本地文件夾就是工作區,.git目錄就是版本庫,

git add命令實際上是把修改放到緩存區,然后git commit會將緩存區的所有修改一次性提交到分支。

4. 管理修改

修改后add, 然后再修改,然后直接commit, 這時第二次修改並不會被提交

5. 撤銷修改

root@kali:~/Documents/test# echo "Error" >> README.md 
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
Error
root@kali:~/Documents/test# 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- README.md
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test# git status
On branch master
nothing to commit, working tree clean
root@kali:~/Documents/test# 

git checkout -- 其實就是讓文件回到最近一次 commitadd的狀態

如果修改已經add到緩存區,則怎么辦呢???

root@kali:~/Documents/test# echo "Error" >> README.md 
root@kali:~/Documents/test# git add README.md 
root@kali:~/Documents/test# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   README.md

root@kali:~/Documents/test# git reset HEAD README.md
Unstaged changes after reset:
M	README.md
root@kali:~/Documents/test# 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.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- README.md
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test# 

先git reset HEAD README.md,再git checkout -- README.md

6. 刪除文件

真刪除

root@kali:~/Documents/test# touch test.txt
root@kali:~/Documents/test# git add test.txt 
root@kali:~/Documents/test# git commot -m "add test.txt"
git: 'commot' is not a git command. See 'git --help'.

The most similar command is
	commit
root@kali:~/Documents/test# 
root@kali:~/Documents/test# rm test.txt 
root@kali:~/Documents/test# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   test.txt

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:    test.txt

root@kali:~/Documents/test# git rm test.txt 
rm 'test.txt'
root@kali:~/Documents/test# git commit -m "remove test.txt"
On branch master
nothing to commit, working tree clean
root@kali:~/Documents/test# 

先rm 本地文件,再git rm 倉庫文件,最后commit

誤刪

root@kali:~/Documents/test# touch test.md
root@kali:~/Documents/test# git add test.md 
root@kali:~/Documents/test# git commit -m "add test.md"
[master a62fe29] add test.md
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.md
root@kali:~/Documents/test# 
root@kali:~/Documents/test# rm test.md 
root@kali:~/Documents/test# git status
On branch 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:    test.md

no changes added to commit (use "git add" and/or "git commit -a")
root@kali:~/Documents/test# git checkout -- test.md
root@kali:~/Documents/test# ls
README.md  test.md
root@kali:~/Documents/test# 

直接用git checkout -- test.md還原即可

7. 添加遠程倉庫

可以把本地已有的倉庫和github庫關聯

git remote add origin https://github.com/b4zinga/Lance.git

將所有內容推送到遠程倉庫

git push -u origin master

第一次推送master時,加上-u參數,Git不但會把本地的master分支內容推送的遠程新的master分支,還會把本地的master分支和遠程的master分支關聯起來,在以后的推送或者拉取時就可以簡化命令。

以后只要本地有commit,就可以通過git push origin master將本地master分支的最新修改推送至github.

8. 從遠程庫克隆

git clone https://github.com/b4zinga/Lance.git

9. 創建與合並分支

創建並切換到dev分支

root@kali:~/Documents/test# git checkout -b dev
Switched to a new branch 'dev'
root@kali:~/Documents/test# 

-b 參數表示創建並切換

相當於git branch dev 創建分支,然后git checkout dev轉換分支兩條命令

查看當前分支

root@kali:~/Documents/test# git branch
* dev
  master
root@kali:~/Documents/test# 

當前分支前會有*

root@kali:~/Documents/test# echo "new beanch" >> README.md 
root@kali:~/Documents/test# git add README.md 
root@kali:~/Documents/test# git commit -m "branch"
[dev bae88b5] branch
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
root@kali:~/Documents/test# 
root@kali:~/Documents/test# git checkout master
Switched to branch 'master'
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
root@kali:~/Documents/test#

在dev分支修改,提交后轉換到master,可以看到master上文件無變化

合並分支

root@kali:~/Documents/test# git merge dev
Updating a62fe29..bae88b5
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
root@kali:~/Documents/test# cat README.md 
this is first commit
second commot
third commit
new beanch
root@kali:~/Documents/test# 

git merge用於將指定分支合並到當前分支

Fast-forward指本次合並是“快進模式”

刪除分支

root@kali:~/Documents/test# git branch -d dev
Deleted branch dev (was bae88b5).
root@kali:~/Documents/test# git branch
* master
root@kali:~/Documents/test# 

reference: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM