Skip to content

Instantly share code, notes, and snippets.

@saurabh-sp-tripathi
Created July 13, 2018 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabh-sp-tripathi/d9660ba0c5fe71f622bebe871f33dad9 to your computer and use it in GitHub Desktop.
Save saurabh-sp-tripathi/d9660ba0c5fe71f622bebe871f33dad9 to your computer and use it in GitHub Desktop.
A brief Git introduction

Why Git:

  • Local and distributed :
    • So what?
    • Local is faster : Think about it, reading from HDD/RAM is faster than reading from www.imslow.com each time.
    • It not only saves your time on local operations like read history, diff, work offline [-who doesn’t have internet??] but also makes collaborating tools like Jenkins faster to act.
  • Branch policies & Code review (GitHub, GitLap, VSTS ets): Tools made upon git have eligant feature like
    • Code review: Following pull request workflow (more on this later), you can set branch policies to make sure that a code is reviewed before getting into the branch.
    • Branch policies to prevent anyone directly committing into the branch like prod branch 'master' (similar to trunk)
  • Lightweight:

Git Basics - I : Local and remote repository

  • Because git is lightweight, when you clone a project (like svn checkout), it copies the whole repository i. e all the branches (on just one like svn), all commit history etc]
  • Remote repository is generally referred with prefix origin/ and local with no prefix. Let's say in reliant you have these 5 branches master (the prod branch like svn trunk), autopay, nest, prepay and one-click then if you are working in git you may probably have remote repo as {origin/master, origin/autopay, origin/nest, origin/prepay, origin/one-click} and local repo as {master, autopay, nest, prepay, one-click}
  • Hence a important point for fellows coming from SVN : **> git commit ** is commit operation to local repository and makes changes appear in remote repo you will have to do ** > git push *** but more on that later

A Typical day in dev's life

  • Code :
Calcu.java

public void greet(String name) {
    system.out.println("Hello "+name);
}

  • New requirement came in: greet in spanish

Steps:

  • Identify/determine the url of repository say : http://vsts.com/greet-app
  • Checkout project : git clone http://vsts.com/greet-app
  • Remember as said you have all the branches in local repo. By default you are on master branch
  • create a new local branch for it -- espanol-greet
> git checkout -b espanol-greet
  • You may like to create a corresponding remote branch for it at some point of time; for ease of understanding let's do it now
git push -u orgin espanol
  • Open file Calcu.java and edit
public void greet(String name, String lang) {

    if(lang != null && lang.equals("es")) {
        System.out.println("Ola "+name);
    } else {
        system.out.println("Hello "+name);
    }
}
  • Add file to staging area > git add .
  • Commit file to local repo ``` > git commit -m "Adding support for spanish"

Again, this commit is local to your repository and hence not visible to others.

  • Push your work to remote repository i.e origin/espanol-greet > git push

  • Create a Pull request to origin/develop i.e Merge your code to develop : You might have origin/develop branch, code from which is deploy to dev -> test ==promote==> stage --> test. To merge your code from orgin/espanol ===> origin/develop you will need to create pull request. It's a simpe operation of VSTS site of around 2 drop-down, 2 text box and 3 button clicks. More on that later.

  • Once pull request is made, it must be reviewed and approved by reviewer to get it merged.

A typical git lifecycle: clone ====> FETCH (update your repo before committing ) ====> work/code ======> add modified files to staging area : git add . ====> commit (could be more than one) git commit -m <message> ====> git push (***could be more than one ***) ======> Pull request to origin/develop ====> approve & merge ====> test in dev/stg ====> pull request from origin/develop to origin/master ====> test in prelive ====> prod release

Simply : clone/fetch ===> add ===> commit ====> push ===> pull request

And that's a bird eye view of small set of git operations. More on it later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment