6

I need to start a new project based on one tiny part of an another project of mine whose repo is at github.

I did a git clone into a new project folder... fine.

I deleted everything I didnt need, got rid of the old migrations etc.... fine.

now it runs fine locally, BUT 'git log' shows ALL the old commits.

I want to tell git "forget ALL the prior commits, this is a new project, so forget everything before now, start from right NOW as the first commit"

I read about git rebase but it's not clear if that's the right command, and if so, how to use it for this very simple purpose.

jpw
  • 18,697
  • 25
  • 111
  • 187

4 Answers4

13

Delete the .git folder, run git init and git add

orip
  • 73,323
  • 21
  • 116
  • 148
  • 2
    +1, but if you have any repo specific things like remotes, you'll have to add those back. – vcsjones Mar 10 '11 at 21:53
  • 1
    @vcsjones: Or you could copy out `.git/config` before deleting the rest. – Cascabel Mar 10 '11 at 23:56
  • 1
    There is the risk of adding files that ought to be ignored. Ignore lists are in 4 (or more?) different locations, including under .git/. So I'd do a `git clean -dfx .` before the `rm -rf .git` action – sehe Mar 13 '11 at 23:50
  • 1
    worth noting that used incorrectly git clean -dfx can do very bad things if called while the wrong directory, always use the dry run option first git clean -fdxn – jpw Apr 13 '22 at 07:45
9

The accepted answer here really scares me - deleting your .git directory is a drastic thing to do, and it's not necessary here.

This script uses a safer approach - it creates a branch called old-master to record where you were before starting to squash the commits, then uses git reset --soft $ROOT_COMMIT and git commit --amend to squash the whole branch down to one commit. You can also download the script from this 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"
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
0

Why not just copy that directory to a brand-new git repository and commit it there?

db48x
  • 3,108
  • 24
  • 16
0

Have you tried reposurgeon?

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

Ben
  • 34,935
  • 6
  • 74
  • 113
  • 1
    So... that lets you do what `git rebase` and `git filter-branch` can already do? (And besides, it's overkill for what the OP wants.) Interesting idea, though. – Cascabel Mar 11 '11 at 05:28