修改最近一次提交的信息
git commit --amend
git push <remote> <branch> -f
参考:https://blog.youkuaiyun.com/sodaslay/article/details/72948722
如果修改的是用户名和邮箱用:git commit --amend --author="userName <userEmail>",不加--author参数直接在打开的文件里修改是无效的。
push -f时遇到错误:you are not allowed to push code to protected branches on this project,则去关闭保护,完了可以再开启保护:https://stackoverflow.com/questions/32246503/fix-gitlab-error-you-are-not-allowed-to-push-code-to-protected-branches-on-thi
撤销git commit 但是未git push的情况:
1. 找到上次git commit的 id
git log
找到你想撤销的commit_id
2. git reset --hard commit_id
完成撤销,同时将代码恢复到前一commit_id 对应的版本。
3. git reset commit_id
完成Commit命令的撤销,但是不对代码修改进行撤销,可以直接通过git commit 重新提交对本地代码的修改。
git push -f危害:
https://gitbook.tw/chapters/github/using-force-push.html
rebase merge区别:
http://gitbook.liuhui998.com/4_2.html
https://www.cnblogs.com/xueweihan/p/5743327.html
https://www.jianshu.com/p/f23f72251abc
通常需要stash情况是,线上更新了代码,pull新代码时,就要把不想提交的修改stash起来。
git stash
git stash list
git stash pop stash@{0}
stash恢复之后都变成了add后状态
所以看个人情况,再git reset HEAD .取消add后状态
取消已经暂存的文件
git reset HEAD file
取消对文件的修改
git checkout -- file
git checkout . ##撤销所有修改,注意在根目录下。
git clean -f -d ##撤销所有新增文件
强制更新本地分支:
git fetch --all
git reset --hard origin/master
----------------------------------------------------------------------------
获取远程项目中最新代码时:git pull --rebase,这个时隐性的合并远程分支的代码不会产生而外的commit(但是如果存在冲突的commit太多就像上面说的,需要处理很多遍冲突)。
合并到分支的时候:git merge --no-ff,自动一个merge commit,便于管理(这看管理人员怎么认为了)
git merge 在不是线性提交的情况下,合并提交或分支就会产生新的commit记录。
git pull和git pull --rebase区别:git pull做了两个操作分别是‘获取’和合并。所以加了rebase就是以rebase的方式进行合并分支,默认为merge。
克隆版本库的时候,所使用的远程主机自动被Git命名为origin
在本地主机上要用"远程主机名/分支名"的形式读取
git rebase 是一个危险命令,因为它改变了历史,可能丢失提交纪录,什么场景下?http://jartto.wang/2018/12/11/git-rebase/
默认情况下,git push 并不会把标签传送到远端服务器上,只有通过显式命令才能分享标签到远端仓库。其命令格式如同推送分支,运行 git push origin [tagname]
推送所有本地新增的标签上去,使用 --tags 选项
git push origin --tags
----------------------------------------------------------------------------
如果自己分支要合并到develop分支,然后提交pr方便review的话。可以不使用git merge合并,使用git rebase合并,git rebase其他分支后,git push会出错。因为这是一个 更改历史 的操作,所以不要指望你改了历史然后就顺利把你篡改够的历史提交上去。
https://blog.youkuaiyun.com/ManyPeng/article/details/81095744
git rebase基础认识:
http://gitbook.liuhui998.com/4_2.html
git rebase撤销:
https://juejin.im/post/5a65ac67f265da3e330473f7
git rebase和merge区别
https://blog.youkuaiyun.com/liuxiaoheng1992/article/details/79108233
git-reflog是用来恢复本地错误操作
git checkout master
git cherry-pick 62ecb3 #合并单个提交到master,这里的commit_id可以任意分支上的。
------------------------------------------------------------------------
当输入用户名密码错误多时可能缓存到了无效的密码,所以git pull等操作无效。
https://www.jianshu.com/p/5eb3a91458de
缓存密码:
git config --global credential.helper wincred
https://help.github.com/en/articles/caching-your-github-password-in-git
删除某用户缓存:
https://www.jianshu.com/p/b49f6dfbf721
https://www.cnblogs.com/jimboi/p/9019942.html
关闭缓存:git credential-cache exit
开启缓存:git config credential.helper cache
直接把账户密码写到本地,不用缓存过期后又输入:
git config credential.helper 'store --file=~/.gitpassword'
删除某个提交:
1.git log获取commit信息
2.git rebase -i (commit-id)
commit-id 为要删除的commit的下一个commit号
3.编辑文件,将要删除的commit之前的单词改为drop
4.保存文件退出大功告成
5.git log查看