You write code every day, right? Why not write Git commands like a code instead of clicking over to your GUI tool?
OK, you need some time to familiarize yourself, but once it happens, you’ll be more efficient than ever.
I’m going to show you some basic Git commands and their features. This isn’t another Git documentation, though.
git status command shows what the full status is, where the
HEAD is, and what the changes are (staged, not staged, untracked files).
Once you’re familiar with the long output, you might use the
git status --short to see the minified result:
Manipulate the Changes
Here’s a list of some commands that allow you to manipulate the changes:
git add file— adds the
filefile to the stage section
git restore file— discards changes to the
git restore --staged file— removes the
filefrom the stage section
file may be replaced with any file or directory path you want to modify.
Inspecting the diffs
git diff— displays the not-staged changes in your working tree
git diff --staged— displays the staged changes
git diff dev— displays changes between the working tree and the
devbranch. You might use a commit hash or tag instead.
git diff master..dev— displays changes between the branches
You might pass a path to a file or directory as an additional argument to filter the diffs to a specified path.
If you only want to inspect the file names, use the
--name-only switch. If you need to know which files were modified, added, or deleted, use the
Once you have some staged changes, use
git commit -m 'Commit message'.
If you don’t use the
-m switch, it opens the default text editor. Don’t be surprised if it’s VIM. Save the file, and close the window in order to complete the commit.
You can modify the default editor in the config like:
git config --global core.editor your-editor-executable-path
Use Wildcards for the add/restore/switch/diff Commands
For example, you only want to add all of the TypeScript files from the
src dir to be staged for commit:
git add src/**/*.ts
Discard all unstaged changes from the
git restore test/
Discard all of the staged changes (move to the unstaged section) starting from the current directory:
git restore --staged .
It supports a glob pattern, so you have great control over choosing the elements. For the testing purposes, you can use an
-n argument also to dry-run the command.
Interactively Choose Hunks
-p switch (or
--patch) to interactively choose hunks of code you want to patch using the add (reset or restore, as well) command. You decide which hunk (or an entire file) to add. The
-p switch turns the output into:
The switch command can be used to switch between branches.
git switch branch-name— switches to a
git switch -b abc— creates branch
abcand switches to it
git switch -— switches you to the previous branch
git checkout [args]— works as a
switchbut can also move HEAD to any commit/tag as well*
restore are quite recent commands meant to provide a better interface for the well-known multi-purpose
Here is some article with details if you want to dive in:
git branch— returns a list of the local branches.
-rto list only remote branches or
-ato get them all
git branch abc— creates
git branch -d abc— deletes
abcif already merged.
If not, use
-Dto force deletion.
git log to see the commits’ history.
There are a lot of options here, like:
- Display the changes alongside the commit description:
git log -p
nlatest commits from your current position:
git log -2 # display two recent commits
- Display log for a specific branch/revision:
git log branch-name
- Filter log for a specific file(s) only (glob pattern applies here):
git log *.js # or git log branch-name:*.js
- Filter commits by the author (accepts partial name):
git log --author wiktor
- Filter by date:
git log --since 2019-10-01
- Filter by date using dynamic date ranges (days, weeks, months, years):
git log --since 2months --until 1week
You don’t need to type the entire Git command every time. There’s some basic setup for the aliases. Just paste it into your
br = branch
st = status
rt = restore
sw = switch
ci = commit
d = diff
dc = diff --cached
l = log
last = l -1 # it's possible to reuse existing alias
Now, you can simply use the
git st instead of
git status, etc.
It’s possible to add alias from the command line as well. For example setting
l alias for the
git config --global alias l log.
Aliases in action:
git merge abc — merges the
abc branch into yours.
Sometimes you don’t want to include the commits’ history when merging the changes. There’s an option to squash all of the changes into one commit.
git merge --squash your-branch
It results in staged changes in the target branch so you can still modify it before merging.
Setup SSH Keys
To access/modify your repository from the external server, you have to be authorized.
You can either use your credentials over HTTPS or set up your SSH keys that provide a typing-less password. SSH keys have numerous advantages over passwords like:
- Better security, as it’s extremely unlikely to brute force or guess
- Provides a typing-less password (until the key is created without a passphrase)
In order to create the SSH key pair, use the
ssh-keygen -t rsa -b 4096
It creates an RSA key with a key size of 4096 bits for better security. It generates the keys in
~/.ssh directory by default. Please remark the private key
id_rsa is secret and must not be shared.
Then, send the public key
~/.ssh/id_rsa.pub to your GitHub account.
There’s an official tutorial for that:
Adding a new SSH key to your GitHub account
To configure your GitHub account to use your new (or existing) SSH key, you'll also need to add it to your GitHub…
From now on, you can access and write to the repository without a password.
Please note it applies for GitLab, Bitbucket, or anything else also. The process should look pretty similar.
You definitely must try a text-mode Git tool called tig. It facilitates browsing changes in the repository. Look at the screenshot:
This was just a simple example of using the basic Git commands. Each of the commands is so comprehensive you’d have to write an article (or series) for each of them separately. The manual of the
log command only consists of 2300+ lines.
git log --help | wc -l
I hope you enjoyed reading!