Dec
02

My Git learning notes

Git Basic

  • Git Help
    • $ git help
    • $ git help config
  • Setting up git
    • $ git config –global user.name “xxx”
    • $ git config –global user.email “xxx@xxx.com”
    • $ git config –global color.ui true
  • Starting a Repo
    • $ git init
    • Work flow:
      • create file aaa.txt
      • add aaa.txt to staging area
      • commit changes
      • modify aaa.txt
      • add aaa.txt to staging area
      • commit changes
    • $ git status : watch the status
    • $ git add aaa.txt : add the modified file into staging area
    • $ git commit -m “hahaha!”
    • $ git add aaa.txt bbb.txt : add two files to staging area
      $ git add –all
      $ git add *.txt : add all txt files in current directory
      $ git add docs/*.txt
      $ git add docs/
      $ git add “*.txt” : add all txt files in the whole project
    • $git commit -m “hahahaha!”
  • Timeline History
    • $ git log

Staging & Remotes

  • Viewing staged differences
    • $ git add aaa.txt
    • $ git diff
    • $ git diff –staged : View staged differences
  • Unstaging files
    • $ git status
      $ git reset HEAD xxx.txt
  • Discard Changes
    • $ git status
      $ git checkout — xxx.txt : Blow away all changes
  • Skip staging and commit
    • $ git commit -a -m “hahaha” : add changes from all tracked files
  • Undoing a Commit
    • $ git reset –soft HEAD^ : Undo last commit, put changes into staging
    • $ git reset –hard HEAD^ Undo last commit and all changes
    • $ git reset –hard HEAD^^ Undo last 2 commits and all changes
  • Adding to a commt
    • $ git commit –amend -m “jajaja…”
  • Git Remote
    • $ git remote
    • $ git remote -v
    • $ git remote add origin https://….
  • Pushing to Remote
    • $ git push -u origin master
  • Pulling from Remote
    • $ git pull: pull = fetch + merge
  • Working with the remotes
    • $ git remote add <name> <address>
    • $ git remote rm <name>
    • $ git push -u <name> <branch>

Cloning & Branching

  • Cloning repo
    • $ git clone <adress>
    • $ git clone <adress> <my folder>
  • Branching out
    • $ git branch miaomaio : create branch
    • $ git branch
  • Switching to a Branch
    • $ git checkout miaomiao
    • $ git add mimi.txt
    • $ git commit -m “hehe”
  • Merge a branch
    • $ git checkout master
    • $ git log
    • $ git merge miaomaio
  • Delete a Branch
    • $ git branch -d miaomiao
  • Create and  checks out a branch
    • $ git checkout -b ruru
  • Fast-forward merging and recursive merging

Collaboration

  • different git commits
    • $ git pull
    • $ git push
    • $ git log
    • $ git merge

Remote branch

create remote branch

  • git checkout -b mybranch
  • git push origin mybranch

pushing to the banch

  • git add myfile
  • git commit -a -m “push file to my branch”

pulling new branches

  • git pull
  • git branch
  • git branch -r
  • git  checkout shopping_cart
  • git branch

Remote show

  • git remote show origin

removing a branch

  • git push origin :shopping_cart

On deleted remote branch

  • git remote prune origin

Remote branch names

  • git push heroku-staging staging:master

Tagging

  • git tag : list all tags
  • git checkout v0.0.1
  • add new tag
    • git tag -a v0.0.3 -m “version 0.0.3”
  • to push new tags
    • git push –tags

Rebase

git fetch

git rebase

git rebase –continue

git rebase –skip

git rebase –abort

Local branch rebase

  • git checkout admin
  • git rebase master
  • if all goes well, merge master
    • git checkout master
    • git merge admin

Conflict

  • git fetch
  • git rebase

 

History & Configuration

Viewing the log

  • git log

Colorizing the log

  • git config –global color.ui true
  • git log
  • git log –pretty=oneline

Log format

  • git log –pretty=format:”%h %ad- %s [%an]”
    • %h sha hash
    • %ad author date
    • %an auther name
    • %s subject
    • %d ref names
  • git help config: for more inoformation

patch

  • git log –oneline -p

stats

  • git log –oneline –stat
  • git log –oneline –graph

data ranges

  • git log –until=1.minute.ago
  • git log –since=1.day.ago
  • git log –since=1.hour.ago
  • git log –since=1.month.ago –until=2.weeks.ago
  • git log –since=2000-01-01 –until=2012.12-21

Diffs

  • git diff = git diff HEAD
  • git diff HEAD^
  • git diff HEAD^^
  • git diff HEAD~5
  • git diff HEAD^..HEAD

Earlier commits

  • git diff f5a6sdfsfsdfsdfff9..4sdsdfsdfsdfsdffb063f
  • git diff 4fb063f..f5a6ff9
  • git diff master bird
  • git diff –since=1.week.ago –until 1.minute.ago

Blame

  • git blame index.htm –date short : list a history of a file

Excluding files

  • got the the directory:
    .git/info/exclude
  • put the folder unter the exclude directory

Exclude patterns

  • tutorial.mp4
  • *.mp4
  • experiments/
  • logs/*.log

Excluding from all copies

  • in File: .gitignore
    logs/*.log

Removing files

  • git rm README.txt: Deteled from the local filesystem & untracked
  • git commit -m “Remove readme”

Untracking files

  • git rm –cached development.log
  • In file: .gitignore
    logs/*.log
  • git add .gitignore
  • git commit -m “Ignore all log files.”

Config

  • git config –global user.name “Gregg Pollack”
  • git config –global user.email “gregg@codeschool.com”
  • git config –global core.editor emacs
  • git config –global merge.tool opendiff

Local config

  • git config user.email “spamme@example.com”
  • git config –list
    show thd config list
  • git config user.email

Aliases

  • git config –global alias.mylog “log –pretty=format:’%h %s [%an]’ –graph”
    • git mylog
  • git config –global alias.lol “log –graph –decorate –pretty=oneline –abbrev-commit –all
    • git lol
  • git config –global alias.st status
  • git config –global alias.co checkout
  • git config –global alias.br branch
  • git config –global alias.ci commit
    • git st

 

Written by connygy. Posted in git, Tools

Tags:

Trackback from your site.

Leave a comment