Archive for the ‘branch’ tag
Sometimes it is useful to copy a series of commit to another branch. This post describes how to do this using
git rebase. Two example illustrate different scenarios which should help to understand the command arguments in generell.
Example 1: The commit P, Q, R which are contained in branch feature should be copied to branch master. The commits should be appended to commit M.
The first command
git rebase --onto M O R copies the commits P, Q, R to the branch master. They won’t be visible if you check the visual graph in gitk. This is because there is no branch reference that points to one of the three commits. To update the branch master run the second command
git rebase HEAD master which moves up the HEAD to commit R.
While working with a version control system like git it is sometimes a good idea to create an independent branch. This article shows how to accomplish that. In this example I want to hold onto the documentation in a separate branch.
I assume, that you already created a repository and added various commits onto you
Prepare your ignore list
Now its time to prepare for the repository. First, edit your
.gitignore file and exclude the
doc/ folder which you will be creating in the next step.
Create an orphan branch
Create a new independent branch with the
git checkout option
--orphan. The option is available since Git 1.7.2. Initially, you will have to remove the files that have been created in the working directory, but right after the branch is independent.
Now you can create a
doc/ folder and create documentation files. In the end you can commit them to the repository as usual.
git checkout --orphan documentation
git rm -rf .
// Do work.
// git add your files to the doc/ folder
git commit -m "Added documentation files."
Checkout the master branch
To checkout the
master branch while still being able to list the documentation files do the following.
git checkout master && git merge --no-commit documentation
git reset master
The files added to the
documentation branch earlier will appear in the working directory visible to the
master branch. Thanks to the
.gitgnore file they will not be listed as untracked files.