Useful GIT Commands and Aliases

Share this article

Here is a reference list of some useful GIT commands which I use including GIT Aliases commands. If you know of or use any which are not here please post a comment so I can add them to help others! :)

GIT Bash

GIT Bash for Windows (formerly known as GitWin) is shell command line tool for Windows users. It is essentially a Windows port of GNU bash “Minimalist GNU for Windows” (see msysgit projects website for more details). Download GIT Bash

Useful GIT Commands

Show all branches.
$ git branch --all
git-branch-all Load GITK (Generalized Interface Toolkit) Good for seeing what changes have been made and who by in a nice interface.
$ gitk
gitk Add (if applicable) and commit changes with a message.
git commit -a -m "msg"
Search the tree contents for specific phrase and filetype. ie find the word “CSS” in all .js files.
git grep "css" -- *.js
Create a zipped backup of the current branch.
$ git archive --format=zip master^>backup-10-04-2013.zip
View local GIT config.
$ cat .git/config

Useful GIT Alias Commands

GIT Alias Commands can save you heaps of time typing in long-winded commands into your GIT CMD. They basically map a long command into whatever shorthand version you wish. They also can be configured to prettify/colorify your results. Official post on git alias commands. Also see: Must Have Git Aliases: Advanced Examples.
Show a pretty git log history.
$ git config --global alias.history "log --abbrev-commit --pretty=oneline --graph --decorate"
usage: $ git history
git-history Show the last commit.
$ git config --global alias.last 'log -1 HEAD'usage: $ git last
usage: $ git last
Reset to state of your last commit.
$ git config --global alias.resetlast 'reset --hard HEAD'
usage: git resetlast

Frequently Asked Questions (FAQs) about Git Commands

What is the purpose of using Git commands?

Git commands are used to perform specific operations in Git, a distributed version control system. These commands allow developers to track changes in their code, collaborate with other developers, and manage different versions of their projects. They are essential tools for software development and version control.

How can I create Git aliases for commonly used commands?

Git allows you to create aliases for your most frequently used commands. This can save you time and make your workflow more efficient. To create an alias, use the ‘git config’ command followed by the alias name and the command you want to alias. For example, if you frequently use the ‘git commit’ command, you could create an alias like this: ‘git config –global alias.ci commit’. Now, instead of typing ‘git commit’, you can just type ‘git ci’.

How can I use Gitk to understand Git?

Gitk is a graphical user interface for Git. It provides a visual representation of your repository, making it easier to understand the history and structure of your project. To use Gitk, simply type ‘gitk’ in your command line. This will open the Gitk interface, where you can explore your repository, view commit history, and more.

What is the difference between ‘git fetch’ and ‘git pull’?

git fetch’ and ‘git pull’ are both used to update your local repository with changes from a remote repository. However, they work in slightly different ways. ‘git fetch’ only downloads the changes, but does not merge them into your current branch. On the other hand, ‘git pull’ downloads the changes and automatically merges them into your current branch.

How can I undo a Git commit?

If you need to undo a commit in Git, you can use the ‘git revert’ command. This command creates a new commit that undoes the changes made in the specified commit. For example, to undo the last commit, you would use the command ‘git revert HEAD’.

How can I view the history of my Git repository?

You can view the history of your Git repository using the ‘git log’ command. This command displays a list of all the commits made in the repository, along with information about each commit, such as the author, date, and commit message.

How can I resolve merge conflicts in Git?

Merge conflicts occur in Git when two or more developers modify the same part of a file and then try to merge their changes. To resolve a merge conflict, you need to manually edit the conflicted file, decide which changes to keep, and then commit the resolved file. Git provides several tools to help with this process, including ‘git diff’ to view the differences between the conflicting changes, and ‘git mergetool’ to visually resolve conflicts.

How can I clone a Git repository?

To clone a Git repository, use the ‘git clone’ command followed by the URL of the repository. This command creates a copy of the repository on your local machine, allowing you to work on the project without affecting the original repository.

How can I switch to a different branch in Git?

To switch to a different branch in Git, use the ‘git checkout’ command followed by the name of the branch. For example, to switch to a branch named ‘feature’, you would use the command ‘git checkout feature’.

How can I delete a branch in Git?

To delete a branch in Git, use the ‘git branch -d’ command followed by the name of the branch. For example, to delete a branch named ‘feature’, you would use the command ‘git branch -d feature’. Note that you cannot delete a branch if you are currently on that branch. You must switch to a different branch before you can delete it.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week