1. 程式人生 > >git使用<一>:本地倉庫的常用操作

git使用<一>:本地倉庫的常用操作

碼農 ubun 所有 都是 chan 常用 rac master 添加文件

  編寫軟件,時常免不了修修改改,修改過後的代碼不一定比前面好,甚至產生新問題,或者有時無意間修改了某行代碼,導致出錯,這種情況都是很常見的,如果此時沒有版本管理,如果是小軟件可能沒什麽影響,如果代碼量很大,就是個很頭疼的問題,git的出現正是為了解決這個問題的,對於碼農來說,簡直是神器,下面簡單記錄下。

基本操作:
1.倉庫初始化:
直接進入文件夾,輸入git init
2.添加文件:
一個文件(比如x文件):git add x
多個文件(比如x,y文件):git add x y
整個文件夾文件:git add *
3.提交文件:
一行註釋:git commit -m "you comment"
多行註釋:git commit -m ",然後按下回車,輸入內容,然後繼續輸入內容,輸入完之後輸入"


4.查看狀態:
git status
5.查看日誌:
查看版本日誌:git log
查看所有操作日誌:git reflog
6.查看異同:
僅僅查看:git diff
制作patch:git diff > diff.patch
7.版本回滾與切換:
先執行git log確定要回滾到的版本,然後使用git reset --hard進行回滾;如果回滾後突然又想回到之前的版本,怎麽辦呢?這個時候只需要再次查看日誌,不過需要使用git reflog,然後再次git reset --hard就可以恢復到之前的版本
操作實例:

[email protected]:tst$ git init        # 初始化
Initialized empty Git repository 
in /samba/TMP/tst/.git/ [email protected]:tst$ vi tst.txt [email protected]:tst$ git status # 查看狀態 On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) tst.txt nothing added to commit but untracked files present (use
"git add" to track) [email protected]:tst$ git add tst.txt # 添加文件 [email protected]:tst$ git commit -m " # 提交文件 > first commit > " [master (root-commit) 767ba26] first commit 1 file changed, 1 insertion(+) create mode 100644 tst.txt [email protected]:tst$ git log # 查看日誌 commit 767ba26cc39de73ab2848680058a339341599fe8 Author: Your Name <[email protected]> Date: Thu Aug 24 22:11:56 2017 +0800 first commit [email protected]:tst$ vi tst.txt [email protected]:tst$ git diff # 查看修改 diff --git a/tst.txt b/tst.txt index d00491f..48082f7 100644 --- a/tst.txt +++ b/tst.txt @@ -1 +1 @@ -1 +12 [email protected]:tst$ 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: tst.txt no changes added to commit (use "git add" and/or "git commit -a") [email protected]:tst$ git add tst.txt # 再次添加修改後的文件 [email protected]:tst$ git commit -m "second commit" # 提交修改 [master be932f8] second commit 1 file changed, 1 insertion(+), 1 deletion(-) [email protected]:tst$ git log # 查看日誌 commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5 Author: Your Name <[email protected]> Date: Thu Aug 24 22:16:21 2017 +0800 second commit commit 767ba26cc39de73ab2848680058a339341599fe8 Author: Your Name <[email protected]> Date: Thu Aug 24 22:11:56 2017 +0800 first commit [email protected]:tst$ git reflog # 查看所有操作日誌 be932f8 [email protected]{0}: commit: second commit 767ba26 [email protected]{1}: commit (initial): first commit [email protected]:tst$ git reset --hard 767ba26 # 回滾到第一次提交 HEAD is now at 767ba26 first commit [email protected]:tst$ git log # 查看日誌 commit 767ba26cc39de73ab2848680058a339341599fe8 Author: Your Name <[email protected]> Date: Thu Aug 24 22:11:56 2017 +0800 first commit [email protected]:tst$ git reflog # 查看所有操作日誌 767ba26 [email protected]{0}: reset: moving to 767ba26 be932f8 [email protected]{1}: commit: second commit 767ba26 [email protected]{2}: commit (initial): first commit [email protected]:tst$ git reset --hard be932f8 # 回滾到第二次提交 HEAD is now at be932f8 second commit [email protected]:tst$ git log commit be932f8fee8cfa266e0c2051905ff428d2f8bdb5 Author: Your Name <[email protected]> Date: Thu Aug 24 22:16:21 2017 +0800 second commit commit 767ba26cc39de73ab2848680058a339341599fe8 Author: Your Name <[email protected]> Date: Thu Aug 24 22:11:56 2017 +0800 first commit [email protected]:tst$

git使用<一>:本地倉庫的常用操作