-1

I've committed some local changes.

Now I would like to move these commits into a branch for later

(in order to start working on the version before my commits).

I already read all the titles of similar question, and they either assume,

  • that one wants to move commits from one branch into another, or
  • they assume that one wants to move uncommitted changes into a branch.
  • Other questions ask to move between (existing) branches. I want to create a branch.

git status tells me the following:

hostname /dir : git status
On branch master
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

no changes added to commit (use "git add" and/or "git commit -a")
hostname /dir :
  • 2
    Just create a branch where you're currently pointing at with `git branch ` – Romain Valeri Sep 15 '21 at 16:02
  • 3
    How is your request different from "moving commits from one branch into another"? You say that doesn't answer your question, but that seems to be exactly what you describe that you want. – Joachim Sauer Sep 15 '21 at 16:02
  • @Romain Valeri -- As far as I understand this new branch would not contain the local history of commits. –  Sep 15 '21 at 16:07
  • @Joachim Sauer -- git status tells me, that I'm not working inside a branch. –  Sep 15 '21 at 16:07
  • @FrankPuck: how does it tell you that? Do you mean you're on `main`/`master`? Because if so, then you're on a branch: those are branches too. If not, then you're probably in a [detached head](https://stackoverflow.com/questions/10228760/how-do-i-fix-a-git-detached-head) status and simply need to create a branch at that point. – Joachim Sauer Sep 15 '21 at 16:09
  • 2
    @FrankPuck As far as I understand, this new branch most certainly **would** contain the local history of whatever your `HEAD` is pointing at right now, be it a commit or a branch. Did you try it? – Romain Valeri Sep 15 '21 at 16:44
  • Your `git status` in your question says, literally, `on branch master`. Your [followup comment here](https://stackoverflow.com/questions/69196407/move-local-commits-into-a-to-be-created-branch#comment122301048_69196407) claims that `git status` tells you that you're not on (you used the word "inside") a branch. I am now lost. – torek Sep 16 '21 at 01:09

1 Answers1

4

Let's say your repository started like this with three commits on main.

A - B - C [main]

I've committed some local changes.

Presumably to your main branch. Let's call them 1, 2, and 3.

A - B - C - 1 - 2 - 3 [main]

Now I would like to move these commits into a branch for later

You don't move the commits, you move the branches. Unlike other version control systems you may be familiar with, branches are just a label pointing at a commit. main points at commit 3.

First, with main checked out, make a new branch. This will point at the same commit as main, 3. It is equivalent to main.

$ git branch other

A - B - C - 1 - 2 - 3 [main]
                      [other]

And now move main back to commit C. You'll have to use git log to figure out which commit ID is C.

Again, this is just moving a label that points at a commit.

$ git reset --hard C

A - B - C [main]
         \
          1 - 2 - 3 [other]

Note that only my drawing changed to make it easier to draw, the commits are exactly the same. There's nothing special about the connection between C and 1. Nothing has changed but what commit main points at.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 1
    @FrankPuck That's how Git behaves and what a Git repository looks like ***for real***. Commits are connected to each other, branches are labels pointing at a commit. Doesn't matter what your local policy is. – Schwern Sep 15 '21 at 16:18
  • Please distinguish between commits (which can be used in merge) and the state after the commit. I know that I can create a branch at the current state. This is not what I want to do! –  Sep 15 '21 at 16:21
  • 2
    @FrankPuck "*Somewhere you should tell git, which commits are contained in the newly created branch*" This assumption is incorrect. There is only one commit history. Branches simply point at a commit. You tell Git which commit a branch points at, and now that branch has the entire history of that commit. If branch A and branch B both point at commit abc123 the both branches contain the same commits. Always. You can ***copy*** commits, but that doesn't happen then you make branches. – Schwern Sep 15 '21 at 16:23
  • @FrankPuck In the illustrations above, commit 3 knows its parent is commit 2. Commit 2 knows its parent is commit 1. Commit 1 knows its parent is commit C and so on. All `main` knows is it points at commit C and therefore contains C, B and A. All `other` knows is it points at commit 3 and therefore contains 3, 2, 1, C, B, and A. – Schwern Sep 15 '21 at 16:26
  • "There is only one commit history" This is wrong. When I do a merge, only some commits are merged! –  Sep 15 '21 at 16:26
  • @FrankPuck A merge creates a new commit with two parents. – Schwern Sep 15 '21 at 16:27
  • @FrankPuck If branch A points at abc123 and branch B points at xyz789 and you merge them you get a new commit whose parents are *both* abc123 and xyz789. All the old commits still exist. You can see the true history with `git log --graph --decorate`. However, everything goes out the window if you squash merge. That smashes everything together into one new commit. You lose a lot of information that way. I do not recommend it. I recommend instead getting comfortable with how Git works. – Schwern Sep 15 '21 at 16:29
  • I could imagine, that a merge merges all commits until the common point. Is this in fact the case? –  Sep 15 '21 at 16:30
  • @FrankPuck Git merges are fundamentally different. I would suggest reading [Basic Branching and Merging](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging) in [Pro Git](https://git-scm.com/book/en/v2) to get a feel for the fundamentals. – Schwern Sep 15 '21 at 16:34
  • All these documents start with a tree. This is (other than for branches) not the case for me! We have a linear history! –  Sep 15 '21 at 16:36
  • Git is, fundamentally, a tree. Really a directed acyclic graph. I can only guess at what "we have a linear history" means or how your project is handling merges. You're probably doing squash merges which will, yes, result in a linear history. Doesn't change how Git works or how you manage branches before you merge. I would suggest reading Pro Git. – Schwern Sep 15 '21 at 16:44