Some Definitions
Repo(sitory)
A collection of code
Branch
Different version of the same repository
Remote
Url where collection is hosted
Fork
Copy of repository to another repository owned by the forker ™
Clone
Local copy of a repository
Collaborator
Someone contributing to a project
Organization
Provides a common place for projects that have more than one collaborator
How People Use Git Differently
Centralized Workflow
- All members share one repo
- All members push all the changes to that repo
- Code changes are taken as merges on the master or someone else's code base onto master
- The last one tends to cause confusing merge conflicts
Feature Branch
- Each new feature gets it's own branch while it is being worked on
- A single feature should have a single owner
- When a feature is completed it is merged into the master branch
Gitflow Workflow
- Doesn't add any new concepts or commands beyond what is used in Feature Branch
- Assigns very specific roles to different branches and defines how and when they should interact
Forking and Branching
- All contributors maintain their own fork of the orginization's repository
- Each contributor maintains their own repo
- Within each contribor's repo they create their own feature branches
- When feature is completed it is merged into the orginization's repo by submitting a pull request
Situations and Commands to Help With Them
Starting a Repo
git init # Creates git folder and starts a git history
git add -A # Stages everything in the directory and child directories
git commit -m “first commit” # Commits those changes
git remote add origin https://github.com/imdevan/intro-to-git.git # Tells .git where to store files remotely
git push -u origin master # Push and intiate the master branch remotely
Cloning a Repo
git clone [repo url] # Copy a repository from a remote url
Pushing Changes to a Repo
Pushing to a Repo
git add -A # Stages everything in the directory and child directories
git commit -m “commit message” # Commits those changes
git push # Push changes to remote repo
Pulling Changes From a Remote Repo
git pull # Pull down any changes that were made to a remote branch
Branches, Merging and Rebasing
Dealing with Branches
git branch [branch name] # Creates a new branch (shows branches if no name is provided)
git checkout [branch name] # Switch to branch name provided
git checkout -b [new branch name] # Creates a new branch and switces to it
Merging
git checkout [feature branch] # Switch to branch
git merge master # Merge master into branch
or
git merge master [feature branch] # Merge feature branch into master
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO NOT MERGE INTO PRODUCTION
DO A PULL REQUEST
Rebasing
git checkout [feature branch] # Switch to branch
git rebase [remote]/[branch] # Put your changes on top of remote branch
git rebase [remote]/[branch] -i # Use this to squash multilple commits into one
Pull Requests and Undoing Changes
Submitting a Pull Request
- No terminal neaded for this one
Reviewing a Pull Request
Makes local branch of pull request for testing
git fetch upstream pull/[pull request number]/head:[branch name] # Get pull request
git checkout [branch name] # Switch to branch
(ie git fetch upstream pull/1/head:test)
Stash
git stash # hide last changes
git stash pop # brings back changes
git stash clear # clear all stashed changes
git stash list # show all stashed changes
Checking Out Files Again
git checkout -- [file path] # Reverts file to last commit (changes lost)
Reset
git fetch upstream # Bring upstream into local environment
git reset --hard upstream/master # Set local environment to upstream
Go Back to a Previous Commit
git revert [commit] # Go back to commit number
git reset --hard [commit] # Reset back to commit number
Reset
git fetch upstream # Bring upstream into local environment
git reset --hard upstream/master # Set local environment to upstream
git push origin master --force # push changes back up to your fork
Useful (non-command) Things
.gitignore
- Things I don't want to be included into project
- Things that take a lot of space like libraries and frameworks
- Sensitive user information (databases) & api keys
- node_modules/, bower_components/, maven things, etc
Github for Task Management and Notes
Github Shortkeys
- g + i = issues
- g + p = pull requests
- g + n = notifications
Ask for Halp!
- Eventually somone will have the knowledge to help
- @devanIpsum
Cheatsheet
- git init
- git add -A
- git add [file name]
- git commit -m "[commit here]"
- git remote add origin [git repo url]
- git push --set-upstream origin master
- git pull
- git commit -m "your commit message"
- git commit --amend
- git commit --amend --no-edit
- git push
- git push -f
- git fetch [remote] [branch optional]
- git merge [branch name]
- git commit --amend
- git commit --amend --no-edit
- git push
- git push -f
- git fetch [remote] [branch optional]
- git merge [branch]
- git rebase [branch]
- git rebase --abort
- git rebase --continue
- git mergetool
- git rm [file name]
- git diff --name-only --div-filter=U
- git reset --hard HEAD
- git checkout HEAD [file]
- git reset --hard [commit]
- git stash
- git blame [file]
- git log -1
- git branch
- git branch [new branch name]
- git branch -D [branch name]
- git checkout [branch name]
- git checkout -b [new branch name]
- git fetch upstream && git reset --hard upstream/master && git push origin master --force
Git and Your Terminal
What Are Hopefully Useful Pointers; If Not A Decent Review
Created by Devan Huapaya / @devanIpsum