Skip to main content
Git
December 14, 2015

References

Configuration

Alias

To create shortcut for long commands. Below defines loagd instead of log --oneline --all --graph --decorate. Config file location can be --global, --system, --local, --file, --blob.

1
git config --global alias.loagd "log --oneline --all --graph --decorate"

List, locate, and inspect aliases:

1
2
3
4
5
6
7
8
# List all global config
git config --global --list

# Find the origin of a config key
git config --show-origin alias.loagd

# Show the value of a config key
git config --get alias.loagd

Personal Access Token (PAT)

You can use personal access tokens to authenticate against Git over HTTP. They are the only accepted password when you have Two-Factor Authentication (2FA) enabled.

Create a PAT first:

1
2
3
git clone https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

Sign Off Your Commits

1
2
git commit --signoff --message 'This is my commit message'
git commit -s -m "This is my commit message"

Cloning

Clone a specific branch

1
git clone -b <branch> <remote_repo>

Clone including submodules

1
git clone --recurse-submodules -j8 <remote_repo>

Branches

List branches

1
2
git branch       # local branches
git branch -a    # all branches (local + remote)

Create a branch

1
2
3
git branch <branch-name>
git branch <branch-name> <sha1-of-commit>   # from a specific commit
git checkout -b <branch-name>                # create and switch

Switch branches

1
git checkout <branch-name>

Push a branch to remote

1
git push origin <branch-name>

Rename a branch

1
2
3
4
5
6
7
8
9
# Rename the current branch
git branch -m <new-name>

# Rename a different branch
git branch -m <old-name> <new-name>

# Update the remote
git push origin :<old-name> <new-name>
git push origin -u <new-name>

Delete a branch

1
2
3
4
5
6
7
# Delete locally
git branch -d <branch-name>

# Delete remotely
git push origin --delete <branch-name>
# or shorter
git push origin :<branch-name>

Prune stale remote-tracking branches

Deletes stale remote-tracking branches that have already been removed from the remote.

1
2
3
4
5
# Preview what will be removed
git remote prune origin --dry-run

# Actually prune
git remote prune origin

Move uncommitted changes to a new branch

1
2
git checkout -b <new-branch>
git status

Rename the default branch

1
2
3
4
git branch -m old_branch new_branch
git push origin new_branch
# Then change "Default Branch" in GitHub/GitLab settings
git push origin :old_branch

Commits

Amend the latest commit message

1
2
git commit --amend -m "New message"
git push --force origin <branch-name>

Remove an unpushed commit (keep changes staged)

1
git reset --soft HEAD~

Remove a pushed commit

Find the commit you want to revert to:

1
git log

Force push that commit as the new HEAD:

1
git push origin +<commit-hash>:main

Or reset and force push:

1
2
git reset --hard <commit-hash>
git push origin HEAD -f

If others work on the same branch, they need to sync:

1
2
git fetch
git reset --hard origin/<branch-name>

Switch to a specific commit

1
2
3
4
git checkout <commit-hash>

# Optionally create a branch from it
git checkout -b <new-branch> <commit-hash>

Tags

Rename a tag

1
2
3
4
git tag <new> <old>
git tag -d <old>
git push origin :refs/tags/<old>
git push --tags

Delete a tag

1
2
git push origin :refs/tags/<tag-name>
git tag --delete <tag-name>

Submodules

Delete a submodule

Remove the submodule entry from .git/config:

1
git submodule deinit -f path/to/submodule

Remove the submodule directory from .git/modules:

1
rm -rf .git/modules/path/to/submodule

Remove the entry in .gitmodules and the submodule directory:

1
git rm -f path/to/submodule

Syncing & Remotes

View remote URLs

1
git remote -v

Set up upstream remote (for forks)

1
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

Sync fork with upstream

1
2
3
4
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Fetch everything (branches, tags, prune)

1
git fetch --all --tags --prune

Pull with rebase

Rebase your local changes on top of the latest remote changes:

1
git pull --rebase

Undoing Changes

Revert uncommitted changes

1
2
3
4
5
# Revert modified files
git reset --hard

# Remove untracked files and directories
git clean -fd

Reset local branch to match remote

1
2
git fetch origin
git reset --hard origin/main

Save your current state before resetting (just in case):

1
2
git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Reset both local and remote

1
git reset --hard <commit-hash> && git push --force origin <branch-name>

Merge Strategies

Explicit Merge: Creates a new merge commit. (--no-ff)

Explicit Merge

Workflow:

1
2
3
4
5
6
7
8
git checkout -b development
(do your work on "development" branch)
git status
git commit -am  "updates in development branch"
git checkout main
git merge --no-ff development
git branch -d development
git push origin main

Fast Forward Merge: Forward rapidly, without creating a new commit: (--ff)

Fast Forward Merge

Rebase: Establish a new base level:

Rebase

Squash on Merge: Crush or squeeze (something) with force so that it becomes flat: (--squash)

Squash on Merge

Fork & Pull Request Workflow

  1. Fork the repository and clone your fork
  2. Set up the upstream remote
  3. Create a topic branch for your changes
  4. Commit, push, and open a Pull Request
1
2
3
4
5
6
7
8
9
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
git fetch upstream
git checkout main
git merge upstream/main
git checkout -b ISSUE1
# ... make changes ...
git commit -am "Fix for ISSUE1"
git push origin ISSUE1
# Open a PR from ISSUE1 → upstream/main

Inspection

View commit history

1
2
git log
git log --oneline --all --graph --decorate

Show current commit hash

1
git log --pretty=format:'%h' -n 1

List the N largest files

1
git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10

Quick Reference

CommandDescription
git initInitialize a new repository
git addStage files
git commitCommit staged files
git branchList branches
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git merge <branch>Merge a branch into current
git diffShow unstaged changes
git diff --cachedShow staged changes
git logShow commit history