To be honest, I became little gitphobia for now, because sometimes I tends to screw up things. Luckily we work from home, so I can turn to someone and call for help, like revert my stupid mistakes. But of course I need to rememeber or at least write those things down somewhere. And now the project is kind finished, finally I can have sometime to write those useful commands down.
Articles/Books lists
How to undo Git (Chinese) <– From Ruan Yifeng’s blog
猴子都能懂的GIT入门 (Git 101 for even monkey, Chinese) <– Really good, all in Chinese.
One article about rebase (Chinese) <– Seems we should prefer to use rebase to keep a clean line.
Another article from Liao Xuefeng, also about rebase(Chinese) <– Should be more careful when we use rebase, though it is good and creates clean baseline and combine commits.
Git Community Book (Chinese) <– Official Documentation, in Chinese
為你自己學 Git <– Also a good one, clearly enough, in traditional Chinese
OhShitGit, one good article (English)
Revert commitgit revert HEAD
only revert onegit revert [倒数第一个提交] [倒数第二个提交]
revert multiple
1 | # 假如你想撤回倒数第三次提交,你可以这么做: |
Drop commitgit reset [last good SHA]
让最新提交的指针回到以前某个时点,该时点之后的提交都从历史中消失。
默认情况下,git reset
不改变工作区的文件(但会改变暂存区),--hard
参数可以让工作区里面的文件也回到以前的状态。
git reset --hard [last good SHA]
执行git reset
命令之后,如果想找回那些丢弃掉的提交,可以使用git reflog
命令,具体做法参考这里。不过,这种做法有时效性,时间长了可能找不回来。
Amend commitgit commit --amend -m "Fixes bug #42"
产生一个新的提交对象,替换掉上一次提交产生的提交对象。
这时如果暂存区有发生变化的文件,会一起提交到仓库。所以,–amend不仅可以修改提交信息,还可以整个把上一次提交替换掉。
Undo branches changes
1 | # 新建一个 feature 分支,指向当前最新的提交 |
上面的操作等于是撤销当前分支的变化,将这些变化放到一个新建的分支。
Revert local commitgit reset --soft HEAD~1
Remote repo
add remote repo: git remote -> paste the link
git remote origingit push -u origin master
(将本地repo推送至remote repo)git push -u origin <branch>
Delete repo
If you really want to remove all of the repository, leaving only the working directory then it should be as simple as this.rm -rf .git
Deleting a branch REMOTELY
Here’s the command to delete a branch remotely: git push <remote> --delete <branch>
.
For example: git branch origin --delete fix/authentication
The branch is now deleted remotely.
You can also use this shorter command to delete a branch remotely: git push <remote> :<branch>
1 | // delete branch locally |
git merge & rebasegit merge --squash feature
Quoted from “OhShitGit, one good article“
reset
1 | git reflog |
amend commits
1 | # make your change |
remove commit out from wrong branch (from mater to other branch)
1 | # create a new branch from the current state of master |
This doesn’t work if you’ve already pushed the commit to a public/shared branch, and if you tried other things first, you might need to git reset HEAD@{number-of-commits-back}
instead of HEAD~. Infinite sadness.
What if I commited to the wrong branch
1 | # undo the last commit, but leave the changes available |
or we can use Cherry-pick
1 | git checkout name-of-the-correct-branch |
What if I need to undo an old commit (there are already many other commits after this one)
1 | # find the commit you need to undo |
Something else
1 | # get the lastest state of origin |