git branch
To create a branch, run:
$ git branch development
To create a branch from a particular commit, run:
$ git branch development 56befbfbd06788dad7107cac7046b2e95b768ee2
The last option is the commit hash code from which to branch.
To list all branches, run:
$ git branch
To select a branch to work with, run:
$ git checkout development
To create and checkout a branch, run:
$ git checkout -b feature1
Note that git checkout
may also be used to revert changes on a
file to the last committed changes.
What if I have untracked changes in my active branch while trying to checkout another branch?
I may have created a new file...
$ git checkout development
$ printf "I will not stage this new file" > unstagedFile.txt
$ git checkout master
Or edited an existing file.
$ git checkout development
$ printf "I will not stage this change" > trackedFile.txt
$ git checkout master
Untracked changes are brought to the checked out branch.
What if I have staged changes in my active branch while trying to checkout another branch?
$ git checkout development
$ printf "I will not stage this change" > file.txt
$ git add file.txt
$ git checkout master
Staged changes are brought to the checked out branch ONLY if they do not cause conflict.
Otherwise, you will see a message like this:
$ git checkout development
error: Your local changes to the following files would be overwritten by checkout:
file.txt
Please commit your changes or stash them before you switch branches.
Aborting
What if I have committed changes in my active branch while trying to checkout another branch?
$ git checkout development
$ printf "I will not stage this change" > stagedFile.txt
$ git add stagedFile.txt
$ git checkout master
Committed changes stay in the original branch.
To see the content of the file that is causing the conflict:
$ cat file.txt
To compare the content of the files:
$ git diff file.txt
To see the content of the file in another branch:
$ git show dev:file.txt
git branch
To delete a branch, run:
$ git branch -d development
If the branch you are trying to delete is currently checked out, the command will fail:
$ git branch -d development
error: Cannot delete branch 'development' checked out at '~/repos/file_state_repo'
Git will also "complain" if the branch to be deleted has changes that have not been merged into the current branch:
$ git checkout master
Switched to branch 'master'
$ git branch -d development
error: The branch 'development' is not fully merged.
If you are sure you want to delete it, run 'git branch -D development'.
To delete the branch anyway, run:
$ git branch -D development
Git is a distributed version control system: It allows developers to make and record changes independently, and combine their changes at any time—all without a central repository.
A merge unifies two or more commit history branches.
A merge must occur within a single repository.
When modifications in one branch do not conflict with modifications found in another branch, Git computes a merge result and creates a new commit that represents the new, unified state.
When branches conflict, which occurs whenever changes compete to alter the same line of the same file, Git does not resolve the dispute.
git merge
To merge branch source
into branch target
, you must:
$ git checkout target
$ git merge source
Let's open the Visualizing Git App and play with branches.
$ mkdir my_repo
$ cd my_repo
$ git init
$ touch readme.md
$ git add .
$ git commit -m "First commit"
b1
and b2
$ git branch b1
$ git branch b2
$ git checkout b1
$ printf ".DS_Store" > .gitignore
$ git add .
$ git commit -m "Add basic .gitignore"
$ git checkout b2
$ printf "Hello" > README.md
$ git add .
$ git commit -m "Add README.md"
b2
into b1
:
$ git checkout b1
$ git merge b2
b2
:
$ git branch -d b2
Consider the following case. What will happen when the last command is invoked?
$ git init
$ touch readme.md
$ git add .
$ git commit -m "First commit"
$ git branch dev
$ printf "# My title" > readme.md
$ git add .
$ git commit -m "Added title"
$ git checkout dev
$ printf "# A much better title\n\n ## With a header below it" > readme.md
$ git add .
$ git commit -m "Added a nice title and header"
$ git checkout master
$ git merge dev
To find the conflicting files, run:
$ git status
What happens inside a conflicting file?
<<<<<<< HEAD
This is a nice project!
=======
# This is a better title!
## And I'll throw in a header
> > > > > > > dev
We should manually assess the conflicts and change the files.
Then stage and commit the changes:
$ git add readme.md
$ git commit -m "Merged 'dev'"
git rebase
Consider the following situation:
How do I get the C, D, and E into my topic
branch?
I can rebase these commits and get the following situation:
By running the commands:
$ git checkout topic
$ git rebase master
or:
$ git rebase master topic
git merge
vs git rebase
Merge preserves history whereas rebase rewrites it.
Figure from hackernoon.com