When I first started working with branches I could never remember which branch I was on when it came time to commit changes. My go-to command quickly became git branch
.
If you missed part of the series:
- Part One: Reference Guide: Common Commands for Terminal.
- Part two: Create a GitHub Repository
- Part three: Committing Changes
COMMITTING CHANGES WITH BRANCHES
It’s best to think of the master
branch as production. Therefore, it’s best to never push to the master branch. When using branches remember they run parallel to the master. As soon as a new project is created get into the habit of creating a new branch before making file changes. It’s also best to create a new branch for each design feature.
Branch commands:
-
git branch
- Displays branches within current project including the master branch.
-
git checkout <branch name>
- Used to switch to a different branch.
Make a new branch:
-
git checkout –b <name of new branch>
-
Checkout
is similar to the homepage of a website by indicating a switch from the homepage ormaster
branch to an alternate page view orbranch
. -
-b
initiates a new branch to be created.
-
When code is ready to be committed to a branch use the following commands:
-
git status
*Items that appear in red indicate the files and subsequent changes are not being tracked. These files need to be staged and submitted. -
git add –A
- This command initiates changes being tracked and the
A
indicatesall
files. This also transitions the code to be staged.
- This command initiates changes being tracked and the
-
git status
- Repeating this command will show the files previously highlighted in
red
have switched togreen
indicating changes have been tracked, saved and ready to push to the repository.
- Repeating this command will show the files previously highlighted in
-
git commit –m “add comment description of changes made”
- Staged code is now saved locally with a note to others about the code. Makes commit messages descriptive and meaningful.
-
git status
- Repeat command to verify all changes have been staged and ready to push to the repository.
-
git push origin <name of branch>
- Saved changes are pushed to the named branch.
Up next: Handling Merge Conflicts
For the completed Reference Guide Series:
- Part One: Reference Guide: Common Commands for Terminal.
- Part two: Create a GitHub Repository
- Part three: Committing Changes
- Part four: Committing Changes with Branches -Part five: Merge Conflicts
- Part six: Pull Requests
- Part seven: Conducting a Code Review -Part eight: Complete and Merge a Pull Request
Top comments (2)
Really good post so many devs struggle with git
Good for beginners.