2011-03-10 79 views
6

我需要基於我的另一個項目的一小部分來開始一個新項目,這個項目的回購在github上。如何告訴'git''忘記'之前的所有提交?

我做了一個git克隆到一個新的項目文件夾......很好。

我刪除了一切,我不需要,擺脫了舊的遷移等....罰款。

現在它在本地運行良好,但'git log'顯示所有舊的提交。

我想告訴混帳「忘記一切之前提交,這是一個新的項目,所以忘記之前的一切,現在,從現在就開始爲第一個提交」

我讀到混帳底墊中,但目前還不清楚如果這是正確的命令,如果是的話,如何使用它來實現這個非常簡單的目的。

回答

12

刪除.git文件夾,運行git initgit add

+2

+1,但是如果您有任何特定於回放的回購協議,則必須將其添加回去。 – vcsjones 2011-03-10 21:53:39

+0

@vcsjones好點 – orip 2011-03-10 21:59:14

+0

完美,簡單,謝謝! – jpwynn 2011-03-10 22:04:10

0

你試過reposurgeon

http://www.catb.org/~esr/reposurgeon/

+1

之前做一個'git clean -dfx .'因此...可以讓你做'git rebase'和'git filter-branch'已經可以做的事情了嗎? (而且,這對OP來說太過分了。)有趣的想法。 – Cascabel 2011-03-11 05:28:28

9

這裏接受的答案真的讓我害怕 - 刪除您.git目錄是做劇烈的事情,在這裏沒有必要。

此腳本使用更安全的方法 - 它創建了一個名爲old-master來記錄你在哪裏開始壓扁之前提交分支,然後使用git reset --soft $ROOT_COMMITgit commit --amend壁球整支下降到一個承諾。您也可以從這個GitHub gist下載腳本。

#!/bin/bash 

# A script that squashes your entire current branch down to a single commit, 
# if this repository has a single root commit. This will change the object 
# name of the root commit. 

if [ -n "$(git status --porcelain)" ] 
then 
    echo "git status wasn't clean - refusing to run..." 
    exit 1 
fi 

# From: http://stackoverflow.com/questions/1006775/ 
root_commits() { 
    git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$" 
} 

NUMBER_OF_ROOT_COMMITS=$(root_commits|wc -l) 

if (("$NUMBER_OF_ROOT_COMMITS" > 1)) 
then 
    echo "This script won't work when you have multiple root commits" 
    exit 1 
fi 

ROOT_COMMIT=$(root_commits) 

if [ -z "$ROOT_COMMIT" ] 
then 
    echo "No root commit was found!" 
    exit 1 
fi 

set -e 
set -x 

# Create a branch based on the current HEAD for safety: 
git branch old-master 

# Reset the branch to the root commit, leaving the previous 
# state of the tree staged: 
git reset --soft $ROOT_COMMIT 

# Now amend the root commit with the state of the index: 
git commit --amend -m "The branch restarted" 
0

爲什麼不把這個目錄複製到一個全新的git存儲庫並在那裏提交?

相關問題