Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git Advanced

Kapil Sharma
December 20, 2014

Git Advanced

Git advanced commands presentation by Kapil Sharma during PHP Reboot's 'Working with Git' meetup on December 20th, 2014.

Kapil Sharma

December 20, 2014
Tweet

More Decks by Kapil Sharma

Other Decks in Technology

Transcript

  1. Short intro Kapil Sharma Technical Architect Ansh Systems Pvt. Ltd.

    Skype: kaps8981 Email: [email protected] Github: kapilsharma Twitter: @kapilsharmainfo Linked in: KapilSharma Kapil Sharma PHP REBOOT 3
  2. What will we cover now? In first session, we already

    covered Git basics. This session will include  Working with logs  Stash  Versioning basics  Git workflows  Gitflow Workflow in details Kapil Sharma PHP REBOOT 4
  3. Git logs  Quickly show past commit logs.  Important

    to check history.  Git log provide lot of options to format log as per our requirements  Also provide many options to filter logs.  Lets check the basics Kapil Sharma PHP REBOOT 5
  4. Git logs git log Kapil Sharma PHP REBOOT 6 

    Most basic logging option  It will display ‘commit hash’, ‘Author name and email’, ‘Date of commit’ and ‘message’.  With lot of commits in the project, might be difficult to read without formatting and filtering.
  5. Git log sample output git log Kapil Sharma PHP REBOOT

    7 $ git log commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> Date: Sat Dec 13 13:42:53 2014 +0530 readme.md finished commit 95c22815ccf7f0d4fc0c29df700c5ece37c8afbb Author: kapilsharma <[email protected]> Date: Sat Dec 13 13:17:37 2014 +0530 added config part ….
  6. Limit our logs git log -x Kapil Sharma PHP REBOOT

    8  Here ‘x’ is any integer.  By providing –x, for example ‘git log -2’, only last two commits will be displayed.
  7. Example - Limit our logs git log -2 Kapil Sharma

    PHP REBOOT 9 $ git log commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> Date: Sat Dec 13 13:42:53 2014 +0530 readme.md finished commit 95c22815ccf7f0d4fc0c29df700c5ece37c8afbb Author: kapilsharma <[email protected]> Date: Sat Dec 13 13:17:37 2014 +0530 added config part
  8. Log Changes git log -p Kapil Sharma PHP REBOOT 10

     Just like git log but also show deleted/added lines of code  Each log show lot of data so should be used with limited number of logs.
  9. Kapil Sharma PHP REBOOT 11 $ git log -p -1

    commit 779a83dd085a3eabd3a90524234f841a993700cd Author: unknown <[email protected]> Date: Wed Nov 19 21:03:18 2014 +0530 Issue20 diff --git a/public_html/meetup.php b/public_html/meetup.php index 6f3cd45..87efacc 100644 --- a/public_html/meetup.php +++ b/public_html/meetup.php @@ -16,7 +16,7 @@ require_once($config['include_path'] . 'header.php'); <p> - Saturday, December 13th, 2014 <br/> + Saturday, December 20th, 2014 <br/> 11:00 a.m. to 2:00 p.m. </p> <p> @@ -41,7 +41,7 @@ require_once($config['include_path'] . 'header.php'); </p> <p> <strong>Workshop: (13:15-14:00)</strong><br/><br/> - Once of the common feedback we found during meetups is, + Once of the common feedback we found during meetups is, <strong>Prerequisite for Git Workshop:</strong><br/> (END)
  10. Stat option git log --stat Kapil Sharma PHP REBOOT 12

     Like git log –p but show summary of changes with number of added/deleted lines.  Example $ git log –stat -1 commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> Date: Sat Dec 13 13:42:53 2014 +0530 readme.md finished readme.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
  11. Pretty output git log –pretty=<option> Kapil Sharma PHP REBOOT 13

     Quickly format logs in some predefined or custom format.  Predefined options are: oneline short full fuller
  12. Pretty output - oneline git log –pretty=oneline Kapil Sharma PHP

    REBOOT 14  Show commit hash and commit message in one line. Useful to compare many commits.  Example: $ git log --pretty=oneline e08b0fa68fb7e11de15572785d0d342df770db07 readme.md finished 95c22815ccf7f0d4fc0c29df700c5ece37c8afbb added config part 79956e0332fbe6ab3a9ca130b1b805fca5f987ea Commit early ba9add8e27720d8627a68cc14957332988dfc990 First commit
  13. Pretty output - short git log –pretty=short Kapil Sharma PHP

    REBOOT 15  Show commit hash, author and commit message. Useful to compare many commits.  Example: $ git log --pretty=short commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> readme.md finished commit 95c22815ccf7f0d4fc0c29df700c5ece37c8afbb Author: kapilsharma <[email protected]> added config part
  14. Pretty output - full git log –pretty=full Kapil Sharma PHP

    REBOOT 16  Show commit hash, author, committer and commit message. Useful to compare many commits.  Example: $ git log --pretty=full -1 commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> Commit: kapilsharma <[email protected]> readme.md finished
  15. Pretty output - fuller git log –pretty=fuller Kapil Sharma PHP

    REBOOT 17  Show commit hash, author, author date, commit, commit date and message. Useful to compare many commits.  Example: $ git log --pretty=fuller -1 commit e08b0fa68fb7e11de15572785d0d342df770db07 Author: kapilsharma <[email protected]> AuthorDate: Sat Dec 13 13:42:53 2014 +0530 Commit: kapilsharma <[email protected]> CommitDate: Sat Dec 13 13:42:53 2014 +0530 readme.md finished
  16. Pretty output - custom git log –pretty=format="options" Kapil Sharma PHP

    REBOOT 18  Git also allow us to custom format logs  Available options: %H - Commit hash %h – Abbreviated commit hash %an – Author name %ae – Author email %ad – Author date %cn – Commit name %ce – Commit email %cd – Commit date %s - Subject
  17. ASCII Graph git log –pretty=oneline --graph Kapil Sharma PHP REBOOT

    19  Show ASCII graph of Branches for quick visualization.  Example $ git log --pretty=format:"%h - %s" --graph * 7ee5e22 - Magazie page + few minor changes in menu and footer * d7ac824 - Merge branch 'master' of into f_v2.0 |\ | * 3d23b88 - Update readme.md * | 3c05afe - home page finished * | 7e9bcdd - Changed footer and slide show * | afc3f8d - Deleted old template files |/ * 3947488 - Analytics * f80400c - addming folder as server is not supporting www
  18. Other quick log options git log --since="3 Months“ –until="3 days"

    Kapil Sharma PHP REBOOT 20  Logs within period  Logs of specific file/folder git log <file-or-folder-name-with-path> There are many other log option and filters which can be handy in certain conditions. It is not possible/practical to discuss all of them here. Please visit official Git documentation to go through all possible options.
  19. Stashing There are conditions when you have uncomplete work that

    cant be committed but you have to switch the branch. Stash is very helpful tool in such conditions. Stash take a snapshot of your uncommitted work (except untracked files) and save them. That saved stage can be retrieved later. Kapil Sharma PHP REBOOT 21
  20. Stash current code This command stash all the uncommitted (except

    untracked files) work. Kapil Sharma PHP REBOOT 22 git stash
  21. Get List of current stash Show list of all currently

    stached code. Example Kapil Sharma PHP REBOOT 23 git stash list git stash list stash@{0}: WIP on master: 049d078 added the index file stash@{1}: WIP on master: c264051 Revert "added file_size" stash@{2}: WIP on master: 21d80a5 added number to log
  22. Apply stash Apply last stash (Without staging previously staged files)

    Kapil Sharma PHP REBOOT 24 git stash apply Apply last stash (Also stage previously staged files) git stash apply --index Apply and drop last stash git stash pop
  23. Versioning Before moving to Git workflow, lets look standard way

    of assigning versions to our project/product. We will need it to understand gitflow workflow example. A good way of versioning is ‘Semantic Versioning Specification’, authored by Tom Preston-Werner, Cofounder of GitHub. These specification can be found at http://semver.org Kapil Sharma PHP REBOOT 26
  24. Semantic Versioning Here Major, Minor and Patch are incremented positive

    integers. MAJOR version when you make incompatible API changes. MINOR version when you add functionality in a backwards-compatible manner. PATCH version when you make backwards-compatible bug fixes. Kapil Sharma PHP REBOOT 27 MAJOR.MINOR.PATCH Please visit semver.org for full specifications.
  25. Git Workflows Workflow defines strategy to manage code on Git.

    There are many possible workflows, to suite style of your team. If a wrong workflow selected, you will not be taking full benefit of power provided by Git. We will be taking example of ‘Gitflow workflow’, which is standard among many organization. It is not too complicated for small/single person team or powerful enough for large teams. Kapil Sharma PHP REBOOT 28
  26. Gitflow Workflow The Gitflow Workflow is well defined by Vincent

    Driessen on blog post nvie.com/posts/a-successful-git-branching-model The Gitflow Workflow defines a strict branching model designed around the project release and good for big teams working on large projects. However it can be easily used for small teams. It do not add any new command but just tell how git branches should be used. Kapil Sharma PHP REBOOT 29
  27. Gitflow Workflow 1 Existing project should be committed on master

    as v1.0 New project can be committed as v0.1.0 Kapil Sharma PHP REBOOT 30
  28. Gitflow Workflow 3 Create feature branch. It will be used

    to develop any new feature. Each feature branch have common naming convention git checkout –b feature_m1 Kapil Sharma PHP REBOOT 32
  29. Gitflow Workflow 5 Once developed, merge back to develop git

    push origin feature_m1 git checkout develop git pull origin develop git merge feature_m1 git push origin develop Kapil Sharma PHP REBOOT 34
  30. Gitflow Workflow 6 As soon as developers get free from

    milestone 1, they start work on milestone 2 git checkout develop Git checkout –b feature_m2 Kapil Sharma PHP REBOOT 35
  31. Gitflow Workflow 7 Code is pushed to release branch for

    testing git checkout release git pull origin release git merge develop git push origin release Kapil Sharma PHP REBOOT 36
  32. Gitflow Workflow 8 While testing milestone 1, developers continue to

    work on milestone 2. Kapil Sharma PHP REBOOT 37
  33. Gitflow Workflow 9 Code is release to test server. Any

    issue found during testing will be fixed on release branch. Kapil Sharma PHP REBOOT 38
  34. Gitflow Workflow 10 While milestone 1 bugfix, most team members

    are still working on milestone 2 Kapil Sharma PHP REBOOT 39
  35. Gitflow Workflow 11 Once milestone 1 is QA approved, it

    is pushed to master branch for production release. git push origin release Git checkout master git push origin master On Server git pull origin master Kapil Sharma PHP REBOOT 40
  36. Gitflow Workflow 12 Release branch is also merged with develop

    branch after production release. Kapil Sharma PHP REBOOT 41
  37. Gitflow Workflow 13 Obviously milestone 2 development is not impacted

    by release process. Kapil Sharma PHP REBOOT 42
  38. Gitflow Workflow 14 develop branch is merged to feature_m2 to

    merge m1 release bugfix. Kapil Sharma PHP REBOOT 43
  39. Gitflow Workflow 15 & 16 Now suppose we get any

    issue detected on production server. Simply create a hotfix branch from master. 17 Feature_m2 not impacted Kapil Sharma PHP REBOOT 44
  40. Gitflow Workflow 18 Merge hotfix on master. (If time permit,

    through release branch) 19 & 20 Also merge hotfix to develop and feature branch 21 Feature_m2 development still not impacted. Kapil Sharma PHP REBOOT 45
  41. Gitflow Workflow 22. Once feature_m2 development finishes, merge it to

    release branch (through develop) 23 bugfix 24 & 25 Release and back to develop branch. Kapil Sharma PHP REBOOT 46
  42. Workshop time Lets have short break and practice all these

    commands during workshop Workshop prerequisite Laptop (Fully charged) Internet dongles. If not present, wifi details will shared. Kapil Sharma PHP REBOOT 47