git advanced 

session

Git merge

           Fast-Forward                                           3 way merge
                                          

merge continued..

$ git merge --no-commit 

<branch-name>


$ git merge --no-ff  <branch-name>

                         

merge continued..

* Inspecting conflict:
   $ git diff
   $ git   ls-files    -u
   
   $ git branch --merged   
   $ git branch --no-merged

diff3 merge conflict style:
 $ git  config  --global   merge.conflictstyle   diff3   
     -Shows previous HEAD, your changes, and conflicting 
       changes

MERGE CONTINUED..

Tips for minimizing merge conflicts:

   - One branch for one task only
   - Resolve conflicts early

   - Can be avoided if we think and talk 
      with our colleagues before committing

  - Reduce modifying common blocks

  - Reduce modifying global data
  

git rebase 

       
* Rebasing enables fast-forward merges by moving a branch
   to the tip of another branch.                
* Eliminates the need for merge commits, resulting in a 
   completely linear history.

   $ git rebase master new-feature
    $ git checkout master,   $ git merge new-feature  

rebase continued..

  $ git rebase --abort

* Using i mode, commits can be squashed. 

   Word of caution: Only do this on commits, that haven't been
   pushed onto external repository(Used in local repo). Else a
   lot of conflicts may occur!

    $ git rebase -i HEAD~4

* Use  $ git pull --rebase  instead of  $ git pull.

git stash

* Pauses whatever we are currently working on and come back to it later. 

* Now we want to switch branches, but don’t want to commit 
   what we have been working on yet.  We need to do:

     $ git add .
     $ git stash
            
* To see which stashes you have stored:
     $ git stash list

stash continued..

* To apply stash:
     $ git stash apply
     $ git stash apply stash@{2}

* To un-apply stashes:
    $ git stash show -p stash@{0} | git apply -R

* To remove stashes:
    $ git stash drop stash@{2}
    $ git stash clear


git hooks

* These are scripts that are run by Git before or after certain
   commands.
   Hooks are located at your repo .git/hooks directory

* Used to add conditional checks like :
   - Spell check the commit message
   - preventing an accidental rebase from occurring, etc

* Here are some categories:
        - pre-applypatch:
        - applypatch-msg
        - post-applypatch  

hooks continued..

   - pre-commit
   - pre-rebase
   - post-commit
   - commit-msg
   - pre-receive
   - post-receive

git sub-modules

* Allows to keep a Git repository as a sub directory of another
   Git repository.
Starting with Sub-Modules
* To add library project to your project:
     $ git submodule add <path> library_name

* It then creates .gitmodules file, a configuration file that 
  stores mapping between project's URL & local sub dir.

* To view the contents of .gitmodule file, run:
         $ cat .gitmodules

sub-modules continued..

Then we get result like:
        [submodule "testing"]
                     path = testing
                     url = coe@gitserver:testing.git

* It can be pushed and pulled with the rest of your project.

Cloning a Project with Submodules

When we receive such a project, we get the directories that
   contain submodules, but none of the files yet.
   For example,

SUB-MODULES CONTINUED..

$ git clone <path>

         Everything is fine here. But the sub-directory folder will be empty. In order to fetch its contents, we need to run 
following 2 commands:

* $ git submodule init
         - To initialize the local config file

   $ git submodule update
        - To fetch the contents

git flow

                             

flow continued..

* Its a Git Branching Model, developed by Vincent 
  Driessen.

* Totally 5 branches are supported:

- master
- develop
- feature
- release
- hotfix

FLOW CONTINUED..

* After installing the 'Git flow': $ git flow init
  Then it will prompt you to some questions:
   Branch name for production releases: [master]
   Branch name for "next release" development: [develop]
   How to name your supporting branch prefixes?
   Feature branches? [feature/]
   Release branches? [release/]
   Hotfix branches? [hotfix/]
   Support branches? [support/]
   Version tag prefix? []

FLOW CONTINUED..

* Main branches: master & develop
                                

FLOW CONTINUED..

* Supporting branches:
1) feature branch:
                               

feature branch CONTINUED..

* Used to develop new features for the upcoming or 
   a distant future release.

* Typically exist in developer repos only, not in 
   origin.

    $ git checkout -b myfeature
    $ git checkout develop
    $ git merge --no-ff myfeature
    $ git branch -d myfeature
    $ git push origin develop

FEATURE BRANCH CONTINUED..

Then how does git flow help here?

   - Start a new feature using:
      $ git flow feature start <name> 
   This will create a new branch called feature/<name> based on develop branch and git-flow automatically switch to it. Now when you’re done.

    - Just finish it using:
     $ git flow feature finish <name>
     It’ll merge feature/<name> back to develop and delete the feature branch. Nice, right???;) :)

git flow CONTINUED..

2) Release branch:

     $ git flow release start <name>
     $ git flow release finish <name> 

   When you finish a release branch:

       - It’ll merge your changes to master and back to develop, also git-flow will create a tag for this release.  

GIT FLOW CONTINUED..

3) Hotfix branch:

     $ git flow hotfix start <name>
     $ git flow hotfix finish <name> 

  - When you start a hotfix branch, it’s based on master so you can quickly fix it when something is broken on production

  - When you finish it, it’ll merge it back to master and develop.

References








Thank You

Git Advanced Session

By Torry Harris Business Solutions

Git Advanced Session

This covers: Git Merge, Rebase, Stash, Submodules, Hooks, &amp;amp;amp;amp; Flow

  • 1,700