The most basic Git commands you should know (Git Cheat Sheet)
The most basic Git commands you should know (Git Cheat Sheet)

The most basic Git commands you should know (Git Cheat Sheet)

Date
Apr 5, 2023
Tags
programming
basics

Git

Version Control System to track the history of changes made to a project.

Basic Git Commands:

  1. git init initializes a brand new Git repository and begins tracking an existing directory.
  1. git clone creates a local copy of a project that already exists remotely.
  1. git add stages a change. Any changes that are staged will become a part of the next snapshot and a part of the project's history.
  1. git commit saves the snapshot to the project history and completes the change-tracking process.
  1. git status shows the status of changes as untracked, modified, or staged.
  1. git branch shows the branches being worked on locally.
  1. git merge merges lines of development together.
  1. git pull updates the local line of development with updates from its remote counterpart.
  1. git push updates the remote repository with any commits made locally to a branch.
 
When you git clonegit fetchgit pull, or git push to a remote repository using HTTPS URLs on the command line, Git will ask for your GitHub username and password. When Git prompts you for your password, enter your personal access token.

Stage and snapshot

git status shows modified files in working directory, staged for your next commit
git add [file] adds a file to your next commit
git reset [file] unstages a file while retaining the changes in working directory
git diff shows differences of what is changed but not staged
git diff --staged shows differences of what is staged but not committed
git commit -m “[descriptive message]” commits your staged content as a new commit snapshot
 

Branch and Merge

git branch shows the branches being worked on locally.
git branch [branch-name] creates a new branch at the current commit.
git checkout switches to another branch and check it out into your working directory
(more details about git checkout: https://git-scm.com/docs/git-checkout)
git merge [branch] merges the specified branch’s history into the current one
git log shows all commits in the current branch’s history
 

Inspect and compare

git log shows the commit history for the currently active branch
git log branchB..branchA shows the commits on branchA that are not on branchB
git log --follow [file] shows the commits that changed file, even across renames
git diff branchB...branchA shows the differences of what is in branchA that is not in branchB
git show [SHA] show any object in Git in human-readable format
 

Share and update

git remote add [alias] [url] adds a git URL as an alias
git fetch [alias]fetches down all the branches from Git remote
git merge [alias]/[branch]merges a remote branch into your current branch to bring it up to date
git push [alias] [branch]transmits the local branch commits to the remote repository branch
git pull fetches and merge any commits from the tracking remote branch
 

Temporary commits

git stash saves modified and staged changes
git stash listlists stack-order of stashed file changes
git stash popwrites working from top of stash stack
git stash dropdiscards the changes from top of stash stack

Getting a repository on your local machine

Two ways to obtain a git repository:
  • Initializing a repository in an existing directory - take a local directory that is currently not under version control, and turn it into a Git repository.
  • You can clone an existing Git repository
 
The 1st method: Initializing a repository in an existing directory
Go to the directory you want cd /your_directory type git init
This creates a new subdirectory: .git that contains all of your necessary repository files but nothing is tracked yet. To begin tracking those files, do an initial commit:
git add *
git commit -m 'Initial commit'
 
The 2nd method: Cloning an existing repository:
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
If you want to clone the repository into a directory with a different name, specify the new directory name as an argument
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY myproject
 
 
 

References: