$ git init
or
$ git clone https://github.com/SomeProject
a .git folder is created
a master branch is created
The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.Can I change my mind about adding git to a folder?
Yes! Just delete the .git folder. You'll keep all the other files in your project folder, but remember nothing will be tracked (under version control).
3 main parts to every git project:
What's the difference?
INSERT IMAGE
$ git add README.md $ git commit -m "Your Commit Message"
What's really happening with these commands?
INSERT IMAGE & show transitions
File "snapshots" stored as "hashes"
What is a hash?
Hashes are an important way to reference changes in your repo
$ git checkout -b [NEW_BRANCH_NAME]
What's really happening with this command?
INSERT IMAGE of git log & show addition of HEAD pointer
How can I tell git to not track certain files
show .gitignore
What if I accidentally added too many files to staging?
$ git reset HEAD [file]
What is this command really doing?
What if I only want to commit part of a file?
$ git add --patch [file]
INSERT IMAGE
I want to fix my last commit message
$ git commit --amend
I forgot a file in my last commit
$ git add [file] $ git commit -c ORIG_HEAD
I want a do-over on my last commit
$ git reset --soft HEAD~1 $ [make changes to your files] $ git add [files] $ git commit -c ORIG_HEAD
I change my mind - I don't want my last commit at all
$ git reset --hard HEAD~1
My directory is a mess - I want to start over!
No problem, we can always reset git to a previous state
How far back do you want to go?
$ git reflog $ git reset --hardhttp://stackoverflow.com/questions/17857723/whats-the-difference-between-git-reflog-and-log
$ git log
or
$ git log --oneline --decorate
INSERT IMAGE showing hash numbers, as well as HEAD and branch name
$ gitg
or
$ gitk
Other common things you might do with branches
$ git branch -m [new_branch_name]
or
$ git branch -d [branch_name]
I accidentally deleted a branch - how do I get it back?
If you just deleted it
$ git checkout -b [branch_name] [hash]
If it's been awhile look at the log first
$ git reflog $ git checkout -b [branch_name] [hash]
I started making changes on master, but now I want to move things to a new branch
$ git branch [new_branch] git reset --hard HEAD~3 git checkout [new_branch]Go back 3 commits. You *will* lose uncommitted work on master, but you'll get it on the branch
I want to add a commit from my branch to the master branch
$ git checkout master $ git cherry-pick [branch]~3
$ git push origin master
What's really happening with this commands?
origin = points to a remote site INSERT IMAGE of .git/config file
master = name of branch you want to sync
Team of 3: Alice, Mary, and Paula
Alice & Mary: add features, Paula: maintains repo/project
What scenarios might Alice encounter?
Alice may want to:
$ git cherrypick
Alice may want to combine smaller commits in a single bigger commit
$ git log $ git rebase -i [hash]
INSERT image with pick/squash options
Alice may want to remove a commit from her branch
$ git log $ git rebase -i [hash]
INSERT image with a row deleted
Team of 3: Alice, Mary, and Paula
Alice & Mary: add features, Paula: maintains repo/project
What scenarios might Paula encounter?
Paula might have been in the middle of working on the master branch and wants to save the files she's working on before she starts working on merging Alice's changes
$ git stash $ git stash pop $ git stash list $ git stash save $ git stash drop stash@{1} $ git stash clear
$ git merge
Git tries very hard to automatically merge files but sometimes there are conflicts that it doesn't know how to resolve
Git marks each conflict, which can be manually edited
INSERT IMAGE
Often easier to do this using a GUI
$ git mergetool
3-way merging: LOCAL, REMOTE, BASE, what do these mean?
INSERT IMAGE of tool (meld?) and show GUI example
Team of 3: Alice, Mary, and Paula
Alice & Mary: add features, Paula: maintains repo/project
What scenarios might Mary encounter?
Mary should routinely make sure she's working off the latest version of the project
$ git rebase master
difference between rebase and merge
what is rebasing really doing?
deal with it similar to merge conflicts
$ git push -f
we've covered most of these!