2、獨自使用Git時的常見場景


2.1、怎么刪除不需要的分支

##查看所有的分支 $ git branch -av

##刪除不想要的分支
$ git branch -D 分支名

 

2.2、怎么修改最新commit的message

##查看最近一次提交的message $ git log -1 ##更改最近一次提交的message $ git commit --amend

 

2.3、怎么修改老舊commit的message

##首先選定某一個分支(代碼沒有集成) ##查看該分支最近3次的提交日志 $ git log -3 commit 3e995db3abc31311c43a15f509592e36ca043746 (HEAD -> master) Author: = <dy201@163.com> Date: Sun Mar 24 10:16:20 2019 +0800 filename: readme -> readme.md commit e722d48b53663e4a915306ba6cc4eee6b99a168c // 要修改的commit Author: = <dy201@163.com> Date: Sat Mar 23 11:33:31 2019 +0800 second time vim commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d // 修改commit的父節點 Author: = <dy201@163.com> Date: Sat Mar 23 11:29:47 2019 +0800 add js ##如果要修改一個分支,用變基修改其父節點 $ git rebase -i 6cb9d1bc3d [detached HEAD 8e1c396] reword: eecond time vim Author: = <dy201@163.com> Date: Sat Mar 23 11:33:31 2019 +0800
 1 file changed, 2 insertions(+) Successfully rebased and updated refs/heads/master. ##再次查看提交日志,發現commit的值變了 commit bd153a0d51b46ddc6c48a1fa0ddbec4f3a9e0339 (HEAD -> master) Author: = <dy201@163.com> Date: Sun Mar 24 10:16:20 2019 +0800 filename: readme -> readme.md commit 8e1c39611954b20082d30c2b841c296c07591739 Author: = <dy201@163.com> Date: Sat Mar 23 11:33:31 2019 +0800 reword: eecond time vim commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d Author: = <dy201@163.com> Date: Sat Mar 23 11:29:47 2019 +0800 add js

 

執行

$ git rebase -i 彈出的窗口

  pick:表示選定,不改變

 reword:表示修改commit message,繼續提交

2.4、怎么把連續的多個commit整理成1個

##將前面提交的web相關的代碼整合為一個commit ##查看log日志 $ git log commit bd153a0d51b46ddc6c48a1fa0ddbec4f3a9e0339 (HEAD -> master) Author: = <dy201@163.com> Date: Sun Mar 24 10:16:20 2019 +0800 filename: readme -> readme.md commit 8e1c39611954b20082d30c2b841c296c07591739 Author: = <dy201@163.com> Date: Sat Mar 23 11:33:31 2019 +0800 reword: eecond time vim commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d Author: = <dy201@163.com> Date: Sat Mar 23 11:29:47 2019 +0800 add js commit 8b16af839848fa063920a9d9d4e9550a57f19c25 Author: = <dy201@163.com> Date: Sat Mar 23 11:26:40 2019 +0800 add style.css commit f7c30fa886d41c6c13010b45a3544e15b5fc930b Author: dy201 <dy201@163.com> Date: Sat Mar 23 11:21:01 2019 +0800 Add index + logo commit 226b8bf13841275f3404945a3853f1b4d1adc28c Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800 add readme #變基rebase進行合並 $ git rebase -i 226b8bf1384127 [detached HEAD 534c24a] create a complete web project. Date: Sat Mar 23 11:21:01 2019 +0800
 4 files changed, 6 insertions(+) create mode 100644 images/git-logo.png create mode 100644 index.html create mode 100644 js/script.js create mode 100644 styles/style.css Successfully rebased and updated refs/heads/master.

本次進入rebase后,用的的是 sqush s

成功后的截圖:

 

2.5、怎么把間隔的幾個commit整理成1個

#查看 commit log日志 $ git log commit d08007d0d9e3e992e857df52d203cf28b66b19cd (HEAD -> master) Author: = <dy201@163.com> Date: Sun Mar 24 10:16:20 2019 +0800 filename: readme -> readme.md commit 534c24afd0e12181b54fc5c71470eaa698fcbc45 Author: dy201 <dy201@163.com> Date: Sat Mar 23 11:21:01 2019 +0800 create a complete web project. Add index + logo add style.css add js reword: eecond time vim commit 226b8bf13841275f3404945a3853f1b4d1adc28c Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800 add readme ##將1、3兩條進行合並 $ git rebase -i 226b8bf13841 interactive rebase in progress; onto 226b8bf Last command done (1 command done): pick 226b8bf Next commands to do (2 remaining commands): squash d08007d filename: readme -> readme.md pick 534c24a create a complete web project. You are currently rebasing branch 'master' on '226b8bf'. nothing to commit, working tree clean The previous cherry-pick is now empty, possibly due to conflict resolution. If you wish to commit it anyway, use: git commit --allow-empty Otherwise, please use 'git reset' Could not apply 226b8bf... ##遇到異常,查看狀態 $ git status interactive rebase in progress; onto 226b8bf Last command done (1 command done): pick 226b8bf Next commands to do (2 remaining commands): squash d08007d filename: readme -> readme.md pick 534c24a create a complete web project. (use "git rebase --edit-todo" to view and edit) You are currently rebasing branch 'master' on '226b8bf'. (all conflicts fixed: run "git rebase --continue") nothing to commit, working tree clean ##繼續rebase提交 $ git rebase --continue [detached HEAD b1cd002] add redeme.md Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800
 1 file changed, 2 insertions(+) create mode 100644 readme.md Successfully rebased and updated refs/heads/master. ##成功后,查看commit 日志,或者gitk commit 80e5790512bb212e8aebbf5f0568e0277405907f (HEAD -> master) Author: dy201 <dy201@163.com> Date: Sat Mar 23 11:21:01 2019 +0800 create a complete web project. Add index + logo add style.css add js reword: eecond time vim commit b1cd002518c1c118dae414df7c366581df92a9a2 Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800 add redeme.md add readme filename: readme -> readme.md

 

// 因為這個commit是 根,所以我們在前面加上了它自己的commit值 

//(因為提交信息為空,所以導致后面出了一點異常,異常內容: git commit --allow-empty)

// 1、3兩個合並的時候,將第三天的信息剪切到第一條下面

## 調整位置后,前面兩條進行合並,使用 s 參數

 

2.6、怎么比較暫存區的HEAD所含文件的差異

# 修改一index.html文件並提交 $ vi index.html $ git add index.html #查看提交狀態
$ git status On branch master Changes to be committed: (use
"git reset HEAD <file>..." to unstage) modified: index.html # 查看 暫存區 和 HEAD 的差異 (紅色表示刪除,綠色表示添加,我的只有添加)
$ git
diff --cached diff --git a/index.html b/index.html index f8b0da4..27d17c3 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,5 @@ this is a index.html this is second time + +this third time : diff between commit and HEAD

 

2.7、怎么比較工作區和暫存區所含文件的差異

# 修改 index.html 、 css文件 # index.html 已經add, css 沒有進行操作 $ git add index.html # 使用git diff 可以查看修改未添加的css 和 commit的css 的區別 $ git diff # 加參數可對具體的文件做比較 $ git diff -- 文件名

 

2.8、如何讓暫存區恢復成和HEAD的一樣

# 查看git狀態
$ git status On branch master Changes to be committed: (use
"git reset HEAD <file>..." to unstage) modified: index.html modified: readme.md modified: styles/style.css #暫存區恢復到工作區
$ git reset HEAD Unstaged changes after reset: M index.html M readme.md M styles
/style.css #再次查看狀態
$ 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: index.html modified: readme.md modified: styles/style.css no changes added to commit (use "git add" and/or "git commit -a")

 

2.9、如果讓工作區的文件恢復為和暫存區一樣

# index文件已經提交到了暫存區,工作區的做了修改 $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)  modified: index.html 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: index.html modified: readme.md modified: styles/style.css # 用 暫存區 恢復 工作區 $ git checkout -- index.html #再次查看狀態 $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: index.html 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 modified: styles/style.css

 

2.10、怎么取消暫存區部分文件的更改

# 查看暫存區文件 $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)  modified: index.html modified: readme.md modified: styles/style.css # 將暫存區文件恢復到工作區 $ git reset HEAD -- styles/style.css Unstaged changes after reset: M styles/style.css #查看修改后的狀態 $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)  modified: index.html modified: readme.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: styles/style.css

 

2.11、清除最近的幾次提交

# 切換到temp分支 $ git checkout temp Switched to branch 'temp' #查看分支提交日志 $ git log --graph * commit ff49d0f0abaf35015bd308f2a30f8fcd4dd16611 (HEAD -> temp) | Author: = <dy201@163.com>
| Date:   Sun Mar 24 10:49:57 2019 +0800
|
| add test |
* commit 3966a6773a812ecdd8ce93b26b68340341731538 | Author: = <dy201@163.com>
| Date:   Sun Mar 24 10:16:20 2019 +0800
|
|      readme -> readme.md |
* commit e722d48b53663e4a915306ba6cc4eee6b99a168c | Author: = <dy201@163.com>
| Date:   Sat Mar 23 11:33:31 2019 +0800
|
|     second time vim |
* commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d | Author: = <dy201@163.com>
| Date:   Sat Mar 23 11:29:47 2019 +0800
|
| add js |
* commit 8b16af839848fa063920a9d9d4e9550a57f19c25 | Author: = <dy201@163.com>
| Date:   Sat Mar 23 11:26:40 2019 +0800
|
| add style.css |
* commit f7c30fa886d41c6c13010b45a3544e15b5fc930b | Author: dy201 <dy201@163.com>
| Date:   Sat Mar 23 11:21:01 2019 +0800
|
|     Add index + logo |
* commit 226b8bf13841275f3404945a3853f1b4d1adc28c Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800 add readme #恢復到 second time節點 $ git reset --hard e722d48b53663 HEAD is now at e722d48 second time vim #再次查看日志

 

2.12、看看不同提交的指定文件的差異

# 查看所有分支的提交日志 $ git log -n8 --all --graph *   commit 9cf6ffd4f0c0c51ff3d99ad572512cd9df210ec2 (refs/stash) |\ Merge: 5cd42df f19ddbd | | Author: dy201 <dy201@163.com>
| | Date:   Tue Mar 26 16:31:18 2019 +0800
| |
| | WIP on master: 5cd42df Add the first git command with config | |
| * commit f19ddbd5716b0cbff742bf7913f06ab2ad4f3290 |/  Author: dy201 <dy201@163.com>
|   Date:   Tue Mar 26 16:31:18 2019 +0800
|
| index on master: 5cd42df Add the first git command with config |
* commit 5cd42df24f830d4e70f0235083447ae7e225e108 (master) | Author: dy201 <dy201@163.com>
| Date:   Tue Mar 26 15:09:21 2019 +0800
|
| Add the first git command with config |
* commit 80e5790512bb212e8aebbf5f0568e0277405907f | Author: dy201 <dy201@163.com>
| Date:   Sat Mar 23 11:21:01 2019 +0800
|
| create a complete web project. |
|     Add index + logo |
| add style.css |
| add js |
|     reword: eecond time vim |
* commit b1cd002518c1c118dae414df7c366581df92a9a2 Author: k <dy201@163.com> Date: Fri Mar 22 23:21:29 2019 +0800 add redeme.md add readme filename: readme -> readme.md * commit c412e25118f7164f082717824f5d790d6b3bd8c2 (css-fix) | Author: dy201 <dy201@163.com>
| Date:   Sun Mar 24 23:37:24 2019 +0800
|
|     Backgroud: orange -> green |
| * commit e722d48b53663e4a915306ba6cc4eee6b99a168c (HEAD -> temp) | | Author: = <dy201@163.com>
| | Date:   Sat Mar 23 11:33:31 2019 +0800
| |
| |     second time vim | |
| * commit 6cb9d1bc3df731bc491040f90fbbd9a0524ed60d |/  Author: = <dy201@163.com>
|   Date:   Sat Mar 23 11:29:47 2019 +0800
|
| add js # 查看兩個分支之間的區別(也可以commit id 代替分支名) $ git diff temp master -- 文件名

 

2.13、正確刪除文件的方法

# 刪除 readme 文件 $ git rm readme rm 'readme' #查看狀態 $ git status On branch temp Changes to be committed: (use "git reset HEAD <file>..." to unstage)  deleted: readme

 

2.14、開發中臨時加塞了緊急任務

# git stash apply 把之前的任務騰出來,放到工作區 # stash堆棧里面的任務還在,而使用pop,任務就沒有了 # 清理 $ git status On branch temp nothing to commit, working tree clean #查看清理堆棧存放的數據 $ git stash list stash@{0}: WIP on temp: e722d48 second time vim stash@{1}: WIP on temp: e722d48 second time vim stash@{2}: WIP on master: 5cd42df Add the first git command with config #使用apply恢復 $ git stash apply On branch temp 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: index.html no changes added to commit (use "git add" and/or "git commit -a") #使用pop恢復 $ git stash pop On branch temp 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: index.html no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (11bdc7d85534a3359c431a699ab17d092b7ee077)

 

2.15、如何指定不需要Git管理的文件

 

# 某些不用納入版本控制系統 https://github.com/github/gitignore
 # 不需要git管理的文件必須放在.gitignore里面 # 如果是一個文件夾,加不加'/'都可以 # 如果是一個文件必須相匹配

 

2.16、如何將Git倉庫備份到本地

 

啞協議與智能協議: 直觀區別:啞協議傳輸進度不可見;智能協議傳輸可見。 傳輸速度:智能協議比啞協議傳輸速度快。 # 創建一個裸倉庫 k@k-PC MINGW64 /user/dy201/666-backup $ git clone --bare /user/dy201/
101-GitRunner/ 666-backup/ #使用啞協議 k@k-PC MINGW64 /user/dy201/666-backup $ git clone --bare /user/dy201/101-GitRunner/git_learning/.git ya.git Cloning into bare repository 'ya.git'... done. #使用智能協議 k@k-PC MINGW64 /user/dy201/666-backup $ git clone --bare file:///user/dy201/101-GitRunner/git_learning/.git zhineng.git
Cloning into bare repository 'zhineng.git'... remote: Enumerating objects: 30, done. remote: Counting objects: 100% (30/30), done. remote: Compressing objects: 100% (17/17), done. remote: Total 30 (delta 6), reused 0 (delta 0) Receiving objects: 100% (30/30), done. Resolving deltas: 100% (6/6), done. # 和遠端倉庫建立聯系 平時做的變更同步到遠端
k@k-PC MINGW64 /user/dy201/101-GitRunner/git_learning (temp)
$ git remote add zhineng file:///user/dy201/666-backup/zhineng.git

# push操作(本地信息及時更新到遠端) $ git push --set-upstream zhineng temp Everything up-to-date Branch 'temp' set up to track remote branch 'temp' from 'zhineng'.

 


免責聲明!

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



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