git命令

156 阅读2分钟

删除本地分支 需切换到其他分支上进行操作 git branch -d [branch-name]

删除远程分支 git push origin --delete [branch-name]

修改上一次的commit信息 git commit --amend -m [message]

重做上一次的commit,并包括指定文件的变化 git commit --amend [file1] [file2]...

强行推送当前分支到远程仓库 git push [remote] --force

使当前工作区内容恢复到当前暂存区内容 git checkout [file] git checkout -- .

重置暂存区的指定文件,与上一次的commit保持一致,但工作区不变 git reset [file]

删除远程tag git push origin :refs/tag/tagName

情况一:文件被修改了,但未执行git add操作(working tree内撤销) git checkout fileName git checkout .

情况二:同时对多个文件执行了git add操作,但本次只想提交其中一部分文件 git add * git status 取消暂存 git reset HEAD [filename]

情况三:文件执行了git add操作,但想撤销对其的修改(index内回滚) 取消暂存 git reset HEAD fileName 撤销修改 git checkout fileName

情况四:修改的文件已被git commit,但想再次修改不再产生新的Commit 修改最后一次提交 git add sample.txt git commit --amend -m"说明"

情况五:已在本地进行了多次git commit操作,现在想撤销到其中某次Commit git reset [--hard|soft|mixed|merge|keep] [commit|HEAD]

如果你每次更新线上,都会打tag,那恭喜你,你可以很快的处理上述场景二的情况

git checkout <tag> 如果你回到当前HEAD指向

git checkout <branch_name> 情况一:撤销指定文件到指定版本

查看指定文件的历史版本 git log <filename> 回滚到指定commitID git checkout <commitID> <filename> 情况二:删除最后一次远程提交

方式一:使用revert

git revert HEAD git push origin master 方式二:使用reset

git reset --hard HEAD^ git push origin master -f 二者区别: revert是放弃指定提交的修改,但是会生成一次新的提交,需要填写提交注释,以前的历史记录都在; reset是指将HEAD指针指到指定提交,历史记录中不会出现放弃的提交记录。 情况三:回滚某次提交 找到要回滚的commitID git log git revert commitID 删除某次提交 git log --oneline -n5 git rebase -i "commit id"^

以上作为自己的笔记,待更新...