Essential Git Commands That You Must Know

Sep 7, 2022 - 7 minute read | Logan Murphy

History

Git is a version control system created by Linus Torvalds in 2005. He made Git to develop the Linux kernel. Other programmers contributed to its initial development and its core maintainer is now Junio Hamano. Git is available for free under the GPL-2.0-only license.

Torvalds joked about the name Git which means “unpleasant person” in British English slang: “I’m an egotistical person, and I name all my projects after myself. First ‘Linux’, now ‘Git’“.

Vocab

  • scm - Source Control Manager
  • vc - Version Control
  • working tree - current content of the repository
  • stage - changes prepared to be committed
  • commit - staged changes that have been recorded in change history

Installation

Windows - Chocolatey

#!/bin/bash
choco install git

Mac - Homebrew

#!/bin/bash
brew install git

Linux

#!/bin/bash
sudo apt-get install git

Installer

If none of these methods work for you check out the Git website for installation steps.

When you are happy with your changes

You want to be able to save your code once you get everything working.

add

Allows you to stage changes in your working tree.

#!/bin/bash
# Stage all file changes
git add --all
git add -A
git add .
# Add specific files
git add *.html
git add index.html
git add output.log -f # for ignored files
# Stage files chunk by chunk
git add -p

commit

Formally records your staged changes in history with the specified message.

#!/bin/bash
# Commit staged files
git commit -m "a message describing the change"
# Stage and commit changes - only works for tracked files
git commit -a -m "a message describing the change"
# If you did not like your last message
git commit --amend -m "New commit message"

When you are unhappy with your changes

You want to be able to go back to a better state.

reset

Un-stages your files. It can also permanently undo any uncommitted changes as well as delete entire commits. Be careful using this command as you can permanently lose your changes if used improperly.

#!/bin/bash
# Unstage changes
git reset
git reset path/file.ext
# Undo all uncommitted changes
git reset --hard
# Advanced - Pop a commit - You should use revert instead to maintain history
git reset HEAD~
git reset HEAD~2 # Pops the last 2 commits

clean

Removes untracked files from the working tree

#!/bin/bash
# Remove all untracked files and folders
git clean -fd

stash

Allows you to save changes for another time. You can restore these changes at a later time.

#!/bin/bash
# Add files to a stash
git stash
# Apply the latest stash
git stash pop
# Show stashes
git stash list

revert

Creates a commit in history that undoes the changes of another commit.

#!/bin/bash
# Undo a commit
git revert [commit-sha]

restore

Copies a file from another revision. This is a newer command that replaced the previously overloaded checkout command.

#!/bin/bash
git restore --source [branch-name] [path/file.ext]
git checkout [branch-name] -- [path/file.ext]

When you want to know the state of your repository

Use these commands to give you some insight.

log

Lists all commits on the current branch, including who, when, and what they did.

#!/bin/bash
# List all logs
git log
# List the last 10 logs
git log -10

status

Lists the names of changed, new, and deleted files in the working tree.

#!/bin/bash
# List all untracked, unstaged, and staged file changes
git status

diff

Lists the specific changes of tracked files in the working tree.

#!/bin/bash
# List all tracked but unstaged changes
git diff

When you want to start a new feature

You do not want to affect the quality of existing code or progress of new code.

branch

Creates, deletes, or lists a new line of history for making commits.

#!/bin/bash
# List all branches available
git branch
# Create a new branch
git branch [branch-name]

switch

Replaces the use of the overloaded checkout command when switching to a new branch.

#!/bin/bash
# Switch to a new branch
git switch [branch-name]
git checkout [branch-name]
# Create and switch to a new branch
git switch -c [branch-name]
git checkout -B [branch-name]

When you have completed a new feature

You need to combine your changes with existing features.

merge

Brings your changes into another branch as a single commit.

#!/bin/bash
# Merge another branch into the current branch
git merge [branch-name]

rebase

Brings your changes into another branch using the original commit changes. This command better preserves history.

#!/bin/bash
# Rebase another branch on top of the current branch
git rebase [branch-name]

cherry-pick

Let’s you pick each commit you want to pull from another branch. You should use this command rarely.

#!/bin/bash
# Add the specified commit by sha
git cherry-pick [commit-sha]

When you want to sync with a remote repository

You can update both your local and remote repositories.

pull

Gets and applies the latest code on the server.

#!/bin/bash
# Get the latest code from the server and apply them
git pull [remote-name] [branch-name]

push

Updates the latest code on the server. It may require a pull first.

#!/bin/bash
# Add the latest code to the server
git push [remote-name] [branch-name]
# First time pushing a new branch
git push -u origin main
# Advanced - Overwrite the history of a remote branch - This should almost never be done
git push -f origin main

fetch

Caches the latest code on the server. The changes can then be applied later.

#!/bin/bash
# Fetch the latest code from the server
git fetch [remote-name] [branch-name]

When you want to start a new repository

You have a few options for configuring your set-up.

init

Creates a bare-bones repository that you can immediately start committing to.

#!/bin/bash
# Start a new repository
git init

clone

Creates a repository from a remote server.

#!/bin/bash
# Copy the contents of a remote repository
git clone [clone-url]

remote

Specifies where remote resources should be stored.

#!/bin/bash
# Set the push url
git remote add [remote-name] [remote-url]

.gitignore

This is actually a file, not a command. It is very important in maintaining the size of your repository. This file would list out files and wildcards in order to prevent generated files (like logs and shared packages) from being accidentally committed.

When you get a new computer

You need to do a few things before getting started.

config

#!/bin/bash
# Set your global author information
git config --global user.name "Logan Murphy"
git config --global user.email "logan.murphy@domain.com"
# Set your repository author information
git config user.name "Logan Murphy"
git config user.email "logan.murphy@domain.com"

When you are stuck

The entire Pro Git book, written by Scott Chacon and Ben Straub and published by Apress, is available here. All content is licensed under the Creative Commons Attribution Non Commercial Share Alike 3.0 license.

help

Can list all of the Git documentation.

#!/bin/bash
# List general help
git help
# List help on a specific command
git help [command-name]

There is much more to Git than these commands, but if you master these you will have not trouble navigating the version control space with Git. Alternatively, you can consider hiring a Git expert like myself using a freelancing hiring platform like Toptal.

HackingIntoCoding.com © 2024
Logan Murphy