GIT最佳实践
Clone GitHub Repo to local machine
git clone git@github.com:vitrue/sina_weibo.git
Get the latest code of specific branch (such as develop)
1- switch to the branch you want to checkout from
git checkout develop
2- get changes from remote repo
git fetch
git fetch origin remotebranch (only fetch specific branch)
3- update develop branch to the latest code
git merge origin/develop
4- you can do step2 and step3 in one step (not recommend) git pull origin develop ( this develop branch is remote branch)
5- you can also do step 1-3 in one step (not recommend) git pull origin develop:develop (git pull (remote repo) (remote branch name):(local branch name))
Usually we create a new branch based on an existing branch (such as develop)
1- create a new branch based on current branch (such as develop, see above 'Get the latest code of specific branch' )
git checkout develop , then pull/fetch+merge an the latest code
git checkout -b tingting/new_branch
Add new file, then push to remote server
1- make sure you are on right branch, check the git status 2- add file to track a file with git
git add <relative_file_path>
or
git add . (add all changed file, not recommend)
3- commit your changes to git
git commit -m "your comment on this commit"
4- sync to GitHub
git push origin <new_branch>
Rebase, sometimes ,there have confilction, so must do rebase first then merge
1- try to rebase the branch (usually we are on other branch, and we rebase from develop)
git fetch
git checkout tingting/abc
git rebase origin/develop
>May tell you there are some conficts
vi <file_nameto solve conflicts
git add <file_name>
>continue or abort
git rebase --continue
or
git rebase --abort
after rebase finished, push the latest code to github (need force push)
git push origin tingting/abs -f
Delete the branch
git branch -d tingting/new_branch
git branch -D tingting/new_branch (force delete)
Check the status
git status
Check log
git log
git log -1 (check the latest 1 log)
git log --oneline (view each log in one line)
git whatchanged <file_path> (check the change log for specific file)
Reset (when you want to give up all changes and let all files back to the specific commit)
git log (get the SHA code for your desire commit)
git reset <SHA code>
git reset --hard <SHA code> (reset to specific sha, delete all other changes in your repo)
Checkout file (when you want to give up the changes and let the file change back to the status in latest commit)
git checkout <file_name>
Set an tracking relationship between remote and local branch manually
git branch --track localtrack origin/master (new a branch named localtrack, this branch will track the remote master branch, that's mean when you do pull or push on localtrach, it will go to origin/master)
when you set the tracking relationship, you can omit the parameter of this branch of command pull/push/...
git pull
git push
Notice the order of pull and push
git pull origin <remote branch>: <local branch>
git push origin <local branch>: <remote branch>
Current process:
1- push your code to github, then send pull request via GitHub
2- reviewers review your code and give comments
3- coder resolves all comments till add reviewers say "Good to merge"
4- click the button on GitHub to merge your branch into develop
5- delete your branch
[Resources]
Git Pro: http://git-scm.com/book/zh Git Cheat Sheet: http://www.git-tower.com/blog/git-cheat-sheet-detail/
comments powered by