0

Git Lifecycle

Here are the git bash commands that you’ll need for a more typical feature branch development.

Grab the repo

firstly we want to grab the project from the remote repo into a local folder. navigate in the terminal to the desired parent folder (or navigate there, right-click inside and select open terminal here) and enter the following:

  • For an SSH connection:
    workspace$ git clone git@bitbucket.org:UserName/projectRepo.git
  • and for a HTTP connection:
    workspace$ git clone https://UserName@bitbucket.org/UserName/projectRepo.git

cd into the folder, this will put you in the master branch, make sure everything’s ok with a quick status check

workspace$ cd projectRepo
projectRepo(master)$ git status

If the repo is empty then you’ll want to add a file to get things going and push it to the remote:

projectRepo(master)$ echo "# My project's README" > README.md
(master)$ git add README.md
(master)$ git commit -m "Initial commit"
(master)$ git push -u origin master

Work on the branch

So you’ve now connected to a remote repository and have either a single file in a folder to create a new project in (with the editor of your choice) or a new project to play with, either way we’ll need to create and checkout our first branch:

(master)$ git branch [BranchName]
(master)$ git checkout [BranchName]

or more simply:

(master)$ git checkout -b [BranchName]

whilst you’re working on a branch you’ll want to make regular commits, add or remove files from tracking and more:

  • Show status of current branch:
    ([Branchname])$ git status
  • Add file to current branch tracking:
    ([Branchname])$ git add [FileName]
  • Add all files of type in current dir:
    ([Branchname])$ git add '*.txt'
  • Remove file from tracking:
    ([Branchname])$ git rm [filename]
  • Delete local branch:
    ([Branchname])$ git branch -d [BranchName]
  • Push deletion of local to remote:
    ([Branchname])$ git push origin :[BranchName]
  • Commit branch
    ([Branchname])$ git commit -m "commit comments"

Once you’ve made your final commit and you want to merge your changes back into the main project for other developers to use you’ll want to initially push your branch to the remote (there are other ways to handle releases but this is a common one).

Before that happens make sure that your changes are compatible with any updates to the remote repository:

  • update master in your repo:
    (master)$ git pull origin --all
    (master)$ git pull origin --tags
    (master)$ git fetch origin master
  • merge master back into branch:
    ([Branchname])$ git merge master
  • push branch to remote:
    ([...])$ git push -u origin [BranchName]

Now that you’ve pushed your change branch to the remote repo you can raise a pull request which enables other developers to take a look at the code changes you’ve proposed and if they’re good they can be merged into the master branch of the remote repo – available for anyone to merge into their own master branches.

JBaker

Joi is an IT Development Analyst working in London and a consummate technophile. Whilst he isn't coding he can be found reacquainting himself with his guitars, spinning poi, looking for climbing walls or chasing the latest shiny thing to cross his field of vision.

Leave a Reply

Your email address will not be published. Required fields are marked *