Git cheat sheet

ยท

3 min read

Git cheat sheet

What is Git?

Git is a tool that helps you keep track of changes in your computer files. It's like a time machine for your work, allowing you to see what you did, go back to earlier versions, and work with others on the same project without messing things up.

Git Workflow

  • Working directory: The current state of files on your local machine before any changes are committed.
  • Staging area: A temporary area where changes are prepared before being committed to the local repository.
  • Commit: A snapshot of changes that is saved in the local repository with a unique identifier.
  • Local repository: A version control database on your local machine that stores committed changes and their history.
  • Remote repository: A version control database hosted on a remote server, often used for collaboration and backup.

Git configuration

# version check 
git --version git -v 

# Configure Git 
# Set the name 
git config --global user.name "navinkumar" 

# Set the email 
git config --global user.email "navinkumar@gmail.com" 

# Set the default editor  # here i set vscode code editor 
git config --global core.editor "code --wait" # optional step

# configuration: 
git config --list

Git Alias

# use "" if commmand have space
git config --global alias.lo "log --oneline"

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# To view the list of Git aliases that are currently set up on your system
git config --get-regexp alias

Starting a project

# Create a local repository
git init [project name]

# Make a local copy of the server repository
git clone [remote repo URL]

Local changes

# adding files to staging area
git add .
git add <file name>

# commit
git commit -m "commit message"

git add . && git commit -m "commit message"

Track changes / Inspecting

# Display the state of the working directory and the staging area
git status

# Track the changes that have not been staged:
git diff

# Track the changes that have staged but not committed:
git diff --staged

# Track the changes after committing a file:
git diff HEAD

# Track the changes between two commits:
git diff [commit1-sha] [commit2-sha]

# Git Diff Branches
git diff [branch 1][branch 2]

#To compare a file between two previous commits
git diff [commit1-sha]:file1.txt [commit2-sha]:file1.txt

Undoing changes

# reset commit or delete the commmit
git reset --soft [commitid sha]
git reset --hard [commitid sha] # not recommanded

# Revert a particular commit
git revert [commitid sha]
git revert --continue

Removing files

# Remove the files from the working tree and from the index
git rm
git rm [file name]

# Remove files from the Git But keep the files in your local repository
git rm --cached [file name]

Branch

# list the branch
git branch

# Switch between branches
git checkout [branch name]

# Create a new branch and switch to it
git checkout -b [branch name]

# Delete Branch
git branch -d [branch name]

# rename branch
git branch -m [old branch] [new branch]

Merging

# rebase
git rebase [branch 1] [branch 2]
git rebase --continue

# merge
git merge [branch 1] [branch 2]

# cherry pic
git cherry-pick [commitid-sha]

# stash
git stash
git stash pop

# To list history of stash
git stash list

# To pick specific stash commit
git stash pop [stash_commit id]

Remote

# Push data from remote server:
git push

# Pull data from remote server:
# Note: Before pushing data from your local repository, make sure to pull to avoid conflicts.
git pull

# set origin
# both push & pull
git remote set-url origin [URL]

# only for push
git remote set-url --push origin [URL]

# only for pull
git remote set-url --fetch origin [URL]

Did you find this article valuable?

Support navinkumar by becoming a sponsor. Any amount is appreciated!

ย