Table of contents
Add all files from current directory
git add .
Add all files from specific directory
git add <directory>
Create a local branch and switch to it
git checkout -b <new-branch-name>
Switch to an existing branch
git checkout <branch-name>
List all local branches
List remote and local branches
git branch -a
List all merged branches
git branch --merged
Same as above but exclude master and develop branches
git branch --merged | egrep -v "(^\*|master|main|develop)"
git branch -d <branch-name>
git push origin --delete <branch-name-1> <branch-name-2>
Merge another branch into current branch
git merge <branch-name>
Rebase from a branch
git rebase <branch-name>
Interactive rebase + squash commits
git rebase -i# optional branch namegit rebase -i <branch-name># when you want to squash 17 commits where some include merge commitsgit rebase -i HEAD~17 --rebase-merges
Show commit history in single lines
git log --oneline
git log --graph --pretty --abbrev-commit --date=relative --branches
Show all local file changes in the working tree
Undo local modifications to all files
git checkout -- .
Unstage a file
git reset HEAD myfile
git cherry-pick <commit-hash> <commit-hash>
# list all git tagsgit tag -lgit tag --list# create a git taggit tag v1.9.2# remove a git taggit tag -d v1.9.2git tag --delete v1.9.2# push all tags to remotegit push --follow-tags origin mastergit push --follow-tags origin main
# undo pushgit push -f origin HEAD^:<branch-name># undo commitgit reset HEAD~1# undocommitforcegit reset --hard HEAD~1
In Git 2.28, a new configuration option,
init.defaultBranch is being introduced to replace
master, the previous default.
git config --global init.defaultBranch main
Use the diff3 format to see common ancestor code in conflict blocks
git config --global merge.conflictstyle diff3
and then conflict blocks will be formatted like:
<<<<<<<< HEAD:path/to/filecontent from target branch|||||||| merged common ancestors:path/to/filecommon ancestor content========content from your working branch>>>>>>> Commit message:path/to/file
where the default conflict block has been extended with a new section, delimited by |||||||| and ========, which reveals the common ancester code.
Comparing the HEAD block to the common ancestor block will often reveal the nature of the target-branch changes, allowing a straight-forward resolution.
For instance, breath easy if the common ancester block is empty:
<<<<<<<< HEAD:path/to/filecontent from target branch|||||||| merged common ancestors:path/to/file========content from your working branch>>>>>>> Commit message:path/to/file
as this means both branches have added lines; they haven’t tried to update the same lines. You can simply delete the merge conflict markers to resolve.
These release notes were generated using this script:
git log <commit-hash>...<commit-hash> --pretty=format:'- **%s** ([%h](github.com/mrmartineau/notes.zander.wtf/commit/%H)) by %an' --reverse
To retrieve the git commit hashes use this:
git log --oneline
- Anything in the master branch is deployable
- To work on something new, create a descriptively named branch off of master (ie: new-oauth2-scopes)
- Commit to that branch locally and regularly push your work to the same named branch on the server
- When you need feedback or help, or you think the branch is ready for merging, open a pull request
- After someone else has reviewed and signed off on the feature, you can merge it into master
- Once it is merged and pushed to ‘master’, you can and should deploy immediately