关于微博上一次git的讨论
2017-03-20 本文已影响9人
我的名字叫浩仔
原博引起了大家的吐槽:
Paste_Image.png从微博看不出该修改是否已经 commit ,在本地测试这几种可能:
未add 之前,切换分支 修改的代码会丢失;
add 之后,commit之前,切换分支 修改的代码丢失;
[AntLinkCampus] git status 14:01:51 ☁ develop ☀ 𝝙
On branch develop
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: CampusServer/apps/web/conf/app.conf
no changes added to commit (use "git add" and/or "git commit -a")
[AntLinkCampus] git add . 14:02:38 ☁ develop ☂ ⚡ 𝝙
[AntLinkCampus] git status 14:02:50 ☁ develop ☂ ✚ 𝝙
On branch develop
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: CampusServer/apps/web/conf/app.conf
commit 之后;
- 新建分支 git branch <new-branch>
- git reset HEAD^ 恢复上一个版本,
- git checkout -- <file> 取消更改
- 切换分支 git checkout <new-branch>
- 提交自己的更改
[AntLinkCampus] git reset HEAD^ 14:27:54 ☁ develop ☀ 𝝙
Unstaged changes after reset:
M CampusServer/apps/web/conf/app.conf
[AntLinkCampus] git status 14:28:17 ☁ develop ☂ ⚡ 𝝙
On branch develop
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: CampusServer/apps/web/conf/app.conf
no changes added to commit (use "git add" and/or "git commit -a")
[AntLinkCampus] git checkout -- CampusServer/apps/web/conf/app.conf 14:28:50 ☁ develop ☂ ⚡ 𝝙
[AntLinkCampus] git status 14:29:00 ☁ develop ☀ 𝝙
On branch develop
nothing to commit, working directory clean
也可以使用以下方法:利用 git stash
[AntLinkCampus] git stash 14:52:44 ☁ develop ☂ ⚡ 𝝙
Saved working directory and index state WIP on develop: e3be7d3 update
HEAD is now at e3be7d3 update
[AntLinkCampus] git checkout -b ch 14:52:59 ☁ develop ☀ 𝝙
Switched to a new branch 'ch'
[AntLinkCampus] git status 14:53:08 ☁ ch ☀
On branch ch
nothing to commit, working directory clean
[AntLinkCampus] git stash pop 14:53:35 ☁ ch ☀
On branch ch
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: CampusServer/apps/web/conf/app.conf
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (e7a4c7e7a126d47ace94184d7a9443eff41d7e84)
[AntLinkCampus] git add . 14:54:01 ☁
[AntLinkCampus] git commit -m 'update' 14:54:01 ☁
切回主分支撤销修改
[AntLinkCampus] git checkout develop 14:54:01 ☁ ch ☂ ⚡
M CampusServer/apps/web/conf/app.conf
Switched to branch 'develop'
[AntLinkCampus] git status 14:55:26 ☁ develop ☂ ⚡ 𝝙
On branch develop
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: CampusServer/apps/web/conf/app.conf
no changes added to commit (use "git add" and/or "git commit -a")
[AntLinkCampus] git checkout -- CampusServer/apps/web/conf/app.conf 14:55:33 ☁ develop ☂ ⚡ 𝝙