簡體   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