繁体   English   中英

Git命令将当前文件保存在临时分支中,而无需提交给master

[英]Git commands to save current files in temporary branch without committing to master

假设我有一个本地Git存储库和一些未提交的更改。 因为更改可能非常混乱,所以我现在还不想提交到分支,但是我想在云上对其进行测试。

我正在寻找可以执行以下操作的git命令序列:

  1. 将“混乱的更改”提交到另一个分支,例如mymessydev
  2. git push origin mymessydev
  3. 切换回具有相同未提交更改的master分支,好像什么也没发生。

假设您现在处于master分支中,并且发生了一些混乱的变化,

git stash
git checkout -b messybranch
git stash apply
git add .
git commit -m "commit"
git push origin messybranch
git checkout master // clean master

此时,您不会丢失这些更改,因为它们已经在messybranch上被推送了。 为了将这些更改返回给master ,您可以合并messybranch或在master上挑选提交

git merge messybranch

要么

git cherry-pick #commit

cherry-pickmerge提交您的更改,但是如果您希望暂存而不提交它们,则可以执行

git reset head~1

我写了一个python脚本来自动化这个过程。 它甚至适用于未跟踪的文件!

首先安装python绑定: pip install gitpython

import sys
from git import Repo
import time


def save(temp_branch, repo_path='.'):
    repo = Repo(repo_path)
    git = repo.git
    work_branch = repo.active_branch.name

    ret = git.stash()
    is_stash = not 'No local changes' in ret
    # delete the temp branch if already exist
    try:
        git.branch('-D', temp_branch)
    except:  # the branch doesn't exist, fine.
        pass
    git.checkout('-b', temp_branch)
    if is_stash:
        git.stash('apply')
    git.add('.')
    try:
        git.commit('-m', 'temporary save ' + time.strftime('%m/%d/%Y %H:%M:%S'))
    except:
        print('no temporary changes to push')
    git.push('-f', 'origin', temp_branch)
    git.checkout(work_branch)
    git.cherry_pick('-n', temp_branch)
    print(git.reset('HEAD'))


save(*sys.argv[1:])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM