-2

I have a bunch of commits in a branch. I would like to recommit them today starting from a fresh branch rather than having my old commits. How can I do that, comparing with master? I couldn't find a similar question on SO, they all did reset --hard, and I don't want to lose my changes.

alien
  • 749
  • 2
  • 6
  • 16
  • 1
    I didn't quite understand what you are trying to achieve. Are you talking about a rebase? – mkrieger1 Mar 15 '21 at 20:34
  • Does [this](https://stackoverflow.com/q/7929369/113848) do what you want to do? – legoscia Mar 15 '21 at 20:34
  • I also don't understand the question -- are you looking to merely rename the branch (because you made a typo or similar)? You can rename in any Git UI tool by simply right-clicking the branch. Or from the CLI, `git branch -m `. – Obsidian Age Mar 15 '21 at 20:36
  • I want to take my old commits on branch A and put them into one commit on a new branch B, dated today. – alien Mar 15 '21 at 20:36
  • ^ That sounds like a squash (and possibly rename) / rebase. `git rebase` or `git merge --squash`. – Obsidian Age Mar 15 '21 at 20:38
  • You don't lose any commits with `reset --hard` if you have previously added a branch to hold your current place. See my essay https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard/59675191#59675191 — what you are describing appears to be regret 3 (though if you also want the squash you would also have regret 1). – matt Mar 15 '21 at 20:41
  • You can start a fresh (in terms of history) by starting an orphan branch: `git checkout -b --orphan new-branch; git commit -m "First commit in new history"` – eftshift0 Mar 15 '21 at 21:29

1 Answers1

-1

To answer your question, let's break it into two points.

  1. Adding commits from a branch A to branch B

For this, you can use git cherry-pick

git checkout B
git cherry-pick sha1^..sha2

will add those commits to the current branch.

  1. Change time to current time.

You can do that by the commands mentioned in this related SO question.

Karthik Nayak
  • 731
  • 3
  • 14