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'.