if I create a new branch all the commits from the copied branch are coppied to the new one.
How can I stop this? I would like to have just new commits in the new branch..
if I create a new branch all the commits from the copied branch are coppied to the new one.
How can I stop this? I would like to have just new commits in the new branch..
Commits in git contain a reference on their parent commit. This is how git builds the history of your repository.
A branch on the other hand just points to a specific commit. It is literally a file which contains the SHA-1 key of the current commit. You can find them in the .git/refs/heads
folder in your repository.
When you create a new branch without specifying a commit the branch will point to the same commit HEAD
currently points at. (Note that HEAD
doesn't directly point at a commit but at a branch, but that's a different story.)
Let's assume you are currently on your development
branch and want to create a new branch to work on a new feature.
git checkout -b new-branch
Now both development
and new-branch
will point at the same commit, lets say the key is DEF456
. This commit contains a reference to it's parent commit - as every commit does - which is ABC123
which in turn again contains a reference on it's parent commit and so on.
You see, git doesn't copy the history of a branch when creating a new branch, it just points to the same commit as the current branch does.
If you want to learn more about how git stores it's information internally you can take a look at the Git Objects chapter of the gitpro book.
And as you already found in this question. You can force a merge commit, rather than a fast-forward, by using git merge --no-ff <branch-to-merge>
.
A merge commit is special in the sense that it doesn't contain one refence on a parent commit, as most commits in git do, but it contains multiple references to it's parent commits. This defines a merge.
It sounds like you were really asking about forcing "real" merges rather than "fast forward" merges, and about "squash" merges (which are not actual merges at all). For completeness, though, I want to mention that there's one more thing you can do when creating a new branch-name:
git checkout --orphan -b newbr
In this case, git puts you on newbr
without actually creating newbr
yet. This means that the next commit will start a new, independent commit-graph within your repository. In other words, you will have multiple "root commits" in a single repository (a root commit is a commit that has no parents, vs ordinary commits that have one parent and merge commits that have two or more parents).
Multiple independent commit graphs within a repository are unusual, but supported: git's own source repository contains them, for instance. (The git program is maintained using git.)