Branching#
Changes to code are saved with pointers to the commit snapshots
Allowing you to revert to a prior version
The first branch is typically
main
git branch # List branches
git branch [BRANCH_NAME] # Create a new branch
git branch -d [BRANCH_NAME] # Deletes the branch
git branch -m [NEW_BRANCH_NAME] # Renames the branch
-d
is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changesCommits to branch needs
git checkout
followed by the usualgit add
,git commit
, andgit push
.
Switch#
Moving to another branch
git switch [BRANCH_NAME] # Move to a different branch
Checkout#
To merge two branches, do:
checkout
git add .
git commit -a
git push
git branch dev # Made a new branch called dev
git switch dev # Moved branches
..... # After some time.... the code is ready
git check main # Switch to main bringing commit history
git add . # Add new files
git commit -m "merging dev / main" # Commit message
git push # Push changes
git switch dev # Go back to dev branch