Git
Branching Models
Git Flow
A branching model introduced by Vincent Driessen in 2010.
Branching in Git Flow
Branch name | Description | Branch off from | Merge back to |
---|---|---|---|
master | Main production branch. | none | none |
develop | Main development branch. | master | release-* |
feature-* | New features branch. | develop , feature-* | develop |
release-* | New release branch. | develop | master , develop |
hotfix-* | Hotfixes branch. | master | master , develop |
Notes:
- Branch
develop
is created from the very first commit ofmaster
. - Only
release-*
should merge back intomaster
. The exception to that ishotfix-*
. - Hotfixes from a
release-*
branch may be continuously merged back intodevelop
, until the complete release is finished, then merged tomaster
.
GitHub Flow
A branching model made by the GitHub team.
Branching in GitHub Flow
Branch name | Description | Branch off from | Merge back to |
---|---|---|---|
master | Main production branch. | none | none |
<branch-name> | New feature/change branch. | master | master |
Common steps in GitHub Flow to add a new feature/change:
- Branch off of
master
. - Make commits locally, and regularly push them to the remote repo.
- When ready to deploy (or asking for feedback), open a pull request.
- Once it is reviewed, merge it into
master
, then push changes. - At this point deploy (if not already done by CI/CD).
- Finally delete the new branch as itโs no longer needed.
The Seven Rules of a Good Commit Message
- Separate subject from body with a blank line.
- Limit the subject lite to 50 characters.
- Capitalize the subject line.
- Do not end the subject line with period.
- Use the imperative mood in the subject line.
- Wrap the body at 72 characters.
- Use the body to explain what and why (not how).
Git Cheatsheet
A cheatsheet for basic commands.
General
# Initialize
git init
# Add changes to stage
git add [file] # Specific files
git add . # ., -all, -A
git add -p [file] # Asks whether to commit or not by change hunk/block
# Do/apply changes
git commit -m <message>
git commit -am <message> # Automatic to `git add .` before committing
git commit --amend -m <message> # Update last commit message
git commit --amend --no-edit # Add modified files to the last commit
# Undo/modify changes
git reset --soft HEAD^ # Undo last commit (does not modify files)
git restore --sated <file> # Unstage file (reverse `git add`)
git restore <commit-id> # Returns to the last commit state
git revert <commit-id> # Reverts the last commit changes
# Others
git checkout -b <new> <src?> # Create & switch to a new (copied) branch
git checkout <branch-name> # Switch to a given branch
git checkout <commit-id> # Switch (return) to a given commit
git clone <repo-url> # Get a local copy of repo
git diff <file> # Show file's changes
git log # Show commit logs
git log --oneline # Show a log per line
git log -1 # Show the last (-1) commit
git rm -r --cached # Clear local repo
git show <tag-id> # Show tag data and commit
git status # Show repo's status
git tag # List tags
git tag -a <tag-id> -m <message> # Create an annotated tag
git tag <pattern> # List only matching tags
Branch
git branch <branch-name> # Create branch
git branch -d <branch-name> # -d, --delete (-D to force)
# List branches
git branch -a # -a, --all
git branch -l # -l, --local
git branch -r # -r, --remote
git branch -v # -v, --verbose (-vv to include upstream)
Remote
git remote add <remote-name> <repo-url>
git remote remove <remote-name>
git remote update <remote-name> --prune # Update remote branches' list
git remote show <remote-name> # Show remote info
# Send changes
git push
git push -u <remote-name> <branch-name> # -u, --set-upstream-to
git push <remote-name> --delete <branch-name> # Delete a branch
git push <remote-name> :<branch-name> # Delete a branch, alternative
# Grab all
git pull
git pull <remote-name> <branch-name> # If -u wasn't set before
git pull --allow-unrelated-histories # If repos doesn't match (local/remote)
Merge
# Merge changes from a source branch into another/current branch
git merge <src-branch-name> <target-branch-name?>
Reference: