0% found this document useful (0 votes)
26 views11 pages

Git Command Line Cheat Sheet

This document summarizes commonly used Git commands for version control. It covers getting started with Git, making changes and committing, branching, tagging, viewing commit history, ignoring files, and some advanced tricks. The cheat sheet provides the basic syntax for commands like git init, git add, git commit, git branch, git log and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views11 pages

Git Command Line Cheat Sheet

This document summarizes commonly used Git commands for version control. It covers getting started with Git, making changes and committing, branching, tagging, viewing commit history, ignoring files, and some advanced tricks. The cheat sheet provides the basic syntax for commands like git init, git add, git commit, git branch, git log and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Git

This cheat sheet summarizes commonly used Git command line instructions for quick reference.

# Getting Started
Create a Repository

Create a new local repository

$ git init [project name]

Clone a repository

$ git clone git_url

Clone a repository into a specified directory

$ git clone git_url my_directory

Make a change

Show modified files in working directory, staged for your next commit

$ git status
Stages the file, ready for commit

$ git add [file]

Stage all changed files, ready for commit

$ git add .

Commit all staged files to versioned history

$ git commit -m "commit message"

Commit all your tracked files to versioned history

$ git commit -am "commit message"

Discard changes in working directory which is not staged

$ git restore [file]

Unstage a stagged file or file which is staged

$ git restore --staged [file]

Unstages file, keeping the file changes

$ git reset [file]


Revert everything to the last commit

$ git reset --hard

Diff of what is changed but not staged

$ git diff

Diff of what is staged but not yet commited

$ git diff --staged

Apply any commits of current branch ahead of specified one

Configuration

Set the name that will be attached to your commits and tags

$ git config --global [Link] "name"

Set an email address that will be attached to your commits and tags

$ git config --global [Link] "email"

Enable some colorization of Git output

$ git config --global [Link] auto

Edit the global configuration file in a text editor


Working with Branches

List all local branches

$ git branch

List all branches, local and remote

$ git branch -av

Switch to my_branch, and update working directory

$ git checkout my_branch

Create a new branch called new_branch

$ git checkout -b new_branch

Delete the branch called my_branch

$ git branch -d my_branch

Merge branchA into branchB

$ git checkout branchB


$ git merge branchA
Tag the current commit

Observe your Repository

Show the commit history for the currently active branch

$ git log

Show the commits on branchA that are not on branchB

$ git log branchB..branchA

Show the commits that changed file, even across renames

$ git log --follow [file]

Show the diff of what is in branchA that is not in branchB

$ git diff branchB...branchA

Show any object in Git in human-readable format

$ git show [SHA]

Synchronize

Fetch down all the branches from that Git remote


$ git fetch [alias]

Merge a remote branch into your current branch to bring it up to date

$ git merge [alias]/[branch]


# No fast-forward
$ git merge --no-ff [alias]/[branch]
# Only fast-forward
Transmit local branch commits to the remote repository branch

$ git push [alias] [branch]

Fetch and merge any commits from the tracking remote branch

$ git pull

Merge just one specific commit from another branch to your current branch

$ it h i k [ it id]
Remote

Add a git URL as an alias

$ git remote add [alias] [url]

Show the names of the remote repositories you've set up


$ git remote

Show the names and URLs of the remote repositories

$ git remote -v

Remove a remote repository

$ git remote rm [remote repo name]

Change the URL of the git repo

Temporary Commits

Save modified and staged changes

$ git stash

List stack-order of stashed file changes

$ git stash list

Write working from top of stash stack

$ git stash pop

Discard the changes from top of stash stack


Tracking path Changes

Delete the file from project and stage the removal for commit

$ git rm [file]

Change an existing file path and stage the move

$ git mv [existing-path] [new-path]

Show all commit logs with indication of any paths that moved

$ git log --stat -M

Ignoring Files

/logs/*

# "!" means don't ignore


!logs/.gitkeep

/# Ignore Mac system files


.DS_store

# Ignore node_modules folder


node_modules

# Ignore SASS config files


.sass-cache
A .gitignore file specifies intentionally untracked files that Git should ignore

# Git Tricks
Rename branch

Renamed to new_name

$ git branch -m <new_name>

Push and reset

$ git push origin -u <new_name>

Delete remote branch

Log

Search change by content

$ git log -S'<a term in the source>'

Show changes over time for specific file

$ git log -p <file_name>

Print out a cool visualization of your log


$ git log --pretty=oneline --graph --decorate --all

Branch

List all branches and their upstreams

$ git branch -vv

Quickly switch to the previous branch

$ git checkout -

Get only remote branches

$ git branch -r

Checkout a single file from another branch

$ git checkout <branch> -- <file>

Rewriting history

Rewrite last commit message

$ git commit --amend -m "new message"

See also: Rewriting history

Git Aliases
git config --global [Link] checkout
git config --global [Link] branch
git config --global [Link] commit
git config --global [Link] status

See also: More Aliases

Top Cheatsheet

Python Cheatsheet Vim Cheatsheet


Quick Reference Quick Reference

JavaScript Cheatsheet Bash Cheatsheet


Quick Reference Quick Reference

Common questions

Powered by AI

'git log' provides a history of commits which helps track how a project has evolved over time. Options like '--follow [file]' trace changes to specific files across renames, while '--pretty=oneline --graph --decorate --all' offers a visual representation of branch and merge history, aiding in understanding complex development paths and contributions .

Branches help isolate different features or bug fixes, allowing multiple contributors to work on separate tasks without interfering with the main codebase. Developers can create new branches for specific tasks using 'git checkout -b new_branch' and merge them into master branches when completed using 'git merge' after resolving conflicts. This isolates changes until they are stable and ready for integration .

'git rm' is used to delete a file from the working directory and stage the removal for commit simultaneously. This should be used when a file needs to be removed from the repository rather than just the local system to ensure the change is tracked and pushed to the remote repository in the next commit, maintaining consistency across environments .

'git stash' temporarily saves changes that aren't ready to be committed, allowing developers to switch branches or work on other tasks without losing their current work in progress. This is especially useful in maintaining a flexible workflow where unexpected tasks arise and developers need to shift focus quickly without committing incomplete changes .

Synchronization is achieved through commands like 'git fetch', which updates your local repository with the changes from the remote repository without merging them, 'git pull', which merges fetched changes into your current branch, and 'git push', which uploads local commits to a remote branch. This ensures that developers have the latest updates and avoid conflicts by reconciling local changes with the remote repository .

'git reset --hard' is used when a developer wants to discard all local changes, both staged and unstaged, and reset the repository to the last commit. This is appropriate in scenarios where changes are not needed, or the working directory is in an irrecoverable state. However, using it will permanently lose any changes not committed, so it should be used cautiously .

The '--no-ff' option prevents fast-forwarding, ensuring that a new commit is always created on merge. This is useful for maintaining a clear and recorded branch history, indicating where branches were merged. It should be used when you want to keep the complete historical context of development efforts even if fast-forwarding would naturally apply .

Setting a global user.name and user.email in Git configurations ensures that commits are properly attributed to the right individual, providing accurate commit history and accountability. This metadata is critical for traceability and collaboration within teams, allowing contributors to know who made specific changes .

Effective management involves regularly syncing local and remote branches using 'git fetch' and 'git pull', cleaning up obsolete branches with 'git branch -d' or 'git remote rm', and setting relevant upstream branches for synchronization. Mismanagement, such as confusion from unused branches or inconsistent updates, can lead to conflicts, outdated codebases, and disruptions in collaboration .

'.gitignore' files specify which files or directories Git should ignore, meaning they will not be tracked or included in commits. This is crucial for excluding unnecessary files such as system files, temporary files, or dependencies that do not need to be shared, thus streamlining repositories and reducing clutter .

You might also like