0%

Git command - useful ones

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 commit
git revert HEAD only revert one
git revert [倒数第一个提交] [倒数第二个提交] revert multiple

1
2
3
# 假如你想撤回倒数第三次提交,你可以这么做:
git rebase -i HEAD~4
# 在出现的编辑窗口中,将倒数第三条 commit 删除,然後保存。就这么简单。

Drop commit
git reset [last good SHA] 让最新提交的指针回到以前某个时点,该时点之后的提交都从历史中消失。
默认情况下,git reset不改变工作区的文件(但会改变暂存区),--hard参数可以让工作区里面的文件也回到以前的状态。

git reset --hard [last good SHA]
执行git reset命令之后,如果想找回那些丢弃掉的提交,可以使用git reflog命令,具体做法参考这里。不过,这种做法有时效性,时间长了可能找不回来。

Amend commit
git commit --amend -m "Fixes bug #42"
产生一个新的提交对象,替换掉上一次提交产生的提交对象。
这时如果暂存区有发生变化的文件,会一起提交到仓库。所以,–amend不仅可以修改提交信息,还可以整个把上一次提交替换掉。

Undo branches changes

1
2
3
4
5
6
7
8
9
# 新建一个 feature 分支,指向当前最新的提交
# 注意,这时依然停留在当前分支
git branch feature

# 切换到这几次提交之前的状态
git reset --hard [当前分支此前的最后一次提交]

# 切换到 feature 分支
git checkout feature

上面的操作等于是撤销当前分支的变化,将这些变化放到一个新建的分支。


Revert local commit
git reset --soft HEAD~1

Remote repo
add remote repo: git remote -> paste the link
git remote origin
git 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
2
3
4
5
6
7
// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

# Note: You can also use the -D flag which is synonymous with --delete --force instead of -d. This will delete the branch regardless of its merge status.

git merge & rebase
git merge --squash feature


Quoted from “OhShitGit, one good article

reset

1
2
3
4
5
6
7
git reflog
# you will see a list of every thing you've
# done in git, across all branches!
# each one has an index HEAD@{index}
# find the one before you broke everything
git reset HEAD@{index}
# magic time machine

amend commits

1
2
3
4
5
6
7
8
# make your change
git add . # or add individual files
git commit --amend --no-edit
# now your last commit contains that change!
# WARNING: never amend public commits

git commit --amend
# follow prompts to change the commit message

remove commit out from wrong branch (from mater to other branch)

1
2
3
4
5
6
# create a new branch from the current state of master
git branch some-new-branch-name
# remove the last commit from the master branch
git reset HEAD~ --hard
git checkout some-new-branch-name
# your commit lives in this branch now :)

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
2
3
4
5
6
7
8
9
# undo the last commit, but leave the changes available
git reset HEAD~ --soft
git stash
# move to the correct branch
git checkout name-of-the-correct-branch
git stash pop
git add . # or add individual files
git commit -m "your message here";
# now your changes are on the correct branch

or we can use Cherry-pick

1
2
3
4
5
6
git checkout name-of-the-correct-branch
# grab the last commit to master
git cherry-pick master
# delete it from master
git checkout master
git reset HEAD~ --hard

What if I need to undo an old commit (there are already many other commits after this one)

1
2
3
4
5
6
7
8
# find the commit you need to undo
git log
# use the arrow keys to scroll up and down in history
# once you've found your commit, save the hash
git revert [saved hash]
# git will create a new commit that undoes that commit
# follow prompts to edit the commit message
# or just save and commit

Something else

1
2
3
4
5
6
7
# get the lastest state of origin
git fetch origin
git checkout master
git reset --hard origin/master
# delete untracked files and directories
git clean -d --force
# repeat checkout/reset/clean for each borked branch