ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.9 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://hpurmann.com/2015/03/28/git-moving-commits-to-another-branch/ |
| Last Crawled | 2026-03-11 02:50:19 (27 days ago) |
| First Indexed | 2018-06-09 10:09:56 (7 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Moving commits to another branch |
| Meta Description | Let’s say you made a few commits and then realized that you did them to the wrong branch. Because git is awesome, it’s really easy to change the branch you committed to. |
| Meta Canonical | null |
| Boilerpipe Text | 28 Mar 2015
Let’s say you made a few commits and then realized that you did them to the wrong branch. Because git is awesome, it’s really easy to change the branch you committed to.
Let’s learn why!
Commits
Every commit stores the SHA-1 hash of its parent commit(s). Think of it as a directed acyclic graph with each node pointing to its parent(s).
Branches
A branch is just a pointer to a commit. Git only stores a single file with the filename being the name of the branch. Inside, there is only the SHA-1 of the top-most commit.
Or to quote the great book
Git Internals
:
Creating a branch is nothing more than just writing 40 characters to a file.
Go for it and have a look in the
.git
directory of some git repository.
$
cd
.git
$
ls
COMMIT_EDITMSG config hooks info objects
HEAD description index logs refs
$
cd
refs/heads
$
ls
master
The folder
.git/refs/heads
stores the branch reference files.
$
cat
master
eb5c3831d6ebca824857d30cea70948201529ada
Let’s say you are on the master branch and create another branch named
dev
.
$
git branch dev
$
cat
dev
eb5c3831d6ebca824857d30cea70948201529ada
The new branch
dev
is pointing to the same commit as master. If we draw that, it would look like this:
Back to the problem …
With the knowledge of commits and branches you can probably come up with a solution yourself.
At this point you noticed that you want commits
D
and
E
on a new branch called
feature1
.
$
git branch feature1
To “remove” the commits from the master branch, you simply move the branch pointer two commits back. Please note the two carets (
^
) behind
HEAD
.
$
git reset
--hard
HEAD^^
Because commits are just referencing their parents,
D
and
E
are now unreachable from
master
. Now you can just switch to your new branch and keep working on it.
$
git checkout feature1
I hope you agree with me that this is really easy once you understood what these commands do internally. For further reading, I highly recommend reading the
Think-like-a-git
website and the mentioned
Git Internals
book by Peepcode.
Written by Hendrik Purmann
Berlin-based software engineer. Currently interested in cross functional teams, Microservices, GoLang, Kubernetes and the DevOps movement. |
| Markdown | # [hpurmann](https://hpurmann.com/)
# Moving commits to another branch
28 Mar 2015
Let’s say you made a few commits and then realized that you did them to the wrong branch. Because git is awesome, it’s really easy to change the branch you committed to.
Let’s learn why\!
## Commits
Every commit stores the SHA-1 hash of its parent commit(s). Think of it as a directed acyclic graph with each node pointing to its parent(s).

## Branches
A branch is just a pointer to a commit. Git only stores a single file with the filename being the name of the branch. Inside, there is only the SHA-1 of the top-most commit.

Or to quote the great book [Git Internals](https://github.com/pluralsight/git-internals-pdf):
> Creating a branch is nothing more than just writing 40 characters to a file.
Go for it and have a look in the `.git` directory of some git repository.
```
$ cd .git
$ ls
COMMIT_EDITMSG config hooks info objects
HEAD description index logs refs
$ cd refs/heads
$ ls
master
```
The folder `.git/refs/heads` stores the branch reference files.
```
$ cat master
eb5c3831d6ebca824857d30cea70948201529ada
```
Let’s say you are on the master branch and create another branch named `dev`.
```
$ git branch dev
$ cat dev
eb5c3831d6ebca824857d30cea70948201529ada
```
The new branch `dev` is pointing to the same commit as master. If we draw that, it would look like this:

## Back to the problem …
With the knowledge of commits and branches you can probably come up with a solution yourself.

At this point you noticed that you want commits `D` and `E` on a new branch called `feature1`.
```
$ git branch feature1
```

To “remove” the commits from the master branch, you simply move the branch pointer two commits back. Please note the two carets (`^`) behind `HEAD`.
```
$ git reset --hard HEAD^^
```

Because commits are just referencing their parents, `D` and `E` are now unreachable from `master`. Now you can just switch to your new branch and keep working on it.
```
$ git checkout feature1
```

I hope you agree with me that this is really easy once you understood what these commands do internally. For further reading, I highly recommend reading the [Think-like-a-git](http://think-like-a-git.net/) website and the mentioned [Git Internals](https://github.com/pluralsight/git-internals-pdf) book by Peepcode.
Written by Hendrik Purmann
Berlin-based software engineer. Currently interested in cross functional teams, Microservices, GoLang, Kubernetes and the DevOps movement. |
| Readable Markdown | 28 Mar 2015
Let’s say you made a few commits and then realized that you did them to the wrong branch. Because git is awesome, it’s really easy to change the branch you committed to.
Let’s learn why\!
## Commits
Every commit stores the SHA-1 hash of its parent commit(s). Think of it as a directed acyclic graph with each node pointing to its parent(s).

## Branches
A branch is just a pointer to a commit. Git only stores a single file with the filename being the name of the branch. Inside, there is only the SHA-1 of the top-most commit.

Or to quote the great book [Git Internals](https://github.com/pluralsight/git-internals-pdf):
> Creating a branch is nothing more than just writing 40 characters to a file.
Go for it and have a look in the `.git` directory of some git repository.
```
$ cd .git
$ ls
COMMIT_EDITMSG config hooks info objects
HEAD description index logs refs
$ cd refs/heads
$ ls
master
```
The folder `.git/refs/heads` stores the branch reference files.
```
$ cat master
eb5c3831d6ebca824857d30cea70948201529ada
```
Let’s say you are on the master branch and create another branch named `dev`.
```
$ git branch dev
$ cat dev
eb5c3831d6ebca824857d30cea70948201529ada
```
The new branch `dev` is pointing to the same commit as master. If we draw that, it would look like this:

## Back to the problem …
With the knowledge of commits and branches you can probably come up with a solution yourself.

At this point you noticed that you want commits `D` and `E` on a new branch called `feature1`.
```
$ git branch feature1
```

To “remove” the commits from the master branch, you simply move the branch pointer two commits back. Please note the two carets (`^`) behind `HEAD`.
```
$ git reset --hard HEAD^^
```

Because commits are just referencing their parents, `D` and `E` are now unreachable from `master`. Now you can just switch to your new branch and keep working on it.
```
$ git checkout feature1
```

I hope you agree with me that this is really easy once you understood what these commands do internally. For further reading, I highly recommend reading the [Think-like-a-git](http://think-like-a-git.net/) website and the mentioned [Git Internals](https://github.com/pluralsight/git-internals-pdf) book by Peepcode.
Written by Hendrik Purmann
Berlin-based software engineer. Currently interested in cross functional teams, Microservices, GoLang, Kubernetes and the DevOps movement. |
| Shard | 119 (laksa) |
| Root Hash | 8589449272786986719 |
| Unparsed URL | com,hpurmann!/2015/03/28/git-moving-commits-to-another-branch/ s443 |