Git flow
Git command and flow.
I. Git branch strategy
master branch
- this is main branch, fist commit to this branch
- this branch always be merged, not merge to any branch
- be up to date
develop branch
- main branch for develop
- this branch need qualites same as master branch
- sometime merge to master branch
feature[feature-name] branch
- sub branch for a new feature
- get new from develop branch
- finish rebase to develop branch or merge to develop branch
bugfix[bug-name] branch
- sub branch for fix each bug
- get new from master branch
- after pass test then merge to develop branch
release[1.x] branch
- branch for deploy stage and production
- get new from master branch
- have description of features and bugs is resolved
- after fixed and pass testing then merge develop branch and master branch
hotfix[hotfix-name] branch
- sub branch for hot fix bug of release/stage/production branch
- get new from master branch
- after fixed and pass testing then merge to master and develop branch
Questions?
- What different of develop branch with other branch?
- master branch stores the official release history.
-
develop branch serves as an integration branch for features. +
- What different of bugfix branch with hotfix branch?
- bugfix after testing, hotfix after release or deploy stage/production. + —
II. Git account setting
- check git version:
git --version
- set user name:
git config --global user.name "[user-name]"
- show user name:
git config --global user.name
- caching git:
git config --global credential.helper wincred
- push with new account need to use this command then submit new authentication:
git push origin master
III. Git command on repository
Show branch
- show all branch on local checked out:
git branch
- show all branch on remote and local:
git branch -a
- show file on branch:
git ls-tree -r [branch-name]
2. checkout a branch
- checkout an exist branch:
git checkout [branch-name]
- checkout a new branch:
git checkout -b [branch-name]
Fetch from branch
- fetch from current branch:
git fetch
- fetch from a branch:
fetch [branch-name]
Create branch
- create new branch and checkout this in local repository:
git checkout -b [branch-name]
- create new branch on local and push it to remote repository:
git checkout -b [branch-name] git push [branch-name] [branch-remote-name]
Add files change
- commit a file:
git add -[file-name]
- commit all files, file new, file exist:
git add .
- commit all files, file exist and file is deleted:
git add . -u
- commit all files, file new, file exist and file is deleted:
git add . -A
Unstage file
- reset HEAD cusor:
git reset HEAD
- undo add all files:
git reset
- undo add a file:
git reset --[file-name]
Discard file
- checkout a file:
git checkout --[file-name]
- checkout all files:
git checkout -- .
- discard all change and clean:
git reset git checkout . git clean -fdx git clean -df
Remove a file on staging area
git rm --cached [file-name]
Commit change
- commit with message:
git commit -m "sort description"
- add and commit with message:
git commit -a -m "sort description"
- commit empty to trigger rebuild
git commit --allow-empty -m "Trigger rebuild"
Push change
- push change to origin on current branch:
git push [remote-name] : [branch-name]
- if status is checkout a branch-name and we want to push this branch:
git push
- push a local branch to upstream of parent branch on remote:
git push --set-upstream [remote-name] [branch-name]
- if we want to push current branch:
git push -u [remote-name] HEAD
- push single tag:
git push [remote-branch] [tag-name]
- push all tags:
git push [remote-branch] --tags
Delete branch
- delete branch on local:
git branch -d [branch_name]
- delete branch on remote:
git push origin --delete [branch_name]
Rename branch
- rename your local branch, if you are on local branch:
git branch -m [new-name]
- if you are on a different branch:
git branch -m [old-name] [new-name]
- delete the old remote branch and push the new branch:
git push origin :[old-name] [new-name]
- reset the upstream branch for the new branch local branch:
git push origin -u [new-branch]
Rebase branch, rebase a feature branch to master branch
git checkout [feature\branch-name]
git rebase to master
- if conflic => resolve conflic => commit file after resolve => push to feature/branch-name
git rebase --continue
- if conflic => if not resolve then skip step
git rebase --skip
- if all point are rebase, ready to merge new commit to master
git checkout master git merge feature/branch-name
- after merge all commit of feature/branch-name on head of master
- if pass QA test, remove features/branch-name
Merge branch
- merge a feature branch to master branch:
git checkout master git merge feature/branch-name git push
- merge feature branch no-ff branch develop branch:
git checkout develop git merge --no-ff feature git branch -d feature git push origin develop
Tag
- add tag:
git tag -a [tag-name] -m "[tag-description]"
- show info of tag:
git show tag [tag-name]
- show list tag:
git tag git tag -l
- show list tag like character:
git tag -l "[v1.0*]"
- push and share tag:
git push origin [tag-name]
- push and share not aready tags:
git push origin --tags
Log infomation
- log infomation commit:
git log --oneline
- show infomation commit of HEAD cusor:
git reflog
Reset a commit
- reset all change and not commit at HEAD cusor:
git reset --hard HEAD
- reset hard, is remove commit in remote and working space, very clean:
git reset --hard [commit-SHA1]
- reset soft, remove commit but keep change of this commit:
git reset --soft [commit-SHA1]
- reset mixed:
git reset --mixed [commit-SHA1]
- reset merge:
git reset --merge [commit-SHA1]
- reset keep:
git reset --keep [commit-SHA1]
Recover a reseted commit
- recover a reseted commit
git branch recover-branch [commit-SHA1]
- can recover file added when reset commit occour:
git fsck --full git show [commit-SHA1]
- show change infomation, can apply change file again
Revert a commit
- undo a merge commit and want to keep history commit:
git revert HEAD
Amend a commit
- you commied a file then if you want add a new file change in a commited:
git commit --amend --no-edit
Stash
- when you checkouting a branch and have a change, git stash save a change when you want to checkout another branch:
git stash save
- show list stash:
git stash list
- delete a stash, n is stash number:
git stash pop stash@{n}
- delete all stash: git stash clear
Git flow, this is template base on Vincent Driessen
- init a git flow:
git flow init
- git flow for a feature:
git flow feature start [feature-name] git flow feature finish [feature-name] git flow feature publish [feature-name] git flow feature pull [remote-name] [feature-name]
- git flow for a release:
git flow release start RELEASE [BASE] git flow release publish RELEASE git flow release track RELEASE git flow release finish RELEASE
- git flow for a hotfix:
git flow hotfix start VERSION [base-name] git flow hotfix finish VERSION
Cherry-pick
Allows you to pick any commits you made in any branch to any other branch. In your case you can simply checkout master branch and then cherry-pick all the commits from any branch that you wish.
git add -[branch-name-A]
git commit -m "[fix-name]"
git checkout -[branch-name-B]
git cherry-pick [branch-name-A]
Remove sensitive git history
-
Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH
-
Add all the files:
git add -A
-
Commit the changes:
git commit -am "Initial commit"
-
Delete the old branch:
git branch -D master
-
Rename the temporary branch to master:
git branch -m master
-
Finally, force update to our repository:
git push -f origin master
Add remote to source
-
Add remote to exist source code:
git init
git remote add origin [fir-url]
-
Solve pull issue The “fatal: refusing to merge unrelated histories” Git error:
git pull origin master --allow-unrelated-histories
Questions?
- What are different between reset and revert commit?
References
- https://git-scm.com/docs
- https://danielkummer.github.io/git-flow-cheatsheet/index.vi_VN.html
- https://nvie.com/posts/a-successful-git-branching-model/