Git
- Free git book: https://git-scm.com/book/en/v2/
- π git out of town
- Commit Often, Perfect Later, Publish OnceβGit Best Practices
- Oh Shit, Git!?!
Repos
Initialise a git repo
Make a .gitignore file. In terminal:
touch .gitignore
echo ".DS_Store" > .gitignoreMake directory into git repo and connect to remote:
git init
git add .
git commit -m "init"
git remote add origin https://github.com/USER/REPO.gitNow you have two options: Push everything to remote repo:
git push -u origin master
# Or "origin main"
# for force push add "--force"Pull everything from remote repo:
git pull origin masterPull from another repo
git remote add upstream https://github.com/user/repo.git
git pull upstreamPulling another branch and then merging it
git fetch origin master:master
git merge masterDelete a git repo
- Option 1:
CMD+SHIFT+.to show hidden files - Option 2:
rm -fr .gitin the folder that has the repo
Rename a repo
- Go to the remote host (for example, https://github.com/User/project).
- Follow the hostβs instructions to rename the project (will differ from host to host, but usually Settings is a good starting point).
- Go to your local repository directory (i.e., open a command prompt and change to the repositoryβs directory).
- Determine the new URL (for example,
[email protected]:User/project-new.git) - Set the new URL using Git:
git remote set-url origin [email protected]:User/project-new.gitBranches
# Create branch
git branch BRANCHNAME
# Create and move
git checkout -b BRANCHNAME
# Move Head pointer (what's active)
git checkout BRANCHNAME
# Differences between branches
git diff master..BRANCHNAME
# Merge into master
git merge BRANCHNAME
# See merged
git branch --merged
# Delete branches
git branch -d BRANCHNAME
# See all branches
git log --all --decorate --oneline --graph
Rename branches: How to Rename a Local and Remote Git Branch β A Quick Guide
Manually approve each change in a merge
git merge --no-commit --no-ff merge_branchMerge and delete a branch
Local
git merge source-branch && git branch -d source-branchRemote
git push origin --delete branchCommits
Conventional commits
- Conventional Commits
- Cheat Sheet: https://kapeli.com/cheat_sheets/Conventional_Commits.docset/Contents/Resources/Documents/index
Using emoji in commits
Source: https://opensource.com/article/19/2/emoji-log-git-commit-messages
- Imperative
- Make your Git commit messages imperative.
- Write commit message like youβre giving an order.
- e.g., Use β Add instead of β Added
- e.g., Use β Create instead of β Creating
- Rules
- A small number of categories are easy to memorize.
- Nothing more, nothing less
- e.g. π¦ NEW, π IMPROVE, π FIX, π DOC, π RELEASE, and β TEST
- Actions
- Make Git commits based on actions you take.
- Use a good editor like VSCode to commit the right files with commit messages.
- β¨ NEW: IMPERATIVE_MESSAGE
- Use when you add something entirely new.
- e.g., β¨ NEW: Add Git ignore file 2.π IMPROVE: IMPERATIVE_MESSAGE
- Use when you improve/enhance piece of code like refactoring etc.
- e.g., π IMPROVE: Remote IP API Function
- π FIX: IMPERATIVE_MESSAGE
- Use when you fix a bug. Need I say more?
- e.g., π FIX: Case converter
- π DOC: IMPERATIVE_MESSAGE
- Use when you add documentation, like README.md or even inline docs.
- e.g., π DOC: API Interface Tutorial
- π RELEASE: IMPERATIVE_MESSAGE
- Use when you release a new version.
- e.g., π RELEASE: Version 2.0.0
- β TEST: IMPERATIVE_MESSAGE
- Use when you release a new version.
- e.g., β TEST: Mock User Login/Logout
Misc
Remove a file
git rm <file>
git rm -r <folder>
git commit -m "Deleted the file from the git repository"Clean up loose object error
git gc --aggressive --prune=nowSubmodules
git submodule add https://github.com/url-to-submodule
# Update submodule:
git submodule update --recursive --remoteIgnore all subdirectories
# In .gitignore:
**/Print a changelog
Generating release notes from git commit messages using basic shell commands (git/grep) | SAP Blogs
git log --pretty="- %s"Since last release
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%s"My categories
β¨ New
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%s" -i -E --grep="^β¨ NEW: "π Improvements
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%s" -i -E --grep="^π IMPROVE: "π Bugs
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%s" -i -E --grep="^π FIX: "π Docs
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%s" -i -E --grep="^π DOC: "My aliases
release [VERSION NO]: Release a new Obsidian version
Workflow
Inspired by A successful Git branching model Β» nvie.com
Main branches (infinite lifetime)
main: code is always production-readydev: always reflects the latest working changes
dev merges into main when code is stable. Therefore each dev into main merge is a new release by definition
Supporting branches (limited lifetime)
Feature/topic branches
- may branch off from:
dev - must merge back into:
dev - naming convention:
iss2Merge withgit merge --no-ff myfeatureto not loose history
Release branches
- may branch off from:
dev - must merge back into:
devandmain - naming convention:
release-*
Branch of from dev when it is almost ready for another release.
Hotfix branches
- may branch off from:
main - must merge back into:
devandmain - naming convention:
hotfix-*
They are like release branches but unplanned.
Resetting local changes
git reset --hard HEADreset back to HEAD of the branchgit clean -dfDiscard any new files or directories