DEV Community

Cover image for Common Git commands and configurations used in a day-to-day workflow
Zean Qin
Zean Qin

Posted on • Updated on • Originally published at zean.be

Common Git commands and configurations used in a day-to-day workflow

I'm a command line lover and use Git from the terminal all the time.

If you're like me, you might start getting annoyed by some little things at a certain point. For example, it starts to get a bit annoying to always type two separate commands - git add <file> then git commit -m 'Your commit message' - to commit your changes. Or maybe you want to have a better looking git history when you type git log. Or you want your local branch to be automatically pruned when the remote branch has been deleted. Little things like these - you get the idea.

Over time, I have built up a curated list of commands, aliases and configrations that I use on a daily basis that makes my workflow more efficient and pleasant. And I'd like to share them with you below.

List your local branches and their remote tracking branches

In addition, this command also shows the hash code and the commit message of the latest commit. It also tells you if the remote branch has been deleted.

git branch -vv
Enter fullscreen mode Exit fullscreen mode

For example, running the command produces the following output on my machine,

git branch

Checkout a remote branch

With Git versions ≥ 1.6.6 and with only one remote, you can just do:

git fetch
git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

git checkout <branch_name> will NOT work in modern Git if you have multiple remotes. In this case use

git checkout -b <branch_name> <remote_name>/<branch_name>
Enter fullscreen mode Exit fullscreen mode

or the shorthand

git checkout -t <remote_name>/<branch_name>
Enter fullscreen mode Exit fullscreen mode

Add and commit in one command

Add either one of the following aliases to your global Git config file (usually at ~/.gitconfig on a Linux/Mac OS system). I prefer the second one because it saves a few more keystrokes.

# add a `add-commit` alias, or
git config --global alias.add-commit '!git add -A && git commit'

# add a `ac` alias to save a few more keystrokes
git config --global alias.ac '!git add -A && git commit'
Enter fullscreen mode Exit fullscreen mode

And use it with

git add-commit -m 'My commit message' # or
git ac -m 'My commit message'
Enter fullscreen mode Exit fullscreen mode

Delete a branch both locally and remotely

When you're done with a branch, you can delete it from both the remote and your local machine using the commands below.

# delete a remote branch
git push -d <remote_name> <branch_name> # or
git push -D <remote_name> <branch_name>

# delete a local branch
git branch -d <branch_name> # or
git branch -D <branch_name>
Enter fullscreen mode Exit fullscreen mode

Note that in most cases the name is origin.

Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status".

Delete all branches that have been merged in remote

Assume you have a long running development branch, and you branch off it to create different feature branches e.g. feature/A, feature/B.

After your peers have reviewed your pull requests for both features, merged them back to development and deleted them from remote. You can delete feature/A and feature/B from your local by running,

# switch to the development branch first
git checkout development

# delete local branches whose remote tracking branches have been merged back to development
git delete-merged
Enter fullscreen mode Exit fullscreen mode

You probably have noticed that delete-merged is not a Git command - it's actually an alias we set up. The parameters used in the actual command is different depending on your setup. But you can follow the following steps to construct a command that suits your needs.

Step 1: Check out the development branch.

Step 2: List all branches that have been merged into it in remote.

git branch --merged
Enter fullscreen mode Exit fullscreen mode

Step 3: You might see a few branches that you don't want to remove e.g. master, release etc. And you can filter down the list by excluding those branches by

git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)"
Enter fullscreen mode Exit fullscreen mode

The regular expression used by the egrep command basically means "all branches whose name starts with master, development or skip_branch_name will not be deleted".

You can modify the branches above or add your own branches that you don't want to delete.

Step 4: Delete all local branches that are already merged into the currently checked out branch

git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

Step 5: Set a global alias deleted-merged for the command

git config --global alias.delete-merged 'git branch --merged | egrep -v "(^\*|master|development|skip_branch_name)" | xargs git branch -d'
Enter fullscreen mode Exit fullscreen mode

Discard unstaged files in current working directory

To discard all unstaged files in current working directory,

git checkout -- .
Enter fullscreen mode Exit fullscreen mode

For a specific file, use

git checkout -- path/to/file/to/revert
Enter fullscreen mode Exit fullscreen mode

The -- is to remove argument ambiguation.

Rename a local and remote branch

Step 1: Rename your local branch

If you are on the branch you want to rename:

git branch -m <new-name>
Enter fullscreen mode Exit fullscreen mode

If you are on a different branch:

git branch -m <old-name> <new-name>
Enter fullscreen mode Exit fullscreen mode

Step 2: Delete the <old-name> remote branch and push the <new-name> local branch

git push origin :<old-name> <new-name>
Enter fullscreen mode Exit fullscreen mode

Step 3: Reset the upstream branch for the <new-name> local branch

Switch to the branch and then:

git push origin -u <new-name>
Enter fullscreen mode Exit fullscreen mode

Prettify the Git log

You can format your Git log to look like below and set an alias for it.

git lg

Step 1: Set up the following alias in your global Git config file

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
Enter fullscreen mode Exit fullscreen mode

Step 2: Append additional flags if needed

# filter by start date
--after="2016-01-31"
--since="2016-01-31"

# filter by end date
--before="2017-03-10"
--until="2017-03-10"

# filter by author
--author="Zean Qin"
Enter fullscreen mode Exit fullscreen mode

My most commonly used command is

git lg --after="yesterday" --author="Zean"
Enter fullscreen mode Exit fullscreen mode

Prettify Git diff

I use diff-so-fancy to make the diffs more readable. Follow the official setup steps and then just run

git diff
Enter fullscreen mode Exit fullscreen mode

Prune remote-tracking branches automatically for a branch on the other side that has already been deleted

Without git fetch --prune, remote-tracking branches for a branch
the other side already has removed will stay forever.

To always --prune for git fetch and git pull in all your Git repositories:

git config --global fetch.prune true
Enter fullscreen mode Exit fullscreen mode

To always --prune but from one single repository,

git config remote.origin.prune true
                 #^^^^^^
                 #replace with your repo name
Enter fullscreen mode Exit fullscreen mode

Make Git to use Vim as editor for writing commit messages

If you want to set the editor only for Git, do either (you don’t need both):

  • Set core.editor in your Git config: git config --global core.editor "vim"
  • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim

If you want to set the editor for Git and also other programs, set the standardized VISUAL and EDITOR environment variables:


Setting both is not necessarily needed, but some programs may not use the more-correct VISUAL.

export VISUAL=vim
export EDITOR="$VISUAL"
Enter fullscreen mode Exit fullscreen mode

References

  1. How do I delete a Git branch locally and remotely?
  2. How do I check out a remote Git branch?
  3. Git add and commit in one command
  4. How do I make Git use the editor of my choice for commits?
  5. How do I discard unstaged changes in Git?
  6. Rename a local and remote branch in git
  7. Good-lookin' diffs. Actually… nah… The best-lookin' diffs.
  8. Automatic prune with Git fetch or pull
  9. fetch: make --prune configurable
  10. How can I delete all Git branches which have been merged?

Top comments (2)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done.

I recently wrote a similar post on the top Git commands that you should know

Collapse
 
zeanqin profile image
Zean Qin

Thanks!