Skip to content

Git workflows

Below are various typical workflows for using Git in a variety of scenarios.

Downloading source code from an existing repository

Clone the repo

git clone <repo_url> [path/to/local/repo]

Check the project page for the exact url of the repository.

The path/to/local/repo argument is optional. If omitted, git will create a directory named after the repository under the current directory.

Switch to the main development branch

git checkout develop

The main development branch is usually named develop, but not always. Check the project details.

Update the develop branch

git pull origin develop

Always make sure you have the latest version of the code. This will save you hours of conflict resolution down the road. Update the code frequently.

Working on a new feature

Start from the main development branch

git checkout develop
git pull origin develop

The main development branch is usually named develop, but not always. Check the project details.

Create a new local feature branch

git checkout -b feature/<feature>

Pick a name for the local feature branch that is short and relevant to the feature being worked on. Do not reuse an existing local branch unless it is related to the same feature. Branches in git are cheap and easy to create. There is no need to recycle them.

Tip

In some projects, the convention is to name the feature branch after the Trello or Jira ticket number. Follow whatever convention has been adopted by your project or team.

Work on the local feature branch

  • work on the local feature branch
  • build the app
  • test locally

Commit to the local branch

git add .
git commit -m "..."

All commits require a message describing the changes. Commit messages MUST follow the conventional commits specification.

Recording changes in git is a 2-step procedure, where first the changes are added then committed.

You can combine both operations in a single command with git commit -a -m "...".

Merge or rebase from the develop branch

git checkout DEVELOP
git pull origin DEVELOP
git checkout feature/FEATURE
git merge DEVELOP

Alternatively, you can rebase from the develop branch

git rebase DEVELOP

Resolve conflicts

Pay attention to any conflicts reported by the git merge operation. Resolve them appropriately. If you had conflicts, then you will need to run:

git add .
git commit -m "..."

Rebuild the app and test it locally.

Push to remote

If everything works, then push to the remote repo:

git push origin features/feature

Danger

Your changes are not saved to the remote repo until you have pushed them.

Creating a new branch from local code

Assume you have a local directory with some code that is as yet unversioned, and you want to version it.

  • Create a new repo in github, gitlab or bitbucket
  • cd to your project directory
  • Create a local repo: git init .
  • Add the remote repo
git remote add origin REMOTE_REPO_URL
git push origin main

Duplicating a repo

You wish to duplicate an entire repository (including all the branches, tags, and the full history of changes), for example from one server to another.

Make sure you have the rights to duplicate the repo before doing so.

git clone --bare ORIGINAL_REPO_URL
cd ORIGINAL_REPO
git push --mirror NEW_REPO_URL

See this tutorial.