Create Git aliases, And Git Commands You May Not Know

Harish Kumar · · 181 Views
Create Git aliases, And Git Commands You May Not Know

One of my favorite feature in Git is aliases. Git supports aliases, which implies you can provide your commands any name you need. I like to set aliases for long commands to avoid searching for them every time I need it.

The syntax for Git alias:

git config --global alias.[new_alias] "[git_command(s)]"

Let's create some useful Git aliases:

git recommit

git config --global alias.recommit 'commit --amend -m'

git commit --amend allows you to change the last commit message. Now this recommit command is less difficult and a lot simpler to remember.

# Change the last commit message with recommit
git recommit "New commit message"

git commend

git config --global alias.commend 'commit --amend --no-edit'

Commit amend with --no-edit flag permits you to commit the new changes in the repository with the last commit made, so you don't need to repeat the commit message again.

git search

git config --global 'grep'

# Example
git search [search_term]

git grep permits you to search in the repository for a keyword and it gives different matches. It is cool, yet I don't have a clue what grep means. Linux/Unix user may know what grep means but I think windows user might don't know about it. So, I prefer "search" instead because it is easy to remember and easy to use.

git here

git config --global '!git init && git add . && git commit -m "initial commit"'

Usually, when I initialize a new repository, I'll stage all the files and commit them with an initial commit message. git here does it all in one step.

git who

git config --global alias.who 'blame'

# Example
git who index.ts

git blame is utilized to look at the contents of a file line by line and see when each line was last modified and who made that modification. If there was a bug, in a line, you discover who did it and blame them.

git zip

git config --global 'archive --format=tar.gz -o ../repo.tar.gz'

# Example
git zip [branch_name]

The archive commands allow you to make tarballs or zips of your entire repository. git zip will make it simple to remember.

git newbie

git config --global alias.newbie 'checkout --orphan'

# Example
git newbie [new_branch_name]

git checkout with the --orphan flag permits you to make a branch with no any history from the parent branch.

git clonely

git config --global alias.clonely 'clone --single-branch --branch'

# Example
git clonely [branch_name] [remote_url]

git clonely v3 [email protected]:vuejs/vue-apollo.git

git clone with --single-branch --branch flags allows you to clone a specific branch from a repository.

git plg

git config --global alias.plg "log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative"

# Example
git plg # plg - Pretty Log

There isn't anything wrong with git log aside from that it is somewhat ugly, no color differences. Using this alias command, you'll get a very pretty log of everything.

git fresh

git config --global alias.fresh "filter-branch --prune-empty --subdirectory-filter"

# Example
git fresh [subfolder] [branch_name]
git fresh src main # Don't do this unless you know what you are doing

The series of commands that fresh replace is utilized to make another repository out of the contents of a subfolder. filter-branch with it many flags take the contents of a specified subfolder and replace the content in the whole repository with the content of the subfolder.

.gitconfig file

Now after creating all these aliases .gitconfig file will look like this:

    recommit = commit --amend -m
    commend = commit --amend --no-edit
    here = !git init && git add . && git commit -m \"Initialized a new repository\"
    search = grep
    who = blame
    zip = archive --format=tar.gz -o ../repo.tar.gz
    lonely = clone --single-branch --branch
    plg = log --graph --pretty=format:'%C(yellow)%h%Creset -%Cred%d%Creset %s %Cgreen| %cr %C(bold blue)| %an%Creset' --abbrev-commit --date=relative
    fresh = filter-branch --prune-empty --subdirectory-filter

Please login or create new account to add your comment.

You may also like:

Git Cheat Sheet - Commands

git init <directory>:  Create empty Git repo in the specified directory. Run with no arguments to initialize the current directory as a git repository.
Deepak Kumar

30+ Github Repos for JavaScript Developers

As the biggest stage for open source collaboration, Github is the ideal place for developers to work on resources related to JavaScript. A speedy hunt will yield JavaScript cheatsheets, (...)