Level 1 Git Basic
# get help $ git help config # git config $ git config --global user.name "Gregg Pollack" $ git config --global user.email gregg@codeschool.com $ git config --global color.ui true # Start a repo $ mkdir store $ cd store $ git init Initialized empty Git repository in /Users/gregg/store/.git/ # status $ git status # git add files $ git add --all # commit $ git commit -m "Create a README." # log $ git log
Level 2 Staging and remote
# show the unstaged differences since last commit $ git diff # show the staged differences $ git diff --staged # HEAD refers to last commit. remove the staged content to unstaged. $ git reset HEAD LICENSE $ git status # Blow away all changes since last commit $ git checkout -- LICENSE $ git status # add and commit the modified content. But does not add new files, only add tracked content. $ git commit -a -m "Modify readme" # Reset into staging and Move to commit before 'HEAD'. # undo last commit and put changes into staging $ git reset --soft HEAD^ $ git status $ git commit --amend -m "New Message" # Change the last commit $ git reset --hard HEAD^ # Undo last commit and all changes $ git reset --hard HEAD^^ # Undo last 2 commit and all changes $ git remote add origin https://github.com/Gregg/git-real.git $ git remote -v $ git push -u origin master $ git pull $ heroku create $ git remote -v $ git push heroku master
Level 3 Cloning and Branching
#1 - Downloads the entire repository into a new git-real directory. master #2 - Adds the ‘origin’ remote, pointing it to the clone URL. #3 - Checks out initial branch (likely master). $ git clone https://github.com/codeschool/git-real.git $ git clone https://github.com/codeschool/git-real.git git-demo # create a branch $ git branch cat # check branch $ git branch # switch to a new branch $ git checkout cat $ git checkout master $ git merge cat # Deleted branch cat $ git branch -d cat # creates and checks out branch $ git checkout -b admin
Level 4 Collaboration Basics
$ git push # push the changes to remote server # git push may generate error. git pull is used to pull down the code and push it up again. # 1. sync local repository with the remote repository. (git fetch) fetch the master in the remote server to origin/master branch in local server. # 2. merge the origin/master with master branch. $ git pull $ git push # git pull may fail with conflicting files. $ git status $ git commit -a
Level 5 Remote Branches & Tags
# When you need someone to work on your branch. $ git checkout -b shopping_cart $ git push origin shopping_cart $ git add cart.rb $ git commit -a -m "Add basic cart ability." $ git push $ git pull $ git branch * master $ git branch -r #check remote branches origin/master origin/shopping_cart $ git checkout shopping_cart #check the remote branch $ git branch master * shopping_cart # check remote branch $ git remote show origin # Delete remote branches $ git push origin :shopping_cart To https://github.com/codeschool/git-real.git - [deleted] shopping_cart $ git branch -d shopping_cart error: The branch 'shopping_cart' is not fully merged. If you are sure you want to delete it, run 'git branch -D shopping_cart'. git branch -D shopping_cart Deleted branch shopping_cart (was ea0a1b9). $ git commit -m -a "Add ability to pay." [shopping_cart 9851887] Add ability to pay. 1 file changed, 1 insertion(+), 1 deletion(-) $ git push Everything up-to-date $ git remote show origin Gregg Remote branches: master tracked refs/remotes/origin/shopping_cart stale (use 'git remote prune' to remove) $ git remote prune origin Pruning origin URL: https://github.com/codeschool/git-real.git * [pruned] origin/shopping_cart # Heroku deploys only master branch Would not work, would push to staging heroku-staging $ git branch * staging master $ git push heroku-staging staging:master # A tag is a reference to a commit (used mostly for release versioning) $ git tag # check tags $ git checkout v0.0.1 # check out code at commit $ git tag -a v0.0.3 -m "version 0.0.3" # To add a new tag $ git push --tags #To push new tags
Level 6 Rebase Belongs to us
# Merge commits are bad. $ git fetch # 1. Move all changes to master which are not in origin/master to a temporary area. # 2. Run all origin/master commits. # 3. Run all commits in the temporary area, one at a time. $ git rebase $ git checkout admin $ git rebase master # if all goes well, then merge them together. $ git checkout master $ git merge admin
Level 7 History and Configuration
$ git log # Colorizing the log $ git config --global color.ui true $ git log $ git log --pretty=oneline $ git log 08f202691c67abd12eb886b587ac7b26d51005c7 Update index $ git log --pretty=format:"%h %ad- %s [%an]" #patch $ git log --oneline -p # Stats $ git log --oneline --stat # graph $ git log --oneline --graph # Date ranges $ git log --until=1.minute.ago $ git log --since=1.day.ago #since (days) $ git log --since=1.hour.ago #since (hours) $ git log --since=1.month.ago --until=2.weeks.ago # since & until (relative) $ git log --since=2000-01-01 --until=2012-12-21 # since & until (absolute) $ git diff $ git diff HEAD # difference between the last commit and current state. $ git diff HEAD^ #parent of latest commit $ git diff HEAD^^ #grandparent of latest commit $ git diff HEAD~5 #five commits ago $ git diff HEAD^..HEAD #second most recent commit vs. most recent $ git diff f5a6sdfsfsdfsdfff9..4sdsdfsdfsdfsdffb063f # range of SHAs $ git diff master bird # branch difference $ git diff --since=1.week.ago --until=1.minute.ago # time-based diff $ git blame index.html --date short $ git config --global alias.st status #git st $ git st $ git config --global alias.co checkout #git co $ git config --global alias.br branch #git br $ git config --global alias.ci commit #git ci