Git Tips

Git tips #

About Reset #

The “Oh crap I didn’t mean to commit yet” Trick

如何恢复当前工作区到最初始状态

git reset HEAD

Undo last commit and bring changes back into staging (i.e. reset to the commit one before HEAD)

git reset --soft HEAD^

“That commit sucked! Start over!” (危险,慎用)

Undo last commit and destroy those awful changes you made (i.e. reset to the commit one before HEAD)

git reset --hard HEAD^

Git configuration #

Origin not allow push (设置Origin不允许Push)

git remote set-url --push origin no-pushing

Golang use private repo (Golang 使用私有 Repo需要做如下两个步骤)

git config --global url.git@github.com:.insteadOf https://github.com/

cat ~/.gitconfig
[url "git@github.com:"]
    insteadOf = https://github.com/

export GOPRIVATE=github.com/rmrf/BPlan

# Then you can go mod tidy

Git display log more readable

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --"
# then 
git lg

Branch #

How to use old commit as new Branch (如何使用 old commit 作为新的 branch)

git checkout -b justin b9c196a09500837ec03b
#This will create the new branch and check it out.

git branch justin b9c196a09500837ec03b
#This creates the branch without checking it out.

# It is worth noting that if you had checked out a commit using git checkout <SHA1> 
# (and therefore you're on a detached HEAD), you can create a branch at that commit 
# by just using 

git branch "<branchname>" or git checkout -b "<branchname>"

Delete remote branch (删除远端 branch)

git push origin --delete "<branch name>"

Delete local branch (删除本地branch)

git branch -d "<branch name>"

Git setup default branch master options

gig branch.master.remote origin
git config branch.master.merge refs/heads/master

# or can modify : (.git/config):
[branch "master"]
remote = origin
merge = refs/heads/master

Fetch and Rebase #

git fetch origin master
git rebase origin/master
git push

Cherry-pick #

When origin/prom banch rebased, commit changed, then your local commit can not be pushed. Solution: cherry pick this commit to remote origin/prom banch

当远程 的 prom 分支经过了 rebase 之后,所有的commit都会变化, 这个时候本地的新commit 是无法 push上去的, 解决办法是: 将这个 commit cherry-pick 到远程的 origin/prom 分支上

➜ git rev-parse HEAD
8b7d5d9da64b817cacbff059387ddf8593697ce6

➜ git remote update
Fetching origin

➜ git reset --hard origin/prom
HEAD is now at 1932d45 remove blank line

➜ git cherry-pick 8b7d5
[prom 3d6e19f] fix labels
 Date: Tue Mar 1 13:59:03 2022 +0800
 2 files changed, 20 insertions(+), 4 deletions(-)

Submodule #

Git Submodule:

git submodule add git@github.com:example/user-center-proto.git proto/user-center-proto
git pull && git submodule init && git submodule update && git submodule status
git checkout <commit id>

Submodule update to remote latest(submodule 更新到远程最新的)

git submodule update --init --remote --recursive

Stage Operation #

Use -amend to add latest change to last commit

使用 -amend 将最新的修改添加到上次commit中

git commit -a -amend

Unstage a wrongly git add file

将错误 git add 到 stage 的文件 删除

git reset abc.txt

Remove the folder which forget to put inside .gitignore

删除没有提前放进 .gitignore 中的目录

git rm --cached -r .idea/

# or a file
git rm --cached mylogfile.log