A UI client spoiled me with my first introduction to Git. Everything was magic. All that was required was a click here and a click there; I never understood what was going on. Don't get me wrong, I do not have anything against such tools, but at what point does one click become too many? For me personally, not having to reach for the mouse or trackpad is always a win. Another advantage to learning the command line is that it is a natural path to understand the underpinnings of Git. On this journey of discovery, I realized there are a few commands that I would almost always use due to the nature of Git flow we practice. Putting these commands together may help other users get a quicker grasp of the basics. Let's get started.
Make sure you have installed the latest Git. If you are on a Mac, like I am, you may want to override the Git that comes with Mac in order to get the latest version.
My preference is to use Homebrew:
brew install git
Keep an eye out on the install report. If you see something like:
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/git
This is because a conflict of existing Git came with your Mac. Follow the next steps:
Optional: You can remove the Mac Git with:
or just run:
brew link --overwrite git
You can confirm by checking:
Installing Git via Homebrew comes with a cool perk. This operation installs bash or zsh completion commands.
Alternatively, if it did not or you did not change your Git, you can install bash_completion via Homebrew like:
brew install bash-completion.
To get this working correctly, you want to edit your .bash_profie (or shell equivalent) to contain:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
And now you can use this to get hints and completions:
git <Tab> <Tab>
Just <Tab> <Tab> on any partial Git command to get all options or completions.
I. Create a Repository
The very first step in managing a repository would be to initialize and setup your source as a git repository.
Let’s assume you have a working project directory called my-awesome-project:
If you're starting with nothing:
git init my-awesome-project
This creates a new Git repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you'll run in a new project.
Your very first step may be cloning an existing remote repository:
git clone ssh://email@example.com/path/to/my-awesome-project.git
II. Create a Branch
After cloning a repository, the most common next step would be to create or checkout a desired branch before making changes.
To Create a New Branch
Having cloned or created a new repository, you are most likely checked out on the master branch. To create a new local branch from the current branch:
git checkout -b branch_name
To Checkout a Desired Branch
Let's figure out which branch to create our new branch from.
List all available remote branches:
git branch -r
If the list is too long, I like to pipe the output like so:
git branch -r | less
If I know what branch name to look for, then:
git branch -r | grep 'phase-4'
Once the desired branch is identified, let’s check this out in our local repository:
git checkout name_of_remote_branch
git checkout origin/release-18.06.01
You can also do the following:
git checkout release-18.06.01
This sets tracking to the origin branch automatically.
Now create/checkout the branch that you need from current branch:
git checkout -b branch_name
III. Saving changes
After making changes to the working files or directory:
List the changes in the folder:
Stage the file(s) to be committed to the staging index/directory:
git add path_to_filename
Add everything under the working directory:
git add .
Commit the changes (to commit history):
This opens up an editor to add comments and description
Commit the changes with a message added inline:
git commit -m 'comments for commit'
Stage all changes for tracked files and then commits with inline commit message:
git commit -am 'comments for commit'
I prefer the last, incase I may have forgotten to stage any recent changes by Git add.
Create your own power command to combine favorite but long Git commands.
e.g. Create and combine git add . && git commit -m 'commit message' into an alias or function in .bash_profile
IV. Undo Changes
If you are not happy, you can undo all the changes in the last commit.
If you simply want to change the message on the last commit:
git commit --amend
This opens up an editor to add comments and description.
git commit --amend -m ‘my new message’
If, however, you want to modify files and message in the last commit. Make the file changes and then run:
git add .
git commit --amend
To undo the last commit entirely and put everything into staging area:
git reset --soft
To undo the last commit entirely and unstage all contents from staging area:
git reset --mixed
To undo and remove all changes in the last commit completely:
git reset --hard
Keep in mind that this action will destroy all changes you have made.
To remove all untracked files:
git clean -nfd
This is a dry run mode. It safely shows you what is going to be removed without actually removing them.
git clean -fd
This removes all untracked files and directories.
To understand how Git manages state, look into the Three-Trees of Git.
V. Get Changes from Remote
This retrieves the branches or tags for all branches in the repository—along with objects necessary to complete their histories.
This is the same as git fetch, but it also does a git merge FETCH_HEAD in the end. Effectively merging with latest head of the current branch.
VI. Pushing Changes to Remote
If our current branch was only created locally and is not tracked by a remote equivalent, we can push this up by:
git push -u origin branch_name
If you're already tracking a remote branch:
VII. Visual Tool
Sometimes command line will not cut it when it comes to visualizing differences and historical context.
Here gitk—the first graphical repository browser—comes to our rescue.
To run simply type:
VIII. Git Aliases
Now that you are getting acquainted with the most common commands, you can save shortened forms of your most common commands:
$ git config --global alias.co checkout
Now to checkout just type:
$ git config --global alias.unstage 'reset HEAD --'
git unstage myFile
See more examples here.
If you follow Gitflow practice exactly, look into using the git-flow wrapper which will save you few steps. See gitflow-wrapper for more details.
So far, these have been the most basic commands I use for all normal needs.
In the next part, we shall get a little deeper with some Git concepts and more advanced commands.