Skip to content

Instantly share code, notes, and snippets.

@delameter
Last active May 12, 2024 19:39
Show Gist options
  • Save delameter/919d2560a1d303930446300605329cfc to your computer and use it in GitHub Desktop.
Save delameter/919d2560a1d303930446300605329cfc to your computer and use it in GitHub Desktop.

git hard-to-remembers

Git command list, which I use infrequently enough to keep forgetting the correct syntax for.

Note

Remote name is assumed to be "origin".


Branches

Delete local branch

git branch -d <branchname>

Delete remote branch

git push -d <remote_name> <branchname>
# --- or ---
git push <remote_name> :<branchname>

Delete remote tag

(same commands as for branch deleting)

Clone master only

git clone <repo> --single-branch --branch <branch> .

List remote branches

git ls-remote --heads origin

Fetch single branch and checkout it

git fetch origin <remote-branch>:<local-branch>
git checkout <local-branch>

Add an extra remote as fetch upstream

git remote add upstream <remote-url>
git fetch -v upstream
git remote set-url --push upstream INVALID_URL

Any nonexisting URL can be used instead of original one; it doesn't have to be literally INVALID_URL. But AFAIK there is no way to delete the push url while keeping the pull one (or vice versa).

Clone full repo (all branches)

git clone <repo> --mirror

Note

This results in a bare repo.

Push all branches (from bare repo)

git push --all

Diffs

Compare by words

git diff --color-words <ref1> <ref2>

Compare by characters

git diff --color-words=. <ref1> <ref2>

Compare files not under git

git diff --no-index <path1> <path2>

Resets

Revert ALL uncommited changes

galya-otmena() {
    # no arguments
    # ! resets both index and working tree; purges untracked files
    git reset --hard HEAD
    git clean -d --force
}

Revert ALL uncommited changes, reset to latest remote version

galya-zavoz() {
    # usage: galya-zavoz [<target-branch>]
    # ! resets both index and working tree; purges untracked files
    git fetch
    git reset --hard "origin/${1:-"$(git branch --show)"}"
    git clean -d --force
}

Pushes

Push with upstream autosetup

fastpush() {
    # no arguments
    local gitbranch=$(git rev-parse --abbrev-ref HEAD)
    git push --set-upstream origin "$gitbranch"
}

Autocommit and push

autopush() {
    # usage: autopush [<commit-message>]
    local commitmsg="${*:-smol update}"
    local gitdir=$(git rev-parse --show-toplevel) || return 1

    git -C "$gitdir" add .
    git commit -m "$commitmsg"
    fastpush
}

Author:Alexandr Shavykin
Contact:0.delameter@gmail.com
Date:24-Jul-24 04:53:16 PDT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment