Git branches and commits
- Commits are created with
git commit
- Branches are just labels for commits
- "Merging branches" means merging the histories of commits that two branches point at.
- Most git commands operate on where
HEAD
is pointed (i.e. which branch is currently checked out)
Creating a branch
$ git checkout -b <branchname>
Merging a branch
To merge a branch INTO master
$ git checkout master
$ git merge <branchname>
Fixing merge conflicts
During a merge
If you have conflicts during a merge you'll see something like:
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
You will see markers in the conflicting file. Remove these and make the file look the way it's supposed to.
<<<<<<< HEAD
Super
=======
Cool
>>>>>>> cool
becomes...
Super
Cool
Then you need to mark the file as resolved:
$ git add .
$ git commit
During a rebase
If you have conflicts during a rebase you'll see something like:
error: could not apply 186b0f9... Cool
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply 186b0f94708ace2b881aa1ce63a824fa4194f7a2... Cool
You will see markers in the conflicting file. Remove these and make the file look the way it's supposed to.
<<<<<<< HEAD
Super
=======
Cool
>>>>>>> cool
becomes...
Super
Cool
Then you need to mark the file as resolved:
$ git add .
$ git rebase --continue
Moving a branch to a commit or another branch
git branch --force <branch-to-move> <commit-or-branch-to-move-to>
or
git reset --hard <commit-or-branch>
Modifying history
To remove, edit, or reorder commits:
git rebase -i <commit-before-the-first-commit-you-want-to-change>
Undoing
To undo changes, without removing them from history:
git revert <commit-to-undo>
Note: this only undoes the changes in that commit, unlike reset
which "undoes" all the commits between HEAD and the specified commit.