# branch

# 重命名分支

  • Rename your local foo branch with bar:

    git branch -m foo bar

  • Remember this will add the new branch when you push, but it won’t delete the old foo remote branch.

    Add -f --mirror to rename the branch on the remote:

    git push origin -f --mirror

# 删除分支

# 删除本地分支

  • git branch -d <branch>

# 强制删除本地分支

  • git branch -D <branch>

# 删除远程分支

  • git push origin :<branch>

# 关联远程分支

  • git branch -u origin/<branch> <branch>

  • git branch --set-upstream-to origin/<branch> <branch>

# 取消关联远程分支

  • git branch --unset-upstream

# 创建分支

  • git branch -b dev origin/dev

# stash

# 暂存工作区(暂存进栈)

  • git stash

    缓存包括未被git追踪的文件,如新文件git stash -u

# 查看暂存区域

  • 查看暂存列表git stash list

  • 查看具体缓存文件git stash show statsh@{0}

# 暂存出栈

  • git stash pop

    如遇到冲突,会出现类似下面的提示

    Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt The stash entry is kept in case you need it again.

    这种情况,去解决冲突就行了,同时,git keep住了这次的stash,以防你下次还有需要再用到

# 删除暂存

  • 删除具体某次暂存git stash drop <stash>

    例如:git statsh drop stash@{0}

  • 清空所有暂存 git stash clear

# Reset

例子:

git reset --soft HEAD^

这样就成功的撤销了你的commit

注意,仅仅是撤回commit操作,您写的代码仍然保留。

HEAD^的意思是上一个版本,也可以写成HEAD~1

如果你进行了2次commit,想都撤回,可以使用HEAD~2

参数解析:

# --mixed

意思是:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作

这个为默认参数,git reset --mixed HEAD^ 和 git reset HEAD^ 效果是一样的。

# --soft

不删除工作空间改动代码,撤销commit,不撤销git add .

# --hard

删除工作空间改动代码,撤销commit,撤销git add .

注意完成这个操作后,就恢复到了上一次的commit状态。

git commit --amend

此时会进入默认vim编辑器,修改注释完毕后保存就好了。

# config

# 设置大小写敏感

git在windows下面忽略大小写,在linux大小写敏感,导致在windows把大写名字文件夹重命名成小写后,git无法识别到修改,无法提交到远程,导致linux下面路径报错

git config core.ignorecase false

# pull

拉取不同仓库代码

https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error

git pull origin master --allow-unrelated-histories
1

# cherry-pick

git cherry-pick <commit hash>
1

参数

  • -e--edit

打开外部编辑器,编辑提交信息

  • -n--no-commit

更新工作区和暂存区,不产生新的提交。

  • -x

在提交信息的末尾追加一行(cherry picked from commit ...),方便以后查到这个提交是如何产生的。

  • -s--signoff

在提交信息的末尾追加一行操作者的签名,表示是谁进行了这个操作。

# rebase

# 取消rebase

通过reflog找到变基前的commit, reset回去

# commit 合并

在进行rebase前,防止出现多个冲突的情况,可以合并当前分支的commit, 那样的话进行rebase的时候就只需要解决一次冲突了

合并前面三个commit

git rebase -i HEAD~3
git rebase -i 24793cc5
1
2

rebase前:

rebase后:

rebase完成后再做rebase, 就只需要解决一次冲突就行了

解决完冲突后执行:

git add <<已解决冲突的文件>>
git rebase --continue
1
2

再看看rebase后的git graph

大功告成!

# merge

git merge 默认使用fast forward(ff)模式, 不推荐,禁用ff模式:

git merge --no-ff -m "合并feat-v1.1.0" feat-v1.1.0
1
上次更新: 2021-09-23 10:33:39