1. git add 添加多余文件想撤销
我们知道,git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件
git add 如果添加了错误的文件的话
撤销操作
-
git status 先看一下add 中的文件
-
git reset HEAD 如果后面什么都不跟的话 就是上一次add 里面的全部撤销了 ,HEAD 可以不写,直接git reset
-
git reset HEAD XXX/XXX/XXX.java 就是对某个文件进行撤销了,HEAD 可以不写,直接git reset XX文件
2. git commit 提交(未git push)后想回退
如果不小心 弄错了 git add后 , 又 git commit 了。
先使用git log 查看节点
commit xxxxxxxxxxxxxxxxxxxxxxxxxx
Author:
Date:
回退操作
- 回退到未提交前
git reset --soft 90f1ce4d73c5dc63f46fa61984a6bb878f47374^
3.git commit后,可以使用 git revert(反做)
还原已经提交的修改
此次操作之前和之后的commit和history都会保留,并且把这次撤销作为一次最新的提交
-
git revert HEAD 撤销前一次 commit
-
git revert HEAD^ 撤销前前一次 commit
-
git revert commit-id (撤销指定的版本,撤销也会作为一次提交进行保存)
git revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去,版本会递增,不影响之前提交的内容。
4.使用git push(将代码推送至远程仓库)报错:Please make sure you have the correct access rightsand the repository exists
报错原因:密钥配置
解决方案:重新配置密钥(搜索下列链接中的“设置SSH key”部分)
ctrl+f 设置SSH key
5.使用git命令git remote add origin url.git报错:fatal: remote origin already exists.
报错方案:上传到git的远程源已经存在
解决方案:删除远程源
Git Bash.exe中键入代码git remote rm origin,然后接着提交:git remote add orgin url.git
即可
6.使用git push报错
[root@linux1 php]# git push -u origin master
To git@github.com:kangvcar/Results-Systems--PHP.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:kangvcar/Results-Systems--PHP.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
报错原因:GitHub远程仓库中的README.md文件不在本地仓库中。
解决方案:Git Bash.exe键入代码git pull --rebase origin master 再接着git push即可。
注:在github新建代码仓库的时候不要勾选创建README.md
7.工作中经常遇到git commit后(尚未git push操作),需要回退的情况具体方法如下:
1.执行git log找到本次commi的ID信息
比如commit id信息为:90f1ce4d73c5dc63f46fa61984a6bb878f47374
2.执行git reset --soft HEAD^操作
对应HEAD即上述commit id信息
git reset --soft 90f1ce4d73c5dc63f46fa61984a6bb878f47374^
说明:最后的符号^记得不要漏掉
此时通过git status时,可以看到git add 的文件(绿色)
3.git reset modify_file
通过git reset modify_file 操作后,
通过git status可以看到红色的修改文件
4.git checkout modify_file
通过git checkout modify_file还原至修改之前状态
8.Git报错解决:fatal: unable to access ‘https://github.com/…‘: OpenSSL SSL_read: Connection was reset
昨天还可以git push代码到