๐Ÿƒ Leaf

Git

Branching Models

Git Flow

A branching model introduced by Vincent Driessen in 2010.

Branching in Git Flow

Branch nameDescriptionBranch off fromMerge back to
masterMain production branch.nonenone
developMain development branch.masterrelease-*
feature-*New features branch.develop, feature-*develop
release-*New release branch.developmaster, develop
hotfix-*Hotfixes branch.mastermaster, develop

Notes:

GitHub Flow

A branching model made by the GitHub team.

Branching in GitHub Flow

Branch nameDescriptionBranch off fromMerge back to
masterMain production branch.nonenone
<branch-name>New feature/change branch.mastermaster

Common steps in GitHub Flow to add a new feature/change:

  1. Branch off of master.
  2. Make commits locally, and regularly push them to the remote repo.
  3. When ready to deploy (or asking for feedback), open a pull request.
  4. Once it is reviewed, merge it into master, then push changes.
  5. At this point deploy (if not already done by CI/CD).
  6. Finally delete the new branch as itโ€™s no longer needed.

The Seven Rules of a Good Commit Message

  1. Separate subject from body with a blank line.
  2. Limit the subject lite to 50 characters.
  3. Capitalize the subject line.
  4. Do not end the subject line with period.
  5. Use the imperative mood in the subject line.
  6. Wrap the body at 72 characters.
  7. 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: