Keep

Git 常用命令汇总

12/20/2017, 12:04:00 PM 4 min read

// clone.
// https://segmentfault.com/q/1010000000409170/a-1020000014364659
git clone --depth=1
// https://fehey.com/2-tips-for-git-clone
git clone -b develop --single-branch

// 工作区 diff 暂存区
git diff <filename>
// filename branchname 冲突时
git diff -- <filename>

// 工作区 diff 仓库指定版本
git diff <branch> <filename>

// 暂存区 diff 仓库
git diff -–cached <filename>
git diff --cached <branch> <filename>

// 仓库版本之间 diff
git diff <ver> <ver>
git diff <ver>:<filename> <ver>:<filename>

// 版本快键方式
HEAD
HEAD^
HEAD^^
HEAD~{n}

// 切换分支
git checkout <branch>

// 切换到上一次使用的分支
git checkout -

// 创建并切换到分支
git checkout -b <branch>

// 还原, 使用暂存区
// ps: 丢弃本地修改
git checkout -- <file>

// 回退 Stage(index) 区到 HEAD 版本
// 代码依然在 Working Directory
// = git reset --mixed 只影响 Stage(index)
git reset

// 指定文件,从 Stage 回退到 Working Directory
// ps: 撤回 add <file>
git reset -- <file>

// History 中直接回退版本
// Stage(index)/Working Directory 文件不变
git reset --soft <branch>

// 同时影响 Working Directory Stage 修改
// ps: git log 不再能看到<branch>之后的日志
// ps: git reflog 查看命令日志 - 找到版本号,然后切换到指定版本
git reset --hard <branch>

// 把文件从暂存区删除
git rm --cache <file>
// 删除 git 追踪状态
git rm -r --cached <file>
// 更新 .gitignore 文件
git add .gitignore
// https://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository

// 查看当前分支详情
git remote -v
// 添加远程地址
git remote add origin ...
// 删除远程不存在的分支
git remote prune origin
// 删除远程分支
git push origin --delete {branchname}

// 拉取远程分支
git checkout origin/dev --track
// 相当于
git checkout -b dev origin/dev

// 查看 git 日志
git log -p -2
git log --pretty=oneline
// 或者直接
git log --oneline
// 查看文件更新
git show {commitid} -p <file>

// tag.
git tag {tagname} {commitid}
git tag -a {tagname} {commitid} -m "xxxx"

// tag push 到远程
git push origin {tagname}
// 一次性推送所有 tags
git push origin --tags

// 删除指定 tag
git tag -d {tagname}
// 删除远程 tag
git push origin :refs/tags/{tagname}
git push origin --delete tag {tagname}

// 合并.
// --no-ff 保留原分支记录
git merge --no-ff develop

// 合并 commit 使用
// https://stackoverflow.com/questions/2563632/how-can-i-merge-two-commits-into-one
git rebase --abort
git rebase --interactive HEAD~2
git rebase -i HEAD~2
// 调整 pick s
// 保存退出, 然后 git 会提示重新编辑 commit
git push origin master --force

// 编辑 commit 记录
git rebase -i --root

// 一次空提交
git commit --allow-empty -m "[empty] initial commit"
// 修改 commit
git commit--amend
// commit 之后, 继续向 commit 提交代码变更
git commit --amend --no-edit

// 暂存 commit.
// 可以暂存多次. pop 多次就可以
git stash
// 切到上一次 stash.
git stash pop
// 列出当前栈列表
git stash list
// 应用任意 stash, 默认应用顶上 stash
git stash apply stash@{index}

// 把其它分支修改应用指定分支上
git cherry-pick <commit>

// 更新 branch 名称
git branch -m current-branch-name new-branch-name

// 在项目外新建一个目录, 方便临时 review 或者调试
git worktree add path branch
// 删除
git worktree remove path
// 删除目录后, 修剪 worktree
git worktree prune
// https://einverne.github.io/post/2019/03/git-worktree.html

// 导出项目
git archive --format=zip --output ../[name].zip branch

// Set Git configuration conditionally
// https://phuoc.ng/collection/tips/set-git-configuration-conditionally/
// example:
[user]
    name=...
    email=...
[includeIf "gitdir:/personal-projects/"]
    path=~/.personal.gitconfig
[includeIf "gitdir:/works-projects/"]
    path=~/.works.gitconfig

Other.

Tag:
Web

@read2025, 生活在北京(北漂),程序员,宅,喜欢动漫。"年轻骑士骑马出城,不曾见过绝望堡下森森骸骨,就以为自己可以快意屠龙拯救公主。"