Practical Git gitscm com easy to learn book
Practical Git git-scm. com -> easy to learn -> book
What is Git • A Source Code Manager • Tracks a single folder and it’s contents • A database that keeps track of: • Files • Lines within files • (With a little skill / practice) A way to be confident about making changes to your code
Why just Source Code? • Source code has some unique properties • • Files tend to be small (~kb) Changes tend to be small Organized into small lines (~ 80 -100 characters) Code is typically logically organized into files • Consequences: • Bad files for things that don’t follow these conventions (images, compiled executables, csv files, etc)
Installing and config • git-scm. com • Setup requires name and email address
Single Repo Concepts • Commit: Snapshot of the directory • Branch: A label that points to a commit • Tag: A label that points to a commit (forever) Master /. git
Getting Started • New Repo > git init • Existing Repo > git clone Master /. git Your files Files from the remote repo
Preparing to Commit • A commit is a snapshot • Any number of files may be included in the commit • The Staging Area is the list of files to be included in the next commit Master /. git • Add files to the staging area > git add analysis. R • Remove files from the staging area > git reset analysis. R
Committing Changes (Updating the Database) • Creates a new Commit • Moves the current branch pointer to the new commit > git commit –m "New Awesome Analysis" Master /. git
Branching • Creates a new branch label > git branch figure 1 Figure 1 Master /. git
Checking out Code • Updates the file system to reflect the contents of another commit > git checkout figure 1 /. git Figure 1 Master /. git
Checking out Code • Updates the file system to reflect the contents of another commit > git commit –m “my awesome figure” Master Figure 1 /. git
Working with multiple repositories • • • > > A “Remote” is a pointer to another git repository A remote called “origin” is created automatically when cloning another repo Remotes can be created manually too git remote –v git remote add origin https: //github. com/jdthorpe/example-repo Remotes: origin Master /. git
origin Getting code from a remote Master • Fetching gathers commits from the remote databas but leaves your file system unchanged > git fetch origin /. git Master /. git Origin: Master
origin Getting code from a remote Master • Pulling gathers commits from the remote database and merges the current branch with the remote branch – causing the files in your file system to change! > git pull origin master /. git Master /. git
origin Working Together: Merging with another branch Origin: Master /. git Typical Process: • Clone an existing repo • Update some files
origin Working Together: Merging with another branch Origin: Master /. git Typical Process: • Clone an existing repo • Update some files • Stage (some of) them
origin Working Together: Merging with another branch Origin: Master /. git Typical Process: • Clone an existing repo • Update some files • Stage (some of) them • Make the Commit
origin Working Together: Merging with another branch Master /. git Origin: Master /. git Master Typical Process: • Clone an existing repo • Update some files • Stage (some of) them • Make the Commit • Pull updates from the remote repo
origin Working Together: Merging with another branch Master /. git Origin: Master /. git Typical Process: • Clone an existing repo • Update some files • Stage (some of) them • Make the Commit • Pull updates from the remote repo • Merge your work with the remote repo • Handle any merge conflicts (if any) and test the merged code • Commit the merge conflicts (if any)
origin Working Together: Merging with another branch Master /. git Origin: Master /. git Typical Process: • Clone an existing repo • Update some files • Stage (some of) them • Make the Commit • Pull updates from the remote repo • Merge your work with the remote repo • Handle any merge conflicts (if any) and test the merged code • Commit the merge conflicts
How Merging Works • • Git uses an algorithm to determine where edits were made at the file / line level If lines included in a single "edit" don’t overlap within the same file, both edits are kept message = "hello world" favorite_number = 37 message = "hello world" random_number = 37 message = "hi world" favorite_number = 37 message = "hi world" random_number = 37
How Merging Works • • • Git uses an algorithm to determine where edits were made at the file / line level If lines included in a single "edit" don’t overlap within the same file, both edits are kept When edits are overlap, a merge conflict is created message = "hi world" favorite_number = 73 <<<<<<< HEAD random_number = 37 ======= favorite_number = 73 >>>>>>> new_branch_to_merge_later message = "hi world" favorite_number = 37 message = "hi world" random_number = 37
Sending your work Pushing is just like pulling Typically requires a fast-forward merge (just like the Example) • • origin Master /. git > git push origin master Master /. git origin Master /. git
Sending your work Pushing is just like pulling Typically requires a fast-forward merge (just like the Example) • • origin Master /. git > git push origin master Master origin Master /. git
Gotcha: Removing things from a repo • Edit and commit? • Remove the file? > git rm filename. txt > git rm --cached filename. txt origin Master /. git * * *Super Secret Data
Gotcha: Removing things from a repo • Edit and commit? • Remove the file? > git rm filename. txt > git rm --cached filename. txt origin Master /. git *Super Secret Data
Gotcha: Removing things from a repo • Edit and commit? • Remove the file? > git rm filename. txt > git rm --cached filename. txt • Delete the repo and re-initialized the repo origin Master /. git
. gitignore files • . gitignore files contain glob patterns (one per line) • If a file matches any of the patterns, its is not automatically included in the staging area > git add. • “Add All changes” in many GUIs • . gitignore files can be placed anywhere in the repo and affect all nested folders • ~/. gitignore_global affects all repo’s on your machine • Good defaults exist for most languges Gotchas: • Adding a new pattern in a git ignore file *won’t* cause matching files to be removed • Any file can be added explicitly > git add some_ignored_file. txt
Other Topics • Pull Requests • Introduced by Git. Hub • Not a core git feature • “please pull changes in from my repo” • Stashing • Light weight commit • Hooks • Trigger a function after a commit
- Slides: 29