Wednesday, 9 December 2015

git

1. vocabulary
  • working directory/staging area/repository
  • origin: when you clone another repository, git automatically creates a remote named "origin" and points to it
  • master: the main branch

2. git config
  • /etc/gitconfig, ~/.gitconfig, .git/config, one override another
  • git config --list
  • .gitignore specify files not committed to repo, for eclipse project, can be target/, .classpath, .project, .settings/

3. common git command
  • git init
  • git clone
  • git config 
  • git checkout -b new_branch             //create new branch and checkout 
  • git checkout -track -b origin/daves_branch           //create local branch to track remote
  • git commit -m 'initial commit'
  • git add --all
  • git commit --amend                          //combine commit and change comment
  • git reset HEAD
  • git reset --soft HEAD^
  • git reset --soft HEAD~1
  • git reset -- hard HEAD^ 
  • git clean -f -d                                    //remove permanently untracked directory and file
  • git reset                                             //unstage all 
  • git revert <commit>                         //undo a commit in the history
  • git rebase -i HEAD~2                      //combine 2 commits into 1
  • git checkout <feature_branch>
  • git merge <master_branch>            //3-way merge, create new commit C
          A---B feature
         /
   D---E---F---G master
 
          A---B---C feature
         /       /
    D---E---F---G  master 
 
          A---B feature          //fast forward
         /
   D---E- master 
 
    D---E---A---B  feature(master) 
 

  • git checkout <feature_branch>       // rebase move the STARTING POINT of  'feature' on
  • git rebase <master_branch>           // master, destroy A, B, C and create new commits A' B' C'          
          A---B---C feature
         /
   D---E---F---G master
 
                  A'--B'--C' feature
                 /
   D---E---F---G master
  • git remote add <name> <url> 
  • git remote -v
  • git fetch <remote> [branch]
  • git branch -r                                     //display remote branch
  • git branch
  • git pull = git fetch + git merge
  • git pull --rebase (by default it will perform merge, can force it to rebase) 
  • git push origin master
  • git push origin master -f           //overwrite remote branch
  • git log
  • git status
  • git diff
  • git rm
  • git mv
  • git reset --hard origin/master    //clean everything in repo

4. merge vs rebase
  • merging is always safe
  • rebasing is not (Golden Rule of Rebasing - never rebase public branches like 'master' to 'feature)

5. centralized work flow
  • single central repositary
  • work on local master branch
  • git clone
  • git pull --rebase origin master
  • git rebase -- continue
  • git push origin master

6. feature branch flow
  • work on local feature branch
  • git checkout -b feature_branch master
  • git push -u origin feature_branch
  • submit pull request in github or bitbucket (code review) 
  • git checkout master
  • git pull
  • git pull origin feature_branch
  • git push

7. gitflow workflow
  • master(release) branch and dev branch

8. stash(bitbucket) and github


9. local vs remote branch
  • always: work on local branch, rebase on remote branch, push to remote branch
  • local branch can track remote branch
  • after clone, local 'master' will track 'origin/master'
  • 'git fetch, git checkout remote-branch-name' will create local branch that track remote branch 'original/remote-branch-name', just like 'master'
  • or use 'git fetch, git checkout -b remote-branch-name origin/remote-branch-name'

10. clone with/without account
  • git clone git://github.com/SomeUser/SomeRepo.git    //without account
  • git clone git@github.com:UserName/OtherRepo.git

11. submodule
  • .gitmodules                                //define structure
  • git submodule init
  • git submodule update

12. add reviewer to github

No comments:

Post a Comment