Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Git and Version Control at Atlogys

123 views

Published on

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

This Tech Talk covers basic, intermediate and some advanced concepts of Git. Basic Git includes about types of version control system, three states of git, getting git repositories, recording changes, viewing staged and unstaged changes, committing changes, viewing commit history, working with remotes and Tagging.

Git's Killer Feature Branching has been discussed in detail; about branches in git, creating branches, switching branches, merging branches, rebasing, resolving merge conflicts and remote branches.

Other than that some useful features like Staging patches, stashing and cleaning, cherry-pick, git reset, git revert, interactive rebase and undoing merges have been discussed.

This informative tech talk was given at Atlogys by Tech Lead - Mr. Anoop Malav. It is also available on the Atlogys YouTube channel.

Published in: Technology
  • Earn Up To $316/day! Easy Writing Jobs from the comfort of home! ♥♥♥ http://t.cn/AieXS5j0
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Git and Version Control at Atlogys

  1. 1. Version Control using Git Anoop Kumar Malav Atlogys Technical Consulting anoop@atlogys.com February 8, 2018 Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 1 / 65
  2. 2. Git: Original Author Linus Torvalds https://en.wikipedia.org/wiki/Linus_Torvalds Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 2 / 65
  3. 3. Overview 1 About Version Control 2 Git Basics The Three States Getting a Git Repository Recording Changes to the Repository Viewing Your Staged and Unstaged Changes Committing Your Changes Undoing Things Working with Remotes Tagging 3 Git Branching Introduction Branch Merging Remote Branches 4 Git Tools Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 3 / 65
  4. 4. About Version Control Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 4 / 65
  5. 5. About Version Control: Local VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 5 / 65
  6. 6. About Version Control: Centralized VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 6 / 65
  7. 7. About Version Control: Distributed VCS Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 7 / 65
  8. 8. Git Basics: The Three States Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 8 / 65
  9. 9. Git Basics: Getting a Git Repository You typically obtain a Git repository in one of two ways: You can take a local directory that is currently not under version control, and turn it into a Git repository, or You can clone an existing Git repository from elsewhere. In either case, you end up with a Git repository on your local machine, ready for work. Example (Initializing a Repository in an Existing Directory) $ cd /home/user/my_project $ git init $ git add *.c; $ git add LICENSE $ git commit -m ’initial project version’ Example (Cloning an Existing Repository) $ git clone https://github.com/libgit2/libgit2 mylibgit Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 9 / 65
  10. 10. Git Basics: Getting a Git Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 10 / 65
  11. 11. Git Basics: Getting a Git Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 11 / 65
  12. 12. Git Basics: Recording Changes to the Repository The lifecycle of the status of your files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 12 / 65
  13. 13. Git Basics: Recording Changes to the Repository Checking the Status of Your Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 13 / 65
  14. 14. Git Basics: Recording Changes to the Repository Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 14 / 65
  15. 15. Git Basics: Recording Changes to the Repository Tracking New Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 15 / 65
  16. 16. Git Basics: Recording Changes to the Repository Staging Modified Files - Modify previously tracked file called CONTRIBUTING.md Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 16 / 65
  17. 17. Git Basics: Recording Changes to the Repository Staging Modified Files Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 17 / 65
  18. 18. Ignoring Files → Class of files that you don’t want Git to automatically add or even show you as being untracked. → Setting up a .gitignore file for your new repository before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository. → In the simple case, a repository might have a single .gitignore file in its root directory, which applies recursively to the entire repository. → However, it is also possible to have additional .gitignore files in subdirectories. The rules for the patterns you can put in the .gitignore file are as follows: Blank lines or lines starting with # are ignored. Standard glob patterns work, and will be applied recursively throughout the entire working tree. You can start patterns with a forward slash (/) to avoid recursivity. You can end patterns with a forward slash (/) to specify a directory. You can negate a pattern by starting it with an exclamation point (!). Note: GitHub maintains a fairly comprehensive list of good .gitignore file examples for dozens of projects and languages at https://github.com/github/gitignore Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 18 / 65
  19. 19. Viewing Your Staged and Unstaged Changes Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 19 / 65
  20. 20. Viewing Your Staged and Unstaged Changes diff options –staged and –cached are synonyms. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 20 / 65
  21. 21. Committing Your Changes - The simplest way to commit is to type git commit. Doing so launches your editor of choice. - Alternatively, you can type your commit message inline with the commit command by specifying it after a -m flag, like this: $ git commit -m "Story 182: Fix benchmarks for speed" - If you want to skip the staging area, Git provides a simple shortcut. - Adding the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part: $ git commit -a -m ’added new benchmarks’ Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 21 / 65
  22. 22. Removing Files - To remove a file from Git, you have to remove it from your tracked files and then commit. - The git rm command does that, and also removes the file from your working directory so you don’t see it as an untracked file the next time around. - To keep the file in your working tree but remove it from your staging area, use the –cached option: $ git rm –cached README - This is particularly useful if you forgot to add something to your .gitignore file and accidentally staged it. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 22 / 65
  23. 23. View Commit History The most basic and powerful tool to do this is the git log command. git log branchname git log filename git log -n git log -p git log –author git log –grep=pattern Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 23 / 65
  24. 24. View Commit History git log –all –pretty=oneline –graph –abbrev-commit –decorate Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 24 / 65
  25. 25. View Commit History There are many graphical interface to view commit history. One of these is gitk. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 25 / 65
  26. 26. Git Basics: Undoing Things Commit Amend (Redo commit) One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to redo that commit, make the additional changes you forgot, stage them, and commit again using the –amend option: $ git commit –amend It’s important to understand that when you’re amending your last commit, you’re not so much fixing it as replacing it entirely with a new, improved commit that pushes the old commit out of the way and puts the new commit in its place. Effectively, it’s as if the previous commit never happened, and it won’t show up in your repository history. Do not amend commit if you have already pushed your code on remote. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 26 / 65
  27. 27. Git Basics: Undoing Things Unstaging a Staged File Let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type git add * and stage them both. How can you unstage one of the two? The git status command reminds you: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 27 / 65
  28. 28. Git Basics: Undoing Things Unmodifying a Modified File Let’s say you don’t want to keep your changes to the CONTRIBUTING.md file. It’s important to understand that git checkout – <file>is a dangerous command. Any changes you made to that file are gone. git checkout command is used in two context. The first is to revert changes as in above. But git checkout is also used to switch to a branch. To avoid ambiguity, it is best practice to put in the double dash to indicate that we are not switching branch and that we want to stay on current branch. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 28 / 65
  29. 29. Git Basics: Working with Remotes To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Showing Your Remotes Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 29 / 65
  30. 30. Git Basics: Working with Remotes Adding Remote Repositories git remote add <shortname><url> Fetching and Pulling from Your Remotes Pushing to Your Remotes git push <remote><branch> Renaming and Removing Remotes git remote rename <oldname><newname> git remote remove <shortname> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 30 / 65
  31. 31. Git Basics: Tagging → Git has the ability to tag specific points in history as being important. → Typically people use this functionality to mark release points. Creating Tags Git supports two types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a specific commit. git tag <tagname> Annotated tags, however, are stored as full objects in the Git database. They contain the tagger name, email, and date; have a tagging message. git tag -a <tagname>-m <message> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 31 / 65
  32. 32. Git Basics: Tagging Listing Your Tags git tag Sharing Tags git push origin <tagname> If you have a lot of tags that you want to push up at once, you can also use the –tags option to the git push command. git push origin –tags Checking out Tags If you want to view the versions of files a tag is pointing to, you can do a git checkout, though this puts your repository in “detached HEAD“ state. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 32 / 65
  33. 33. Git Branching: Killer Feature The way Git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast. → A branch in Git is simply a lightweight movable pointer to one of the commits. → The default branch name in Git is master. → As you start making commits, you’re given a master branch that points to the last commit you made. → Every time you commit, it moves forward automatically. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 33 / 65
  34. 34. Git Branching: Killer Feature How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 34 / 65
  35. 35. Git Branching Creating a New Branch git branch <branchname> git checkout -b <branchname> Switching Branches git checkout <branchname> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 35 / 65
  36. 36. Branch Merging: Fast Forwarding Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 36 / 65
  37. 37. Branch Merging: Fast Forwarding Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 37 / 65
  38. 38. Branch Merging: Merge Commit git merge <branchtomerge> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 38 / 65
  39. 39. Branch Merging: Merge Commit When merging a Branch, make sure your working directory is clean. There is a chance to loosing your uncommited work. In case of merge conflict people make mistake to commit changes which are not present in either of the branch. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 39 / 65
  40. 40. Branch Merging: Merge Conflict Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 40 / 65
  41. 41. Branch Merging: Merge Conflict Resolving conflict using git git checkout –ours <filename> git checkout –theirs <filename> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 41 / 65
  42. 42. Branch Merging: Rebase then Fast Forward git rebase <onbranch> Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 42 / 65
  43. 43. Branch Merging: Rebase then Fast Forward git checkout master; git merge experiment git checkout experiment; git rebase master Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 43 / 65
  44. 44. Branch Merging: Rebase then Fast Forward git checkout experiment; git rebase master git checkout master; git merge experiment Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 44 / 65
  45. 45. Branch Merging: Rebase vs. Merge The Perils of Rebasing Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be summed up in a single line: Do not rebase commits that exist outside your repository. If you follow that guideline, you’ll be fine. In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere. Rule of thumb: When pulling changes from origin/develop onto your local develop use rebase. (git pull –rebase origin develop) When finishing a feature branch merge the changes back to develop. (git merge –no-ff feature) Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 45 / 65
  46. 46. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 46 / 65
  47. 47. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 47 / 65
  48. 48. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 48 / 65
  49. 49. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 49 / 65
  50. 50. Git Branching: Remote Branches Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 50 / 65
  51. 51. Staging Patches It’s also possible for Git to stage certain parts of files and not the rest. For example, if you make two changes to your simplegit.rb file and want to stage one of them and not the other, doing so is very easy in Git. git add -p -A You have a lot of options at this point. Typing ? shows a list of what you can do: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 51 / 65
  52. 52. Staging Patches Generally, you’ll type y or n if you want to stage each hunk, but staging all of them in certain files or skipping a hunk decision until later can be helpful too. Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 52 / 65
  53. 53. Git Stashing and Cleaning Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. git stash Apply Stash git stash apply Apply and Drop Stash git stash pop Cleaning your Working Directory Dry Run git clean -d -X -n Cleanup git clean -d -X -f Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 53 / 65
  54. 54. Cherry-Pick $ git cherry-pick e43a6 Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 54 / 65
  55. 55. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 55 / 65
  56. 56. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 56 / 65
  57. 57. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 57 / 65
  58. 58. Git Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 58 / 65
  59. 59. Git Revert $ git revert 8d78c0f Given one or more existing commits, reverts the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit). Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 59 / 65
  60. 60. Interactive Rebase Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 60 / 65
  61. 61. Undoing Merges: Reset Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 61 / 65
  62. 62. Undoing Merges: Revert Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 62 / 65
  63. 63. Debugging with Git: File Annotation If you track down a bug in your code and want to know when it was introduced and why, file annotation is often your best tool. It shows you what commit was the last to modify each line of any file. The following example uses git blame to determine which commit and committer was responsible for lines in the top-level Linux kernel Makefile and, further, uses the -L option to restrict the output of the annotation to lines 69 through 82 of that file: Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 63 / 65
  64. 64. Useful Resources Documentation Book https://git-scm.com/book/en/v2/ GitHub Cheat Sheet https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf Visual Git Cheat Sheet http://ndpsoftware.com/git-cheatsheet.html Branching Workflows https://www.atlassian.com/git/tutorials/comparing-workflows Best Practices 1 https://www.git-tower.com/blog/version-control-best-practices/ 2 https://www.lullabot.com/articles/git-best-practices-workflow-guidelines 3 https://blog.hartleybrody.com/git-small-teams/ 4 https://sethrobertson.github.io/GitBestPractices/ Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 64 / 65
  65. 65. Thanks for your attention! Any questions? Hope you slept comfortably! Anoop Kumar Malav (Atlogys) Git Tech Talk February 8, 2018 65 / 65

×