Skip to main content

Command Palette

Search for a command to run...

Git and Linux Cheat-sheet 🚀🐧✨

Published
•7 min read•View as Markdown
Git and Linux Cheat-sheet 🚀🐧✨

⚡Git Cheat-sheet

git init # Create a new Git repository.
git status # View the status of files in the working directory.
git add filename.txt # Add files to the staging area.

git commit -m "added new files"  # Commit all changes
git commit -m "done" filename.txt  # Commit specific file

git restore filename.txt  # Restore changes from the last commit
git checkout filename.txt  # Restore changes from the last commit
git rm --cached filename.txt  # Removes file from the staging area

git diff filename.txt  # View changes to a specific file
git diff  # View changes in the working directory
git diff --cached  # View changes in the staging area

git config --global user.name "<your username>" # Configure user information for Git commits.
git config --global user.email "<your email>"

git log  # View commit history
git log --oneline  # View concise commit history
git log --oneline --pretty  # View formatted commit history

git show <commit-hash>  # Display information about a specific commit, including changes.

git branch  # View existing branches.
git branch dev  # Create branch

git checkout -b dev  # Create and switch to branch
git checkout dev  # Switch to the 'dev' branch.

git remote -v  # View information about remote branches.

git clean -f  # Remove untracked files.
git clean -fd  # Remove untracked directories.

git push origin --delete <branch_name>  # Delete a remote branch named <branch_name>.
git merge dev  # Merge branch
git rebase dev  # Rebase branch
git fetch  # Retrieve changes without merging
git pull origin dev  # Fetch and merge changes
git revert <commit_id>  # Revert changes from a specific commit.

git reset --soft <commit_id>  # Soft reset i.e it reverts the changes and deletes the commit history
git reset --hard <commit_id>  # Hard reset i.e it reverts changes and deletes the commit history and changes/deletes the file from working/staging area

git restore --staged <filename>  # Unstage changes in the working directory
# Create or edit .gitignore to ignore specified files i.e whatever is written in this, it ignores those files for adding to the staging area.
git show  # Display information about the latest commit on the current branch along with the exact changes done.

git cherry-pick <commit_id>  # Apply a specific commit
git cherry-pick --continue  # Continue cherry-pick after resolving conflicts

git stash  # Stash changes
git stash pop  # Apply stashed changes and remove from the stash list
git stash apply  # Apply stashed changes
git stash apply stash@{2}  # Apply a specific stash

git rebase -i HEAD~3  # Squash commits into a single commit.
git branch <branchname> <commit-hash>  # Restore a deleted branch.
git clone <repository URL>  # Clone entire repository to the local repo.
# Create a personal copy of a repository under your account on platforms like GitHub.

git remote add origin <repository URL>  # Connect local and remote repo
git remote -v  # Show remote repositories
git remote set-url origin <new_repository_URL>  # Change the URL of the remote repository.

git push origin master  # Push changes from the local repository to the remote repository.
git clean  # Clean untracked files.

⚡Linux Cheat-sheet

ls  # List files and directories in the current directory.
touch <filename>  # Create a new empty file named <filename>.
pwd  # Print the current working directory.
uname  # Print system information.
whoami  # Print the username.
mkdir <directoryname>  # Create a new directory named <directoryname>.

cd ~  # Change directory to the home directory.
cd /  # Change directory to the root directory.
cd <directory>  # Change directory to <directory>.

sudo useradd -m <username>  # Create a new user named <username>.
sudo passwd <username>

su <username>  # Switch to the <username> user.

cp <sourcefile> <destinationfolder>  # Copy files from <sourcefile> to <destinationfolder>.

mv <filename> <destinationfolder>/  # Move <filename> to <destinationfolder>/ 
mv <old_filename> <new_filename>  # Rename file <old_filename> to <new_filename>

ls -l  # List files with details.
ls -al  # List all files, including hidden ones.

echo "<text>"  # Display "<text>" on the console.
echo "<text>" > <filename>  # Redirect "<text>" to <filename>.
echo "<text>" | tee -a <filename>  # Display "<text>" on the console and append to <filename>.

cat <filename>  # Display the content of <filename>.

vi <filename>  # Open the Vi/Vim editor for <filename>.

rm <filename>  # Remove <filename>.
rm -r <foldername>  # Remove <foldername> and its contents recursively.

man <command>  # View the manual/documentation for the <command>.

sudo cat /etc/passwd  # View all users in Linux OS.
sudo cat /etc/group  # View all groups in Linux OS.

groupadd <groupname>  # Create a group named <groupname>.

sudo userdel <username>  # Delete a user.
sudo groupdel <groupname>  # Delete a group.
sudo gpasswd -a <username> <group>  # Add <username> to the <group>.
sudo gpasswd -d <username> <group>  # Delete <username> from the <group>.

groups <username>  # View all groups where <username> is a member.

sudo chgrp <newgroup> <filename>  # Change the group of <filename> to <newgroup>.
sudo chown :<newgroup> <filename>  # Change the group of <filename> to <newgroup>.
sudo chmod <permissions> <filename>  # Change the file permission of <filename>.

ssh -i "<keypair>" <user>@<hostname>  # Connect to an EC2 instance using SSH.
scp -i "<keypair>" <localfile> <user>@<hostname>:<remotefolder>  # Copy <localfile> from local to server.
scp -i "<keypair>" <user>@<hostname>:<remotefile> <localfolder>  # Copy <remotefile> from server to local.

sudo apt-get update  # Update the local package database.
sudo apt-get upgrade  # Upgrade the installed packages.
sudo apt install <package>  # Install the <package>.
sudo apt remove <package>  # Remove the <package>.
sudo apt purge <package>  # Purge the <package>.

grep <pattern> <file>  # Search for <pattern> in <file>.
grep -i <pattern> <file>  # Case-insensitive search(i.e lower and upper case) for <pattern> in <file>.
grep <pattern> <inputfile> > <outputfile>  # Search for <pattern> in <inputfile> and save the output to <outputfile>.
grep <pattern> <inputfile> >> <outputfile>  # Append the output of grep <pattern> in <inputfile> to <outputfile>.

awk '{print $<column1>,$<column2>}' <file>  # Print columns <column1> and <column2> from <file>.
awk '{print NR,$1,$2}' <file>  # Print line numbers along with columns 1 and 2.
awk '/<pattern>/ {print $<column1>,$<column2>}' <file>  # Search for <pattern> in <file> and print columns <column1> and <column2>.
awk '{print NR,$1,$2}' <file> | head -<num>  # Print the first <num> lines of columns 1 and 2 from <file>.
awk '{print NR,$1,$2}' <file> | tail -<num>  # Print the last <num> lines of columns 1 and 2 from <file>.
awk 'NR>=<start> && NR<=<end> && /<pattern>/ {print $<column1>,$<column2>,$<column3>}' <file>  # Print columns <column1>, <column2>, and <column3> for rows <start> to <end> where entries match <pattern>.
awk '$<timecolumn> > "<starttime>" && $<timecolumn> < "<endtime>" {print $<column2>,$<column3>,$<column4>,$<column5>}' <file>  # Print columns <column2>, <column3>, <column4>, and <column5> for rows where column <timecolumn> is between <starttime> and <endtime>.
awk '$<timecolumn> > "<starttime>" && $<timecolumn> < "<endtime>" && /<monthpattern>/ {print $<column2>,$<column3>,$<column4>,$<column5>}' <file>  # Print columns <column2>, <column3>, <column4>, and <column5> for rows where column <timecolumn> is between <starttime> and <endtime> and entries match <monthpattern>.

find <directory> -name <filename>  # Search for <filename> in <directory>.
find <directory> -name '*.<extension>'  # Search for files with the extension .<extension> in <directory>.
find <directory> -user <username>  # Search for files owned by the user <username> in <directory>.
find <directory> -group <groupname>  # Search for files under the group <groupname> in <directory>.

#!/bin/bash  # First line of a shell script.
read <variable>  # Read input and store it in the variable <variable>.
echo "hello my name is $<variable>"  # Display the value of the <variable>.
chmod 700 <scriptname.sh>  # Give execute permission.
./<scriptname.sh>  # Execute a shell script.
bash <scriptname.sh>  # Execute a shell script without giving execute permission.

getfacl <filename>  # Display the access control list of <filename>.
setfacl -m u:<user>:<permissions> <filename>  # Set ACL for <filename> to give <permissions> to the user <user>.

tar -cvf archive.tar file1 file2 directory1  # Create an archive named archive.tar with file1, file2, and directory1.
tar -xvf archive.tar  # Extract files from archive.tar to the current directory.
tar -tvf archive.tar  # List contents of archive.tar without extracting.
tar -czvf archive.tar.gz file1 file2 directory1  # Create a gzip-compressed archive.
tar -cjvf archive.tar.bz2 file1 file2 directory1  # Create a bzip2-compressed archive.
tar -xzvf archive.tar.gz  # Extract from a gzip-compressed archive.
tar -xjvf archive.tar.bz2  # Extract from a bzip2-compressed archive.
tar -rvf archive.tar newfile  # Append newfile to the existing archive.
tar --exclude='file3' -cvf archive.tar file1 file2 directory1  # Create an archive excluding file3.