Git quick reference

git-transport

(Target audience: developers who knows SVN/CVS, so it skips some intro about Git)

Download

Command-line client

http://git-scm.com/downloads (Windows: http://code.google.com/p/msysgit/ )

View command help (options, parameters, … ) :

git help command

or

git command --help

(command is one of: clone , config, add , commit , push , log , fetch , pull , merge , rebase , checkout , branch , reset , status, blame, diff , apply , revert , update-ref, … )

.

GUI client

TortoiseGit (nên cài Git for Windows trước): http://code.google.com/p/tortoisegit/wiki/Download

(Lưu ý: cài trên Win nhớ đừng đổi CRLF – cứ checkout As Is & commit As Is

git config --global core.autocrlf false

)

GitEye: http://www.collab.net/downloads/giteye

SourceTree: http://www.sourcetreeapp.com/download/

git-cola: http://git-cola.github.io/downloads.html

IDE integration/plugins

IDEA: http://www.jetbrains.com/idea/webhelp/using-git-integration.html

Eclipse: EGit plugin, update site – http://download.eclipse.org/egit/updates

(Eclipse users nên dùng STS có cài sẵn plugin: https://spring.io/tools/sts/all )

Netbeans: http://plugins.netbeans.org/plugin/37577/git-versioning-system-support

.

Collaboration models

git_data_transport_new

Centralized

Popular: single-man project, small team (< 6 developers) with high trust, SVN evangelists .

Also known as: Blessed-center, Free-for-all, Shared-repo .

Summary: Everybody pull and push changes to One centralized (blessed/shared) repository – “origin” , One branch “master” (exactly the same way with SVN – trunk & HEAD) .

Visualized model/workflow: SharedRepo Model DucQuoc

Common operations:

git init --bare /path/to/repo.git

git clone https://user@host/path/to/repo.git

git status # View the state of the repo
git add LocalFile.txt # Stage a file 'LocalFile.txt'
git commit # Commit a file

git push origin master

git pull --rebase origin master

git rebase --continue LocalFile.txt

git push origin master

.

Branch upstream

Popular: Small & Medium team (5-40 developers), private/prorietary repo.

Also known asFeature-branchCheckout-rebase

Summary: Everybody create their own branch (for some feature/issue), then commits to it. Later branch will be pushed to remote ‘origin’, then merged (after pull-request).

Visualized model/workflowFeatureBranch GitModel DucQuoc

Common operations:

# switch to new branch 'ducquoc-br' based on master
git checkout -b ducquoc-br master # modify and add some file git status
git add DucFile.txt
git commit # push to remote repo 'origin', branch 'ducquoc-br' git push -u origin ducquoc-br # update some more and push git push  # pull a branch with rebase option to fetch without merge git pull --rebase origin ducquoc-br # reset local master branch to be latest as origin git checkout origin/master  git branch -D master  git branch master  git checkout master # reset local master branch - using reset hard git update-ref refs/heads/master origin/master git reset --hard master # delete a local branch 'ducquoc-br' git branch -D ducquoc-br # delete remote branch 'ducquoc-br' on 'origin' (and strip some un-merged commits) git branch -rd origin/ducquoc-br git push origin :ducquoc-br # If you don't use the colon ':' , use --delete option instead

.

Fork master

Popular: Medium or big size teams, Open Source projects, Online/cloud hosting projects.

Also known asGitHub-flowFork-and-PullUpstream-repo

Summary: Everybody forks the repo (as a clone ‘upstream‘), then modify master branch and push to ‘upstream’. Pull requests are primarily to merge ‘master‘ of ‘upstream‘ to ‘origin’.

Visualized model/workflowForkNPull GitModel DucQuoc

Common operations:

# list the current remote repos
git remote -v
# set a new remote 'upstream'
git remote add upstream https://upstream-repo-URL/repo.git
# fetch the upstream repo - not merge/rebase yet
git fetch upstream
# switch to 'master' branch and merge upstream's master branch
git checkout master
git merge upstream/master
git log HEAD..origin/master

.

Dictator & lieutenant

Popular: not popular, just in case there are a few ‘newbies/rookies’ who are eager to commit and push code (usually with low quality).

Also known asReviewerAmbassadorBenevolent-Bottleneck

Summary: just a dialect of ‘Branch upstream‘ or ‘Fork master‘ , but instead of pushing to ‘origin‘ , normal devs will push to main devs repo (lieutenants), and from then push to the dictator repo ‘reviewer‘ , then the ‘reviewer’ will decide to merge and push to ‘origin‘.

Visualized model/workflowDictator GitModel DucQuoc

Common operations:

git stash
git stash apply
git stash save "work in progress - login CSRF 2013.08.15 duc"
git stash list
git stash apply stash@{1}
git stash pop #different from apply: will delete the applied stash
git stash drop 
git stash clear

.

References:

+ OrCz crash course: http://git.or.cz/course/svn.html

+ Net Tuts+ guide: http://net.tutsplus.com/tutorials/other/easy-version-control-with-git/

+ Vogella’s tutor: http://www.vogella.com/articles/Git/article.html

+ Scott Chacon’s ProGit: http://git-scm.com/book/

+ Command quick reference: http://jonas.nitro.dk/git/quick-reference.html

+ Command reference: https://www.kernel.org/pub/software/scm/git/docs/

+ Online hosts: GitHub, BitBucket, Gitorious, GoogleCode, Assembla, repo.or.cz, Unfuddle, GitLab, CloudHost, Deveo, …

(For certain GUI clients, you may visit the official documentation of the client tool for latest updates, since they are not consistent with each other.

For instance, EGit: https://wiki.eclipse.org/EGit/User_Guide

)

.

FAQ of branch-upstream model

1/ How do I update latest code (origin master) for my local brach (assumed name duc-br ) ?

Use below commands to sync master on a daily basis (update duc-br to your branch name) :

cd YOUR_ROOT_DIR_GIT

git checkout master && git pull && git checkout duc-br && git merge master

2/ How do I make my master branch exactly the same with latest origin master ?

Use below commands:

git update-ref refs/heads/master origin/master && git reset --hard master 

3/ How do I make my duc-br branch the same with remote branch duc-br (origin)?

Use below commands: (or use “delete” approach as question #7)

git update-ref refs/heads/duc-br origin/master && git reset --hard duc-br 

It’s recommended to know merge abort (#8) and resolving conflict merge, otherwise you may have to delete local branch and checkout it again (#7).

4/ How do I save all my non-commit changes and update current branch with rebase ? 

Use below commands:

git stash && git pull --rebase || git pull --rebase && git stash pop

5/ How do I save all my non-commit changes and create new branch (duc-br) with them?

Use below commands:

git stash && git stash branch duc-br
git push --set-upstream origin duc-br

6/ How do I revert my code to previous commit (i.e. discard all non-commit changes) ? 

Use below commands:

git reset HEAD --hard

7/ How do I revert my code (of branch master) to latest commit of previous pull (i.e. discard all local changes including commits/ahead) ? 

Use below commands:

git checkout origin/master 
git branch -D master 
git branch master 
git checkout master
## may need to --set-upstream with -u option first push

(actually it delete local branch and checkout remote)

8/ How do I check conflict with another local branch (i.e. dry-run to merge local branch) ? 

To check conflict with another local branch: develop

git format-patch develop --stdout | git apply --check

To dry-run a merge with another local branch: duc-br

git merge --no-commit --no-ff duc-br

Then abort the dry-run merge: duc-br

git merge --abort

(when a merge conflict occurs, you can ALWAYS run this abort to return non-conflict state)

9/ What is the different between update/merge of “rebase”, “stash”, “cherry-pick”, “squash”, “commit –amend” ? 

Read the friendly manual !

But any GUI client tool should support all of those and just use one is enough for a convention.

Rebase (re-arrange the commits so that history looks better):

https://backlogtool.com/git-guide/vn/stepup/stepup2_8.html

(Sử dụng rebase cần hiểu và làm đúng các bước nên dễ bị merge conflict, do đó ng ta sử dụng stash/squash/cherry-pick để đơn giản hóa và tránh conflict khi merge/rebase)

Stash (temporarily store the work in progress to local stage or another branch):

http://code.tutsplus.com/tutorials/quick-tip-leveraging-the-power-of-git-stash–cms-22988

Squash (make multiple commits into a single commit):

http://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git

Commit –amend (merge the commit into previous commit and make it single commit):

https://robots.thoughtbot.com/git-interactive-rebase-squash-amend-rewriting-history

Cherry-pick (select a few of commits to merge to the branch):

https://backlogtool.com/git-guide/vn/stepup/stepup7_4.html

Bonus: Vietnamese introduction articles about Git

http://blog.appconus.com/2015/08/07/git-cho-nguoi-moi-bat-dau/

https://hieusensei.com/git-can-ban

http://rogerdudler.github.io/git-guide/index.vi.html

http://blog.duyet.net/2015/04/git-va-cac-khai-niem-co-ban.html#.WSkS2GiGPIU

https://github.com/hocchudong/git-github-for-sysadmin

https://blog.siliconstraits.vn/quan-ly-code-voi-git/

./.

Advertisements

About DucQuoc.wordpress.com

A coder, husband and brother...
This entry was posted in Coding, Linux, Skill. Bookmark the permalink.

3 Responses to Git quick reference

  1. Pingback: Git pull all | DucQuoc's Blog

  2. Pingback: SSH without password | DucQuoc's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s