Skip to content

EvitanRelta/gitconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Setup

The steps below is for creating symlinks in the system root, targeting the config files in this repo.

Specifically, it'll create these symlinks:

# In root: ~/
.bash_profile@   --> gitconfig/.bash_profile
.bashrc@         --> gitconfig/.bashrc
.git_aliases@    --> gitconfig/.git_aliases
.gitconfig@      --> gitconfig/.gitconfig
.vim@            --> gitconfig/.vim
.vimrc@          --> gitconfig/.vimrc

On Linux

git clone https://github.com/EvitanRelta/gitconfig
cd gitconfig
bash init.sh

On Windows

  1. Run Git Bash as administrator
  2. Execute export MSYS=winsymlinks:nativestrict
    (this will allow creation of symlinks used in init.sh)
  3. Do the steps in the On Linux section above

Based on: https://stackoverflow.com/a/40914277


Git Aliases

git ab

Aborts the current rebase/merge/cherry-pick/revert command.

# Alias for:
in_prog_cmd="$(git get-in-prog-cmd)"
    && echo "Aborting $in_prog_cmd..."
    && eval "git $in_prog_cmd --abort"

git addn [file_paths]

Negative add. Adds everything except the specified file(s).

# Alias for:
git add -A
git reset [file_paths]

# Usage:
git addn ./file1 ./dir/file2

git branch-d-both [branch_name]

Deletes branch both locally and on remote.

# Alias for:
git branch -d [branch_name]
git push -d origin [branch_name]

git cane

# Alias for:
git commit --amend --no-edit

git clone-nonempty [-f] [github_repo_url] [clone_to_path]

Clones repo into a non-empty dir.
(Force flag force overwrites any local files that conflicts with the repo files)

# Alias for (w/o force flag):
mkdir -p [clone_to_path]
cd [clone_to_path]
git init
git remote add origin [github_repo_url]
git fetch
git checkout -t origin/master

# Alias for (force flag):
# Force overwrite any conflicting files
...
git checkout -tf origin/master

# Example usage:
git clone-nonempty -f https://github.com/EvitanRelta/my-repo .

git code-unmerged

Open all unmerged files in VSCode editor via the code command.

For example, if FILE1, PATH/TO/FILE2 and ../FILE3 are unmerged, this command runs:

code "FILE1"
code "PATH/TO/FILE2"
code "../FILE3"

git con

Continues the current rebase/merge/cherry-pick/revert command.

# Alias for:
in_prog_cmd="$(git get-in-prog-cmd)"
    && echo "Continuing $in_prog_cmd..."
    && eval "git $in_prog_cmd --continue"

git cone

Continues the current rebase/merge/cherry-pick/revert command while preventing the prompt for editing the commit message.
(similar to --no-edit flag for git commit)

# Alias for:
in_prog_cmd="$(git get-in-prog-cmd)"
    && echo "Continuing $in_prog_cmd (no edit)..."
    && eval "git -c core.editor=true $in_prog_cmd --continue"

git conea

Adds all unstaged changes, then continues the current rebase/merge/cherry-pick/revert command while preventing the prompt for editing the commit message.

# Alias for:
git add -A
git cone

git cum [flags/parameters]

# Alias for:
git add -A
git commit [flags/parameters]

git cumane

# Alias for:
git add -A
git commit --amend --no-edit

git delete-this

Deletes the current branch, checking out to previous branch.

# Alias for:
current_branch=$(git get-current-branch)
git checkout -
git branch -D $current_branch

git dump [-f]

Dumps any unstaged changes.
(Force flag dumps staged changes too)

:/ is to select all files, including files not in the current directory (eg. "../../file.ext")

# Alias for (w/o force flag):
git restore :/
git clean -df :/


# Alias for (force flag):
git reset
git restore :/
git clean -df :/

git f [commit_hash]

Commit-fixup targeting [commit_hash].

# Alias for:
git commit --fixup=[commit_hash]

git ff [remote]

Fast-forwards current branch to corresponding branch on [remote].

# Alias for:
current_branch=$(git get-current-branch)
git fetch [remote]
git merge --ff-only [remote]/$current_branch

git git [commands/flags/parameters]

For when you accidentally type git twice.

# Alias for:
git [commands/flags/parameters]

git link [-f] [github_repo_url]

Links local repo to [github_repo_url], by setting remote origin to url and set-upstream push.
(Force flag force-pushes local repo to remote)

# Alias for (w/o force flag):
git remote add origin [github_repo_url]
git remote set-url origin [github_repo_url]
git push -u origin master


# Alias for (force flag):
...
git push -uf origin master

git merge-this

Merge (no fast-forward) current branch (ie. [branch_name]) to master, and deletes it locally.

# Alias for:
git checkout master
git merge --no-ff [branch_name]
git branch -D [branch_name]

git pop [flags/parameters]

# Alias for:
git stash pop [flags/parameters]

git pull-all [-s [ignore_branch_name] [ignore_branch_name] ...]

Pulls all remote branches.
-s skip-flag skips pulling [ignore_branch_name] remote branches.

# Alias for:
git fetch -p    # Update + prune remote branches
echo "From $(git remote get-url origin)"
for remote_branch in `git branch -r | grep -v " -> " | grep "origin/"`; do
    branch=${remote_branch#origin/}

    git fetch
        --update-head-ok            # Allows changes to current branch/HEAD
        origin "$branch:$branch"    # Updates branch
        2>&1                        # Fix 'fetch' not outputting to 'grep'
        | grep -v "From"            # Removes repeated "From [REMOTE_URL]"

    git branch --quiet -u "$remote_branch" "$branch"    # Sets upstream, as 'git fetch' doesn't
done

# '-s' skip-flag implementation too complicated to show

git pull-force

Pulls and overwrite current local branch.

# Alias for:
git fetch
git reset --hard "origin/$(git branch --show-current)"

git pull-pr [username]:[branch] [optional_remote_name]

Pulls (and checkout to) a PR's branch. The [username]:[branch] is given in the GitHub's PR description.

If no remote with a URL containing [username] exists, create a new remote named [username] (or [optional_remote_name] if given) with a URL based on origin remote.
(eg. If origin https://github.com/EvitanRelta/repo then the URL will be https://github.com/[username]/repo)

If a remote exists, but it's name isn't equal to [optional_remote_name], then that remote is renamed to [optional_remote_name].

For example, for the PR description:
"afiqzu wants to merge 1 commit into CS3219-AY2324S1:assignment-5 from afiqzu:assignment-5"
[username]:[branch] = afiqzu:assignment-5

# Given this scenario:
$ git remote -v
oldremote  https://github.com/EvitanRelta/repo
origin     https://github.com/myorigin/repo

Example usage 1:

git pull-pr EvitanRelta:pr-branch
# is alias for:
git fetch oldremote
git checkout -b oldremote-pr-branch oldremote/pr-branch

Example usage 2:

git pull-pr EvitanRelta:pr-branch newremote
# is alias for:
git remote rename oldremote newremote
git fetch newremote
git checkout -b newremote-pr-branch newremote/pr-branch

Example usage 3:

git pull-pr NewUser:pr-branch newremote
# is alias for:
git remote add newremote https://github.com/NewUser/repo
git fetch newremote
git checkout -b newremote-pr-branch newremote/pr-branch

git pushu

Push (and setup to track) to the same branch-name on origin.

# Alias for:
current_branch=$(git get-current-branch)
git push -u origin $current_branch

git re [flags/parameters]

Alias for rebase.
But if no -i or --interactive is given, it will pass the interactive flag along with a noop editor.
(this is to force autosquash, which for some reason doesn't run without interactive)

# Alias for (if interactive flag given):
git rebase [flags/parameters]

# Alias for (if no interactive flag given):
GIT_SEQUENCE_EDITOR=: git rebase -i [flags/parameters]

git rebase-from [inclusive_from_commit] [new_base]

Rebase the current branch onto [new_base] starting from (and including) [inclusive_from_commit].
(similar to git rebase [new_base] but only picking commits from HEAD up to [inclusive_from_commit])
(uses the re alias instead of rebase, to force autosquash)

# Alias for:
current_branch=$(git get-current-branch)
git re --onto [new_base] [inclusive_from_commit]~ $current_branch

git rebase-preserve [same-options-as-rebase]

Exactly the same as rebase, but preserves the author & committer dates of commits.

# Alias for:
git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE=\"%cD\" git commit --amend --no-edit' rebase [same-options-as-rebase]

# Usage:
git rebase-preserve -i --rebase-merges @~5

git rebun [interactve_command] [commit_hash]

Rebases a single commit (with --rebase-merges flag) without opening interactive editor.
[interactive_command] is the pick/edit/reword/fix etc. command in the interactive editor.

# Alias for:
git rebase -i --rebase-merges [commit_hash]~
# Then replacing the 'pick' of the oldest commit to [interactive_command]

# Example ussage:
git rebun edit HEAD~3
git rebun e 2ea1622
git rebun reword HEAD~4

git redoc

Commit using the last undone (via undoc alias) commit's message.

# Alias for:
git commit -c [last_undone_commit] --no-edit

git replace-with [branch]

Resets the head of current branch to that of [branch], then force deletes [branch].

# Alias for:
git reset --hard [branch]
git branch -D [branch]

git show-stopped [flags/parameters]

Shows the changes of the commit that the current rebase has stopped at.

# Alias for:
stopped_commit_hash="$(git get-stopped-hash)"
git show "$stopped_commit_hash" [flags/parameters]

git show-stopped-unmerged

Shows the changes of the commit that the current rebase has stopped at, specifically only for the currently unmerged files.

# Alias for:
unmerged_paths="$(git get-unmerged-paths)"
git show-stopped -- "$unmerged_paths"

git spoof-dates [author-date] [committer-date]

Changes the author/commiter dates of the previous commit.

# Alias for:
GIT_COMMITTER_DATE="[committer-date]" git commit --date="[author-date]" --amend --no-edit

# Usage:
git spoof-dates "Fri Dec 2 18:53:50 2022 +0800" "Fri Dec 2 18:54:09 2022 +0800"

git st [flags/parameters]

# Alias for:
git status [flags/parameters]

git stash-staged [flags/parameters]

Stashes staged changes, leaving behind unstaged changes.

# Alias for:
git stash-unstaged --quiet
git stash -u [flags/parameters]
git stash pop --quiet "stash@{1}"

git stash-unstaged [flags/parameters]

Stashes unstaged (and untracked) changes, leaving behind staged changes.

# Alias for:
git commit --quiet -m "TEMP (staged changes)"
git stash -u [flags/parameters]
git undoc

git undoc [-f]

Undo/Uncommit the last commit, keeping the commit's changes as staged.
(Force flag doesn't keep the commit's changes)

Also saves the last undone commit's hash to undoc_hash.temp to be used by the redoc alias.

# Alias for (w/o force flag):
git reset --soft HEAD~

# Alias for (force flag):
git reset --hard HEAD~

git unmerge

Must be on a merge commit.
Restore the deleted branch from the merge commit, and undo the merge commit.
(effectively undoing git merge-this)

# Alias for:
git restore-deleted-branch HEAD
git undoc -f
git checkout -

Helper functions

git branch-exists [branch]

Checks if branch [branch] exists.
(Used in conditional statements for other aliases)

# Alias for:
git show-ref --quiet "refs/heads/[branch]"

# Usage:
if git branch-exists my-branch; then
...

git get-current-branch

Gets current branch name.
(Used in other aliases)

# Alias for:
git rev-parse --abbrev-ref HEAD

git get-current-hash

Gets current commit hash.
(Used in other aliases)

# Alias for:
git rev-parse HEAD

git get-in-prog-cmd

If is currently rebasing, merging, cherry-picking or reverting, outputs rebase, merge, cherry-pick or revert respectively. If not, exit with error, and output "Not currently rebasing, merging or cherry-picking" to stderr.
(Used to infer the continue/abort/etc. commands for rebasing/merging/cherry-picking/etc)

# Alias for:
if git is-rebasing; then
    echo rebase
elif git is-merging; then
    echo merge
elif git is-cherry-picking; then
    echo cherry-pick
elif git is-reverting; then
    echo revert
else
    >&2 echo "Not currently rebasing, merging, cherry-picking nor reverting"
    exit 1
fi

git get-stopped-hash

Gets the hash of the commit that the current rebase has stopped at.
(Used in other aliases)

# Alias for:
cat .git/rebase-merge/stopped-sha

git get-unmerged-paths

Gets all the unmerged file paths, relative to the current dir.
For example, user is in /A/, unmerged file at /B/FILE, return ../B/FILE.
(Used in other aliases)

# Alias for:
git diff --name-only --diff-filter=U
# Then convert to be relative to current dir.

git has-unmerged

Checks if there's any unmerged paths, which somehow can occur when .git/MERGE_HEAD doesn't exist.
(eg. during merge conflicts after applying a stash)
(Used in git get-in-prog-cmd)

# Alias for:
git ls-files --unmerged | grep -q .

# Usage:
if git has-unmerged; then
...

git is-ancestor [older_commit/branch] [newer_commit/branch]

Checks if [older_commit/branch] is an ancestor of [newer_commit/branch].
(Used in conditional statements for other aliases)

# Alias for:
git merge-base --is-ancestor [older_commit/branch] [newer_commit/branch]

# Usage:
if git branch-exists origin/master HEAD; then
...

git is-cherry-picking

Checks if is currently cherry-picking, by checking if the .git/CHERRY_PICK_HEAD file exists.
(Used in git get-in-prog-cmd)

# Usage:
if git is-cherry-picking; then
...

git is-merging

Checks if is currently merging, by checking if the .git/MERGE_HEAD file exists.
(Used in git get-in-prog-cmd)

# Usage:
if git is-merging; then
...

git is-rebasing

Checks if is currently rebasing, by checking if the .git/rebase-merge folder exists.
(Used in git get-in-prog-cmd)

# Usage:
if git is-rebasing; then
...

git is-reverting

Checks if is currently reverting, by checking if the .git/REVERT_HEAD file exists.
(Used in git get-in-prog-cmd)

# Usage:
if git is-reverting; then
...

git restore-deleted-branch [merge_commit_hash]

Restores a locally-deleted branch from a merge commit, and returns [branch_name].

Infers the deleted [branch_name] from the merge commit's subject.
(expects the merge commit's subject to be in the form: "Merge branch '[branch_name]' ...")

# Alias for:
git branch [branch_name] [merge_commit_hash]^2
echo [branch_name]