Git Rundown
The Importance of Version Control
What is git?
Git is a distributed version control system.
Distributed:
- Files are stored on multiple machines (i.e. your local machine and github.com)
Many people can work on one project simultaneously
Version control:
- previous versions of the project can be saved and retrieved
Benefits of Distributed Systems
Your code is safely saved in case your computer explodes.
Benefits of Version Control
- You can easily merge two different versions of the same project from two different people
- If you break code, you can always go back to a previous version of the project
Commits
A commit represents the state of your project at the time the commit was made.
Git stores commit by keeping track of changes from one commit to the next.
Visualizing Commits
Because commits are made sequentially and they build off of each other, commits can be visualized as a sequence from one to the next.
Branches
Branches facilitate collaboration.
Side branches will not affect the whole project until they are merged.
Branch often (i.e. for every new feature).
Created a new branch (iss53) to fix issue #53.
Make a commit (C3) while on branch iss53.
An urgent bug was found. Postpone work on issue #53 to do a hotfix.
Merge the hotfix branch with master to put the bug fix into production.
Continue working on issue #53.
Make a new commit (C5) on the iss53 branch
Now we want to merge in the fix for issue #53 into master so that it will appear in production.
The branch iss53 is now merged with master! iss53 can now be deleted.
Git Commands
Configure git on your computer
git config --global user.name "Sam Smith"
git config --global user.email sam@example.com
Create a new git repository in the current directory
git init
Checkout a repository from a remote server
git clone username@host:/path/to/repository
Adding to Commit
Add tracked and untracked file(s) to a commit
git add <filename>
git add * (adds all files/folders in the current directory)
git add :/ (adds all files/folders in the repository)
Remove file(s) from a commit (make them untracked)
git rm <filename>
Committing
Add changes in tracked file(s) to the commit
git commit -a
Add a message to the commit
git commit -m “fixed the bug after days of banging my head against the wall--I will be taking the rest of the day off to visit the hospital”
git commit -am “two-in-one”
Pushing
Push to a remote server
git push
git push <remote> <branch>
git push origin master
Find out what’s going on in your local repo
git status
Branching
Create a new branch
git branch <branch name>
Switch to a branch
git checkout <branch name>
Merge a branch (merge a foreign branch to the local branch you are in)
git merge <foreign branch>