Skip to content

Git

"Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows."

Repo Stats

Common Commands

Config

  • git config --global user.name "Your Name"
  • git config --global user.email "you@your.site"

Pulls can be merged or rebased...the default (merge) can be changed in your config.

Pulling without specifying how to reconcile divergent branches is discouraged. You can squelch this message by running one of the following commands sometime before your next pull:

hint: git config pull.rebase false # merge (the default strategy) hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only


You can replace "git config" with "git config --global" to set a default preference for all repositories. You can also pass --rebase, --no-rebase, or --ff-only on the command line to override the configured default per invocation.

Reseting author...

...of the previous commit

git commit --amend --reset-author

Clone

Get a shallow clone using the --depth option with value 1...

git clone --depth 1 reponame.git

Cloning from Github via SSH

git clone git@github.com:abc/xyz.git

Cloning a specifc branch...note that you can also add --single-branch just after the so as just to fetch that branch (without the others).

git clone --branch <branchname> <remote-repo-url>
git clone -b <branchname> <remote-repo-url>

OR

git clone -b <branchname> <remote-repo-url>

Cloning a Github Gist

Note you can also clone a Github gist (useful as an option for sharing code if you don't need all the bells & whistles of an actual repo).

git clone https://gist.github.com/kevinbluer/c7e5acade89b8f01488d21a53448f1f4

Commit

  • $ git commit -m "A commit"
  • $ git add file_i_forgot.txt

Changing the last commit message...

$ git commit --amend

Semantic commit messages... https://github.com/checkly/headless-recorder/blob/master/CONTRIBUTING.md

Commit messages should follow the Semantic Commit Messages format:

label(namespace): title

description

footer
  1. label is one of the following:
    • fix - puppeteer bug fixes.
    • feat - puppeteer features.
    • docs - changes to docs, e.g. docs(api.md): .. to change documentation.
    • test - changes to puppeteer tests infrastructure.
    • style - puppeteer code style: spaces/alignment/wrapping etc.
    • chore - build-related work, e.g. doclint changes / travis / appveyor.
  2. namespace is put in parenthesis after label and is optional.
  3. title is a brief summary of changes.
  4. description is optional, new-line separated from title and is in present tense.

Example:

fix(code-generator): fix page.pizza method

This patch fixes page.pizza so that it works with iframes.

Fixes #123, Fixes #234

Handling filename case-sensitivity

git config core.ignorecase false

Push empty commit

git commit —allow-empty -m “trigger”

Amend Last Commit Messages

git commit --amend -m "using human readable names for colors"

Reset / Unstaging

Examples below...

$ git reset HEAD <filename>
$ git reset --hard HEAD
$ git reset --hard <remote/branch>
$ git reset --hard 0d1d7fc32

Unstage a single file

git reset -- <filePath>

To reset the commit...

git reset HEAD~

Revert a single file in prior commit

git reset a4r9593432 -- path/to/file.txt

Squashing commits

"Commit squashing has the benefit of keeping your git history tidy and easier to digest than the alternative created by merge commits."

  • Github support: https://github.blog/2016-04-01-squash-your-commits/

Restoring deleted files

Example below (with detail on SO here)...

git checkout 6299aef841cc57d252a4559f092e561b6275993d^ — src/docs/tezos/truffle/getting-started/compiling-tezos-contracts.md

Stash

Examples...

  • git stash to stash current locally made changes
  • git stash list to list your stashes
  • git stash apply to restore the changes you just made :)

Rebase

https://git-scm.com/book/en/v2/Git-Branching-Rebasing

"you don't know what you are building while you are building it...but once you have built it, you can show how to build it commit-by-commit."

Merge

If you run into the following: fatal: refusing to merge unrelated histories

Use --allow-unrelated-histories, e.g.

git pull —rebase —autostash

Fast-forward

Getting the following: Fatal: Not possible to fast-forward, aborting

git pull --rebase

Aborting

git rebase --abort

Log

$ git log —-reverse
$ git log --oneline
$ git log --since=5.days --oneline

To show list a of files changed in recent commits...

git log --name-status -10 path/to/dir

Getting a fixed amount of logs...

git log -1

show a log of all commits that are in alpha and not in develop

git log origin/alpha ^origin/develop

which says "show a log of all commits that are in develop and not in alpha...

git log origin/develop ^origin/alpha --oneline

for just the number of commits

git log origin/develop ^origin/alpha --oneline | wc -l

Searching within the commit history...

git log -S xyz

More: https://stackoverflow.com/questions/4468361/search-all-of-git-history-for-a-string

Diff

...

Branching

Get a branch...

$ git checkout branchname

Create a new branch with name branchname...

$ git checkout -b branchname

To checkout the previously checked out branch (useful for toggling between two branches) 🎉

git checkout -

Disable warning about detached HEAD and the commit at the tip of the current branch is checked out.

git -c advice.detachedHead=false checkout FETCH_HEAD

Clearing / removing a local branch (more)...

git branch -d <local-branch>

Reverting any changes you've made...

git checkout .

Open the last branch

git checkout -

Clean

git clean -fxd

Rerere

https://git-scm.com/book/en/v2/Git-Tools-Rerere

Reflog

"Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository."

git reflog

Tag

Tag specific points in a repository’s history as being important...

git tag -a v2.2.1 -m "v2.2.1"
git push origin v2.2.1

Tagging a specific commit...

git tag -a v5.5.17 -m "v5.5.17" bf265bc40e41ba4e6260d0da7c61184cfe9c85e0
git push origin v5.5.17

Delete all the tags (locally) and grab them again from the remote

git tag -l | xargs git tag -d
git fetch —tags

Switch

"Switch to a specified branch. The working tree and the index are updated to match the branch. All new commits will be added to the tip of this branch."

git switch [<options>] [--no-guess] <branch>
git switch [<options>] --detach [<start-point>]
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --orphan <new-branch>

tig

Text mode interface for git...

  • https://github.com/jonas/tig

Note that you can use tig to more easily switch branches by typing r.

Bisect

"Use binary search to find the commit that introduced a bug"

git bisect <subcommand> <options>

Blame

"..."

Blame count

"It runs git blame on all text files in a repository, and returns a list of unique authors and their blame counts."

git ls-tree -r HEAD|sed -re 's/^.{53}//'|while read filename; do file "$filename"; done|grep -E ': .*text'|sed -r -e 's/: .*//'|while read filename; do git blame -w "$filename"; done|sed -r -e 's/.*\((.*)[0-9]{4}-[0-9]{2}-[0-9]{2} .*/\1/' -e 's/ +$//'|sort|uniq -c

Submodules

"Submodules allow you to keep a Git repository as a subdirectory of another Git repository." (more)

Convention for creating a submodule is to include a .gitmodules file in the root of the project and specify the submodule(s) as follows:

[submodule "optimism-integration"]
    path = optimism-integration
    url = https://github.com/ethereum-optimism/optimism-integration

Will clone and checkout the submodules specified in .gitsubmodule...note that you can optionally pass in a space-separated list of submodule names.

git submodule init

Example of adding a git submodule...

git submodule add -f -b c80b1beb953e623834ac54be879ade3891aa77b2 git@github.com:MetaMask/metamask-extension.git extension

Worktree

Manage multiple working trees attached to the same repository.

https://git-scm.com/docs/git-worktree

Utilities

Editor

Git allows you to change the editor for updating commit messages, etc.

Note when adding commit messages in VS Code...if you hit Enter after the first line, you can provide a body message (CMD + Enter after will submit the commit).


git config --global core.editor "nano"


## Hooks

Programs you can place in a hooks directory to trigger actions at certain points in git’s execution ([docs](https://git-scm.com/docs/githooks)).

Examples

- `pre-commit`
- `post-checkout`
- `pre-push`

## Cherrypick

"`git cherry-pick` is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. Cherry picking is the act of picking a commit from a branch and applying it to another. git `cherry-pick` can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong."

More: https://www.atlassian.com/git/tutorials/cherry-pick

## Worktrees

git clone —bare reponame git worktree add xyz // bare checkouts are a different “way” ```