ā¤ Like
šŸ”– Save
šŸ”— Share
Eric Hu
Eric Hu

Essential Git Commands

@thedevspaceio

Git is the part of the software development cycle that is often overlooked.

It is not just a utility tool for saving code, but a powerful system for tracking changes, collaborating with others, and managing the history of your project.

This cheatsheet covers the essential commands that every developer should know.

āœ… Configuration āœ… Creating repositories āœ… Staging and committing āœ… Branching āœ… Merging and rebasing āœ… Remote repositories āœ… Inspecting history āœ… Undoing changes

#git #versioncontrol #commands #branching #merge #commit #github #webdev #coding #tips


Git is a version control tool that tracks the changes you make to your code. You can create branches, stage changes, and collaborate with others.

CommandDescription
git configRead and write Git settings such as user identity, defaults, and aliases.
git initCreate a new Git repository in the current directory.
git cloneCopy a remote repository (or a specific branch) to your local machine.
git statusShow which files are modified, staged, or untracked.
git addMove changes from the working directory to the staging area.
git commitSave a snapshot of staged changes to the repository history.
git branchManage branches: list, create, rename, or delete them.
git checkoutSwitch between branches or create a new one.
git mergeCombine the history of another branch into the current branch.
git rebaseReplay your commits on top of another branch for a linear history.

CommandDescription
git remoteManage connections to remote repositories.
git fetchDownload changes from a remote without merging them.
git pullDownload and merge changes from a remote branch.
git pushUpload local commits, branches, or tags to a remote.
git logBrowse the commit history, optionally as a compact graph.
git showInspect the changes introduced by a specific commit.
git diffCompare changes between the working directory, staging area, or commits.
git restoreUnstage files or discard uncommitted changes.
git revertUndo a commit by creating a new commit that reverses it.
git resetMove the branch pointer to a previous commit, optionally discarding changes.
git stashTemporarily shelve uncommitted changes and restore them later.
git tagMark a specific commit, typically for a release.

Configuration

Set your identity and default editor.

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

Check your configuration.

bash
git config --list

Creating repositories

Initialize a new Git repository in the current directory.

bash
git init

Clone an existing repository.

bash
git clone https://github.com/user/repo.git

Clone a specific branch.

bash
git clone -b branch-name https://github.com/user/repo.git

Staging and committing

Check the status of your working directory.

bash
git status

Stage files for the next commit.

bash
git add filename.txt
git add .

Commit staged changes with a message.

bash
git commit -m "Add new feature"

Stage and commit in one command.

bash
git commit -am "Fix typo"

Branching

List branches.

bash
git branch
git branch -a

Create a new branch.

bash
git branch feature-branch

Switch to a branch.

bash
git checkout feature-branch

Create and switch in one command.

bash
git checkout -b feature-branch

Rename the current branch.

bash
git branch -m new-name

Delete a branch.

bash
git branch -d feature-branch
git branch -D feature-branch

Merging and rebasing

Merge a branch into the current branch.

bash
git merge feature-branch

Rebase the current branch onto another.

bash
git rebase main

Continue rebasing after resolving conflicts.

bash
git rebase --continue

Abort an in-progress rebase.

bash
git rebase --abort

Remote repositories

List remote connections.

bash
git remote -v

Add a remote repository.

bash
git remote add origin https://github.com/user/repo.git

Fetch changes from a remote.

bash
git fetch origin

Pull changes from a remote branch.

bash
git pull origin main

Push changes to a remote branch.

bash
git push origin main

Push a local branch for the first time.

bash
git push -u origin feature-branch

Inspecting history

Show commit history.

bash
git log
git log --oneline

Show a compact graph of branches.

bash
git log --oneline --graph --all

Show changes in a specific commit.

bash
git show commit-hash

Show changes between working directory and staging area.

bash
git diff

Show changes already staged for commit.

bash
git diff --staged

Undoing changes

Unstage a file.

bash
git restore --staged filename.txt

Discard changes in the working directory.

bash
git restore filename.txt

Amend the last commit.

bash
git commit --amend -m "Updated message"

Revert a commit by creating a new commit.

bash
git revert commit-hash

Reset to a previous commit.

bash
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1

Stashing

Temporarily save changes without committing.

bash
git stash

List stashes.

bash
git stash list

Apply the most recent stash.

bash
git stash pop

Apply a stash without removing it.

bash
git stash apply

Tags

Create a tag for a release.

bash
git tag -a v1.0.0 -m "Version 1.0.0"

Push tags to a remote.

bash
git push origin --tags

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io