撤销提交信息
有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令来重新提交:
有时候执行完git commit -m "错误信息"
, 发现提交信息写错了,或者格式不对想要修改那么可以使用如下命令:
在提交命令下添加--amend
选项
git commit --amend
例子:
git commit -m '错的提交信息'
git add xxx.file // 如果本地没有再次修改可不执行
git commit --amend // 会让你在控制台重新编辑修改信息
git push // 最后只有一条提交信息
如果上次提交没填错,只是在追加提交内容:
git add a.file
git commit -m '提交信息'
git add b.file
git commit --amend --no-edit // 不会在控制台重新编辑
git push // 最后保留一条提交信息
Stash 存储|暂存
使用场景1:
有时候我们早上来写代码,写了一会突然想起来忘记pull了,这时候线上可能还有新的代码没有更新,怎么办别慌
执行
-
git stash save "填写一些信息方便之后查找"
也可以直接
git stash
这样就是不太好查找执行多了不易区分然后发现今天修改的内容都消失了
执行
git status
后没有看到修改信息这时我们今天修改的已经成功保存到暂存区了
使用
git stash show
可以看到刚才保存的文件使用
git stash list
可以查看历史保存的记录 -
然后我们就可以执行
git pull
去更新线上的代码了 -
更新后记得把今天的代码放出来
git statsh pop
如果暂存区有多个存储则默认使用第一个即
git status pop stash@{0}
如果想指定应用某个存储则用git status pop stash@{n}
使用场景2:
当写了半天代码后发现这个功能不是当前分支的,需要切换分之,这时只有把场景1的第2步 改成 git checkout 分支名
– 扩展
git stash apply
应用某个存储,但不会把存储从存储列表中删除,默认使用第一个存储,即stash@{0},如果要使用其他个,git stash apply stash@{n} , 比如第二个存储:git stash apply stash@{1}
git stash drop
stash@{n} 从列表中删除这个存储
git stash clear
删除所有缓存的stash
上传已有项目到GitHub
1、cd 到工程目录下, 初始化仓库
git init
2、 将项目文件添加到索引
git add .
3、 提交到本地仓库
git commit -m "init"
4、关联本地和远程仓库
git remote add origin git@github.com:xxx.git
5、 push到远程仓库
git push origin master
此时报错:[rejected] master -> master (non-fast forward)
6、把远程仓库和本地同步,消除差异
git pull origin master --allow-unrelated-histories
7、重新push
git push origin master
8、OK!
撤销本地修改
git restore
修改 README.md 文件
执行 git status 查看
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
看到git 给的提示有 git restore <file> ...
表示放弃工作区的修改
我们在控制台执行
git restore README.md
再次 执行 git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
发现工作区已经清理干净了
README.md 回到了修改前的状态
git restore —staged
我们再次修改 README.md
这次我们将 README.md 的修改加到暂存区域
git add README.md
然后查看修改状态 git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
控制台给出提示,可以使用 git restore --staged <file>...
我们按照提示执行
git restore --staged README.md
用 git status
查看状态
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
发现文件从暂存区被取出来了,文件没有发生任何修改