Git

On this page

Staging/Adding files Jump to heading

Add all files from current directory

git add .

Add all files from specific directory

git add <directory>

Branches Jump to heading

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

git branch

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)"

Deleting branches Jump to heading

git branch -d <branch-name>

Delete remote branches Jump to heading

git push origin --delete <branch-name-1> <branch-name-2>

Merging Jump to heading

Merge another branch into current branch

git merge <branch-name>

Rebasing Jump to heading

Rebase from a branch

git rebase <branch-name>

Interactive rebase + squash commits

git rebase -i

# optional branch name
git rebase -i <branch-name>

# when you want to squash 17 commits where some include merge commits
git rebase -i HEAD~17 --rebase-merges

Useful:

Logs Jump to heading

Show commit history in single lines

git log --oneline

Fancy Logs

git log --graph --pretty --abbrev-commit --date=relative --branches

Show all local file changes in the working tree

git diff

Cleanup Jump to heading

Undo local modifications to all files

git checkout -- .

Unstage a file

git reset HEAD myfile

Cherry picking Jump to heading

git cherry-pick <commit-hash> <commit-hash>

Tags Jump to heading

# list all git tags
git tag -l
git tag --list

# create a git tag
git tag v1.9.2

# remove a git tag
git tag -d v1.9.2
git tag --delete v1.9.2

# push all tags to remote
git push --follow-tags origin master
git push --follow-tags origin main

Undoing Jump to heading

# undo push
git push -f origin HEAD^:<branch-name>

# undo commit
git reset HEAD~1

# undocommitforce
git reset --hard HEAD~1

Rename files Jump to heading

git mv fileName.css filename.css

Config Jump to heading

Run the command git config --global -e to edit the global config

Default branch Jump to heading

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

Or add the following to your global config:

[init]
  defaultBranch = main

Make VS Code your default Git Editor, Diff Tool, or Merge Tool Jump to heading

Add the following to your global config:

[core]
  editor = code --wait
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = code --wait $MERGED

More info

zdiff3 diff format Jump to heading

Use the diff3 format to see common ancestor code in conflict blocks. Added in Git 2.35.

git config --global merge.conflictstyle zdiff3

Or add the following to your global config:

[merge]
  conflictstyle = zdiff3

and then conflict blocks will be formatted like:

<<<<<<<< HEAD:path/to/file
content from target branch
|||||||| merged common ancestors:path/to/file
common 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, breathe easy if the common ancester block is empty:

<<<<<<<< HEAD:path/to/file
content 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.

More info


Generate release notes Jump to heading

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

← Back home