Git branching

Creating new branches

# Create a new branch
git branch bugFix

# Switch to the new branch
git checkout bugFix

# Commit a new change
git commit

Checkout to a defined commit

In git, you not only can checkout into branches but rather into commits too.

# Get the commit head
git log

# In this example, the commit will have a random hash:
# fed2da64c0efc5293610bdd892f82a58e8cbc5d8

# Checkout to the example commit
# Since git is intelligent, we can just use `fed2` instead of the full hash.
git checkout fed2

Checkout to a variable commit

Git also knows variables, namely the ^ and the ~ variable. With those variables, instead of using the hash, you can just use those. The ^ variable will go back one commit, while with the ~ you can go back a number of commits. For example ~2.

# Checkout to a head
# Since git is intelligent, we can just use `fed2` instead of the full hash.
git checkout main^

git checkout main~2

Merging two branches

# Go to the main branch
git checkout main

# Commit a change
git commit

# Merge `bugFix` into `main`
git merge bugFix

Rebase

# Go to the main branch
git checkout main

# Commit a change
git commit

# Switch back to the bugFix branch
git checkout bugFix

# Commit a change
git commit

# Rebase the commits
git rebase main