DEV Community

Cover image for Reference Guide: Committing Changes with Branches
kymiddleton
kymiddleton

Posted on • Edited on

Reference Guide: Committing Changes with Branches

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:

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 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 or master branch to an alternate page view or branch.
    • -b initiates a new branch to be created.

git checkout b

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 indicates all files. This also transitions the code to be staged.
  • git status
    • Repeating this command will show the files previously highlighted in red have switched to green indicating changes have been tracked, saved and ready to push to the repository.
  • 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:

Top comments (2)

Collapse
 
sjehutch profile image
scott hutchinson

Really good post so many devs struggle with git

Collapse
 
geshan profile image
Geshan Manandhar

Good for beginners.