We just migrated our source code to Git (GitLab) so that we decided to develop some git convention. I share our internal kitchen ’cause it could be useful for others as well. The main idea keeping master as only source for releases and all development should be done on branch called dev. Also we keep in reserve branch stage in case we will have to provide separate branch for qa purpose. For now we do not have dedicated QA on the given project so we directly merge from dev to master but if you have QA on project use following merge scheme dev -> stage -> master. Also on GitLab we have configured CI. For each push to remote repository CI builds project and show warning on MergeRequest in case project build/test fails.
Precondition
- We will use branch master only for releases!!!
- All changes should be merged to branch called dev and only then moved to master
- All changes from dev should be moved to master with GitLab Merge Requests. Do not run any actions manually through console!
- All development should be done on private branch created for each new task
- All changes from private branches should be moved to dev with GitLab Merge Requests
- Private branch should be deleted after approval of Merge Request
How to create dev branch
- Switch to master
git checkout master
- Pull latest changes from origin master with discarding any local changes
git pull --rebase origin master
- Create new branch dev on local env
git checkout -b dev
- Push local branch dev with command
git push -u origin dev
How to create new private branch for development and then create Merge Request
- Sync local environment with git repository structure. Execute command once to let your local structure know about dev branch
git fetch
- Switch to branch dev
git checkout dev
- Get latest changes from dev
git pull --rebase origin dev
- Create and switch to private branch
git checkout -b <my_private_branch_name>
- Development itself
- Commit your changes to local repository
git commit -am "commit description"
- Push changes to remote repository
git push -u origin <my_private_branch_name>
Pay attention on key -u. You can find good explanation on StackOverflow
- Create Merge Request with GitLab user interface. I recommend to select option “Remove source branch” and if you have enterprise version of GitLab also select option “Squash commits”
- Review Merge Request!
- Resolve conflicts if required
- Switch to branch dev again
git checkout dev
- If you would like to remove local branches which are already removed on remote execute
git fetch --prune
How to resolve Merge Request conflicts
- Switch to local version of dev
git checkout dev
- Pull latest changes from origin dev with discarding any local changes
git pull --rebase origin dev
- Switch back to private branch
git checkout <my_private_branch_name>
- Rebase private branch on top of dev latest changes (reapply your work on top of the incoming changes)
git rebase dev
- Resolve conflicts manually in conflicted files with IDE
- Commit changes
git commit -m "commit description"
- Push changes with force key
git push -f origin <my_private_branch_name>
- On this step Merge Request could be approved by reviewer
P.S.
In case you need to remove local branches which already deleted on remote repo, call
git remote prune origin
List all local branches with command
git branch
If you found mistake or just have some idea for improvement – let me know by mail andriy@andrunevchyn.com