Loading...
 

Git

Contents

Git Flow

Branches

PROD

  • The branch for tracking state of live configuration

DEV

  • Branched from PROD
  • Branch for staging changes into, before releasing into PROD

some_feature_branch

  • Branched from DEV
  • used for independently working on new features or configuration updates

Flow

  1. create feature branch from DEV
  2. clone feature branch locally
  3. make modifications
  4. commit changes
  5. push changes from local feature branch into remote feature branch
  6. submit pull request (closing feature_branch) to merge feature branch into DEV branch. Include approver
  7. approver reviews pull request and fixes conflicts (checkout and update both branches locally and merge DEV into feature_branch, then conflicts will show up directly in the files for easy comparison)
    1. approver commits and pushes conflicts directly into feature branch if necessary which will automatically show in the original feature request
  8. approver approves and merges the feature branch
  9. all developers will see their local DEV branch now behind the remote origin, so will need to pull the changes

Git setup

Initialisation

$ mkdir local_repo
$ cd local_repo
Clone it
$ git clone http://SOMESERVER.COM/REPO/NAME.git .
Cloning into 'NAME'...
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 35 (delta 15), reused 26 (delta 9)
Unpacking objects: 100% (35/35), done.
Create local file structure
$ git init
Set my commit name
$ git config user.name "Gareth Brown"
use a helper for caching my login credentials
$ git config credential.helper cache
NB. Pass --global to git config to save settings for git globally instead of in ~/.git/config
Verify config
$ git config --list
user.name=Gareth Brown
credential.helper=cache
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.upstream.fetch=+refs/heads/*:refs/remotes/upstream/*

Checkout a Branch

On your local system, make sure you have a local repository cloned from the remote repository. Then, do the following:

Change to the root of the local repository:
$ cd mytestproject
List all your branches:
$ git branch -a
You should see something similar to the following:
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature
  remotes/origin/master

Notice that it lists both the branches that are local and the remote branches on Bitbucket. Using the list as reference, choose the branch you want to checkout. In this example, the feature branch is the branch.

Checkout the branch you want to use:
$ git checkout feature
Confirm you are now working on that branch:
$ git branch
You should see something similar to the following:
$ git branch 
* feature
  master

Going forward, all your Git commands apply to the branch. When you push the changes to your remote Bitbucket repository, those changes apply to the repository’s branch.

Repository Update

Add an upstream repository
$ git remote add upstream http://SOMESERVER.COM/REPO/NAME.git
Verify repository
$ git remote -v
upstream        http://SOMESERVER.COM/REPO/NAME.git (fetch)
upstream        http://SOMESERVER.COM/REPO/NAME.git (push)
Download objects and references
$ git fetch upstream
remote: Counting objects: 35, done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 35 (delta 15), reused 26 (delta 9)
Unpacking objects: 100% (35/35), done.
From http://10.102.83.61/monitoring/zabbix
* [new branch]      app_config_structure -> upstream/app_config_structure
* [new branch]      development -> upstream/development
* [new branch]      production -> upstream/production
* [new tag]         event_management -> event_management
* [new tag]         monitoring -> monitoring
* [new tag]         zabbix     -> zabbix
Show remote status
$ git remote show upstream
* remote upstream
  Fetch URL: http://10.102.83.61/monitoring/zabbix.git
  Push  URL: http://10.102.83.61/monitoring/zabbix.git
  HEAD branch: production
  Remote branches:
    app_config_structure tracked
    development          tracked
    production           tracked

Add push to multiple repositories

To do this, you will need to create an all (or some other name) repository and update the push repo to include multiple end points.

git remote add all https://my.main-repo.com/repo.git
git remote set-url --add --push all https://my.other-repo.com/repo.git
git remote set-url --add --push all https://my.main-repo.com/repo.git
$ git remote -v
all     https://my.main-repo.com/repo.git (fetch)
all     https://my.other-repo.com/repo.git (push)
all     https://my.main-repo.com/repo.git (push)


Git and IDE

.gitignore

having this file is important, so as to not include any unwanted files created by IDEs. For example, contents could look like the following for PyCharm

*.iml
*.ipr
*.iws
.git
.idea/*

Merge conflicts

The Bitbucket documentation on this is very clear

Git commands

  • Create new local branch
$ git checkout -b tidyup_files
  • Commit code
$ git commit -am "some message explaining what changed"
  • Push local branch and create new remote branch
$ git push -u <upstream_name> <remote_branch_name>