Skip to content

Simple Git Workflow

--> committed --> staged (working changes) --> not staged

Git Simple Workflow

Checkout my .gitconfig for some helpful shortcuts! My, git graph is an awesome way to get an in-console view of the commit history, I use it frequently.

The Workflow

To begin, clone the repository you need to work on.

sh
git clone [email protected]:PROJECT_NAME/REPO_NAME.git
cd PROJECT_NAME
git clone [email protected]:PROJECT_NAME/REPO_NAME.git
cd PROJECT_NAME
sh
git fetch origin
git merge origin/TARGET_BRANCH
git fetch origin
git merge origin/TARGET_BRANCH

If the repo is already cloned locally then pull all remote changes. update the master/main branch with remote changes

sh
git checkout master
git pull
git checkout master
git pull

Next, create the branch that will be used to develop, iterate, and test changes

sh
git checkout -b <name-of-your-branch>
git checkout -b <name-of-your-branch>

Once you've made a few changes stage the new code to be committed

sh
git add .
git add .

(You may want to specify which files to stage explicitly instead of using the . catchall.)

Ensure the code you've staged compiles and that all of the tests are passing. Depending on the way you've set up your build this may look something like

sh
./run // e.g. a bash script that standardizes the build with cmake etc.
./build/unittests
./run // e.g. a bash script that standardizes the build with cmake etc.
./build/unittests

If everything is working well, update the version number in the top-level CMakeLists.txt, then stage the change and commit.

sh
git add CMakeLists.txt
git commit -m "A descriptive message"
git add CMakeLists.txt
git commit -m "A descriptive message"

After a few commits and your code is ready, push your commits to the remote repo (do this often, especially if you use CI/CD, to catch build and test issues)

sh
git push -u origin <name-of-your-branch>
git push -u origin <name-of-your-branch>

This will set the remote repo to origin and push your branch to it. The -u option only has to be used on the first push to specify where the upstream branch at the remote location The -u option only has to be used on the first push to specify where the upstream branch at the remote location.

Once you've completed all the pushes and merged the branch into master, you can clean your repo by removing the remote and local branches that are no longer needed.

sh
git pull -p
git branch -d <branch-name>
git pull -p
git branch -d <branch-name>

The first of the two commmands above prunes the repo, removing any remote branches that were merged and deleted on the source control portal (e.g. GitLab). The second command removes the local branch.