Git commands for everyday use

git_logo_compGit is a very advanced and powerful version control system. It supports too many features and it’s difficult to remember each and every command unless you are a heavy Git user or a Git dev yourself. I try not to remember too many of them. However, Git is used best when it is used from the cmdline. And in my experience, there are a few commands that are used much more frequently than others. I will list those in this article to have a reference for myself as well as for others. In the examples I will use GitHub’s own Atom project repository link for the examples.

Before I begin, here’s a good practice for a Git dev – always have a clean master branch checked-out from the original project’s master branch. Make your changes to a separate development branch and rebase your master and dev branch regularly with the main project branch. In other words, your changes should go into the project’s master branch from your dev branch. Those changes are then pulled into your master branch.

Git installation and initial setup
  • Install Git on Ubuntu
    $ sudo apt-get install git
  • Set your name, email address and default editor
    $ git config --global user.name "My Name"
    $ git config --global user.email "your-email@provider.com"
    $ git config --global core.editor vim
Add a project
  • Use GitHub’s web interface to fork your own master branch from the project’s main branch. Clone your mater branch locally.
    $ mkdir atom
    $ cd atom
    $ git clone https://github.com/username/atom.git
  • In case of very old or large repositories, you can do a shallow clone by fetching history of only latest n commits.
    $ git clone --depth 2 https://github/username/atom.git
  • Add the original Atom project as upstream.
    $ git remote add upstream https://github.com/atom/atom.git
  • Use the GitHub web interface to create a new branch named dev. This is where your own changes should go first.
  • Check-out (or switch to) the dev branch
    $ git checkout -b dev
The powerful checkout command
  • Check-out (switch to) a branch, tag, revision or revert changes to a file
    $ git checkout branch/tag/revision/filename
  • Revert all local changes
    $ git checkout -- .
Get upstream changes to your master branch
  • Switch to the master branch
    $ git checkout master
  • Check details of origin/upstream
    $ git remote show origin
    $ git remote show upstream
  • Retrieve the latest changes from upstream
    $ git fetch upstream
  • Merge upstream into your master branch
    $ git merge upstream/master
Get changes from your master to dev branch

A few words of merge and rebase first. Rule of the thumb is – use merge/rebase when you have no changes in your local dev branch. Both result in same commit history. However, if you have changes in your local dev branch but would like to get the changes in remote as well, then use rebase for a clean commit history.

  • Checkout dev branch
    $ git checkout dev
  • Merge or rebase depending on the situation
    $ git merge master
    $ git rebase master
  • Push the changes to your dev branch origin
    $ git push origin dev
Branch deletion
  • Delete a local branch
    $ git branch -D branch_name
  • Push the deletion to remote
    $ git push origin --delete branch_name
Other common scenarios
  • Check the branch and local status (maybe after a merge/rebase conflict)
    $ git status
  • Check and local modifications to tracked files
    $ git diff
  • Set diffuse as the diff tool
    $ git config --global diff.tool vimdiff
    $ git config --global difftool.prompt false
    $ git config --global alias.d difftool

    To view the diff in diffuse

    $ git d
    $ git d commit#1 commit#2
    $ git diff HEAD^ HEAD
  • Check commit history
    $ git log --stat
  • Hide local changes and pull, then unhide local changes
    $ git stash
    $ git pull
    $ git stash pop
  • Commit changes locally
    $ git commit -a -s
  • Push the changes to origin
    $ git push origin dev
  • Undo a commit, make changes, redo it
    $ git commit ...
    $ git reset --soft HEAD^
    //edit files
    $ git add ...
    $ git commit -c ORIG_HEAD
  • Edit/amend the most recent commit message
    $ git commit --amend -m "New commit message"

    If the commit is already pushed to remote

    $ git push origin dev --force
  • Clean local changes
    $ git reset --hard HEAD
    $ git clean -f -d //cleans all untracked files as well
  • Reset unmerged files
    $ git reset filename
    $ git checkout filename
  • Accept all changes post merge conflict occurs
    $ git reset --hard origin/dev
    $ git merge -X theirs master
  • If you have lost the URL of the remote repo you forked
    // if referential integrity has been broken:
    $ git config --get remote.origin.url
    // if referential integrity is intact:
    $ git remote show origin
Tagging

Tags are baselines you can revert back to in the future.

  • Get and list tags
    $ git pull --tags
    $ git tag
  • Create a tag
    $ git tag -a tag_name -m "your message here"
    $ git push origin --tags
  • Delete a tag
    $ git tag -d tag_name
    $ git push origin :refs/tags/tag_name

Another useful list is the giteveryday man page.

Feel free to suggest if you think I should add any command to the article.

One thought on “Git commands for everyday use”

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s