This cheatsheet covers basic workflows for local and remote Git repositories.
For details on the Git commands mentioned, or on creating and cloning repositories, revisit the chapter on Git More Collaboration.
When working on a project by yourself, you will typically follow this workflow:
git status
git add .
git commit -m "Added feature X"
git push origin master
As you become more comfortable with Git and begin to build larger projects on your own, you'll want to use feature branches. This is a branch created in order to isolate changes related to a single feature or bug. Professional developers use them for a few reasons:
Fun Fact
Some development teams will only work in feature branches. On these teams, developers are NOT allowed to make changes directly to the master
branch. They must work in feature branches and create pull requests into master
to add those changes to the main code base.
In the workflow below, we use main
to refer to the branch from which the feature branch is created (often master
) and feature
to refer to the feature branch. Remember to use descriptive names for your branches.
main
create and move to the feature branch: git checkout -b feature
.feature
have been committed and pushed, move back to main
: git checkout main
.main
, resolving any conflicts: git merge feature
The case of a team working on a project with a single branch is very uncommon. However, multiple developers working on the same branch happens quite a bit. This workflow, therefore, can be thought of as a sub-workflow of the Team / Feature Branches workflow.
This workflow is similar to the Single Developer / Single Branch workflow, only now you must be mindful to merge in changes made by others.
git status
git add .
git commit -m "Added feature X"
git pull origin master
git push origin master
The workflow for a team of developers using feature branches combines the Team / Single Branch and Single Developer / Feature Branches workflows.
In the workflow below, we use main
to refer to the branch from which the feature branch is created (often master
) and feature
to refer to the feature branch. Remember to use descriptive names for your branches.
main
create and move to the feature branch: git checkout -b feature
. OR if contributing to a branch made by a team member, fetch and checkout their existing branch: git fetch origin
then git checkout feature
feature
have been committed and pushed, move back to main
: git checkout main
.main
, resolving any conflicts: git merge feature
. Alternatively, create a pull request <create-pr> into main
as described below.The ability to create pull requests is a powerful feature of GitHub that allows changes to be reviewed and discussed by team members.
A pull request is a request via GitHub to merge one branch into another. Team members can comment on and review the changes in the request, suggesting or requiring changes. Once the code is ready, the pull request is merged and closed. The code from the feature branch is now part of the destination branch.
Many teams use pull requests when using the Team / Feature Branches workflow.
To create a pull request, commit and push all changes in your feature branch. Then visit the project's GitHub page and click on the Branches link.
The Branches page shows all branches that have been pushed to GitHub. To the right of every branch (except master
) is a button to create a new pull request.
To create a new pull request, fill out the brief form describing the changes that it contains.
Once the pull request has been created, it remains in the Open state for team members to comment.
When the code is ready, the pull request is merged and closed. The code is then part of the destination branch.
A scenario that will occur from time-to-time in LaunchCode courses, and which occurs quite a lot for developers in general, is when you want to copy another developer's project and modify it. This process is known as "forking a repository" since if you view a project's history as a timeline, copying it effectively creates a "fork" in that history.
To fork another developer's repository, visit the project at GitHub and hit the Fork button:
This will create a copy of the remote repository under your GitHub profile. You will have a snapshot of the other developer's repository, taken at the moment you hit the Fork button.
From your own profile page, you will see the forked repository listed alongside your other repositories. To work on the code, clone the repository to your computer using the method above.
Forked repositories can easily be identified by the reference to the original project under the project name on your profile.