-1

I have a single branch named develop. I have around 20 commits in it. I want to squash all of them in to one commit, and rename it to "Initial commit". So at the end, the branch will have one commit named "Initial commit".

I have tried:

git rebase -i HEAD~20

And I can pick or "f" the commits. That I understand, but what do I do after I save the changes?

All info about squashing assumes, I had another branch initially (master). I only have and will only have a develop branch.

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • Start over. Most recent 20 commits, you wish they were one commit. `git reset --soft HEAD~20`. Now commit. Done. – matt Mar 24 '20 at 12:43
  • Running the command I get: fatal: ambiguous argument 'HEAD~20': unknown revision or path not in the working tree. – Amiga500 Mar 24 '20 at 13:01
  • I did run your instructions. I changed 20 to 19, and it worked. Then I added all. Commit and push. It asked me to merge? I did... I still have the commits. No change – Amiga500 Mar 24 '20 at 13:10
  • As soon as you said "push" you went on to a whole new problem, diverging from and going beyond anything you asked about and anything in my instructions. My instructions are correct for what you actually asked. I've added an answer that demonstrates. – matt Mar 24 '20 at 13:41

1 Answers1

0

Let us take this as the complete problem domain of the question:

I have a single branch named develop. I have around 20 commits in it. I want to squash all of them in to one commit, and rename it to "Initial commit". So at the end, the branch will have one commit named "Initial commit".

You are wishing that the most recent n commits were one commit. I call this the first type of regret. The first type of regret is cured by doing a soft reset and a commit. Here's a demonstration. First, I'll make a bunch of commits on a develop branch:

git init
echo "one" > file
git add .
git commit -m "one"

git branch develop
git checkout develop

echo "two" >> file
git add .
git commit -m "two"
echo "three" >> file
git add .
git commit -m "three"
echo "four" >> file
git add .
git commit -m "four"
echo "five" >> file
git add .
git commit -m "five"

git log --oneline
# 2febafe (HEAD -> develop) five
# 363ca9f four
# 429b266 three
# d46e6df two
# b8bf284 (master) one
cat file
# one
# two
# three
# four
# five

As you can see, I've got commits two, three, four, and five on develop. I wish those were just one commit. Here we go:

git reset --soft b8bf284
git add .
git commit -m "unified"

Okay, let's look around:

git log --oneline
# 408bdd1 (HEAD -> develop) unified
# b8bf284 (master) one
cat file
# one
# two
# three
# four
# five

Yup, there is now just one commit on develop after the point where it diverged from master, and everything I did (all five lines of the file) is in it.

matt
  • 515,959
  • 87
  • 875
  • 1,141