Skip to content

常用命令

创建新分支(清除all commit)

https://git-scm.com/docs/git-checkout

git checkout --orphan <new-branch>

git 修改branch名字

git branch -m <new name>

or

git branch -m <oldname> <newname>

git 挑选commit

如:在 branch dev 下,合并 prod 的commit

git cherry-pick commit1 commit2

# commits one by one
# git cherry-pick commit1 commit2 commit3 commit4 commit5
# example:
git cherry-pick 1e038f10 2f028f10 34138b11
# commits by range
# git cherry-pick A^..B
# example:
git cherry-pick 6653c90^..481981f

输出commit

tac 行反转,ORS 间隔

git log -10 --pretty="%h" | tac | awk "{print}" ORS=' '

打包

git archive --format=zip --output mpy.zip master

stash

git stash

#保存时添加说明
git stash push -m "修复登录 Bug 的未完成代码"

#查看当前所有 stash
git stash list

#取回 stash
git stash apply stash@{0}

#取回并删除 stash
git stash pop stash@{0}

#stash 某些文件
git stash push -m "stash 某些文件" path/to/file1 path/to/file2

#丢弃某个 stash
git stash drop stash@{0}

#清空所有 stash
git stash clear

#从 stash 创建分支
git stash branch my_new_branch stash@{1}

本地文件忽略跟踪

src/vendor为例子,可以尝试删除进行测试

# 对所有文件设置 skip-worktree
git ls-files src/vendor | xargs git update-index --skip-worktree

# 取消 skip-worktree(恢复正常跟踪)
git ls-files src/vendor | xargs git update-index --no-skip-worktree

# 针对 单个文件
git update-index --no-skip-worktree src/vendor/xxx.js

# 验证当前哪些文件被 skip 了
git ls-files -v src/vendor
S src/vendor/a.js
S src/vendor/b.css
# 以 S 开头的文件 = skip-worktree

# 取消后,如果本地文件被你删过,会受到提示
deleted: src/vendor/xxx
# 恢复进行恢复
git checkout -- src/vendor