Skip to main content

Command Palette

Search for a command to run...

Day 12: Cheat-sheet for linux and git

#Day12 #DevOps #90daysofDevops #trainwithshubham #gitcheatsheet #linuxcheatsheet

Published
β€’9 min readβ€’View as Markdown
Day 12: Cheat-sheet for linux and git
P

πŸ‘‹ Hello, and welcome to my DevOps journey! πŸš€ I am Priyanka Varshney,πŸ› οΈ As an aspiring DevOps engineer, I'm all about bridging the gap between development and operations, making software delivery seamless and efficient. πŸ’»πŸ”§ On this Hashnode blog, I'll be sharing my learnings, experiences and adventures as I dive deep into the world of continuous integration, automation, and cloud technologies. β˜οΈβš™οΈ Let's connect, learn, and grow as a vibrant DevOps community. Follow my Hashnode blog, and let's embrace the DevOps adventure together! πŸ€πŸ”—

Linux Quick reference Cheat-sheet:

Linux is a free and open-source computer operating system that allows user to run programs and manage their computer's Hardware and software. It's like the brain of your computer, helping it perform tasks and allowing you to interact with it using a graphical interface or text commands. Linux is known for its stability, security, and flexibility.

1. File System Navigation:

pwd: Present working directory.

cd: Change the current directory to the specified directory.

ls: List files and directories.

ls -a: Lists files and directories including hidden files.

ls -l: Display detailed information about files and directories.

mkdir: Create a directory/folder.

rm: Remove a file.

rm -r: Remove the file and its content.

mv: Rename a file or move a file.

cp: Copy a file and directory.

2. File manipulation:

cat file: Display the contents of a file.

less file: View the contents of a file with pagination.

head file: Display the first few lines of a file.

tail file: Display the last few lines of a file.

3. File Permissions:

chmod permission file: Change the permissions of a file or directory.

chown user: group file: Change the owner and group of a file or directory.

4. Process Management:

ps: Display information about active processes.

kill process_id: Terminate a process with the specified process ID.

kill all process_name: Terminate all processes with the specified name.

top: Display real-time information about system processes.

bg: Put a process in the background.

fg: Bring a background process to the foreground.

5. Text Editing:

Nano file: Open a file for editing with the Nano editor.

vi file: Open a file for editing with the Vi editor.

grep pattern file: Search for a pattern in a file.

grep -r pattern directory: Recursively search for a pattern in files within a directory.

6. User Management:

useradd username: Create a new user account.

userdel username: Delete a user account.

passwd username: Set or change the password for a user account.

su username: Switch to another user account.

sudo command: Execute a command with superuser (root) privileges.

7. Package Management:

apt-get install package: Install a package using the Advanced Package Tool (APT) package manager.

apt-get update: Update the local package index to retrieve the latest package information.

apt-get upgrade: Upgrade all installed packages to their latest versions.

apt-get remove package: Uninstall a package.

apt-cache search keyword: Search for packages based on a keyword.

8. System Monitoring and Maintenance:

top: Monitor system processes and resource usage in real time.

htop: Interactive process viewer and system monitor.

du -sh directory: Display the total size of a directory.

df -h: Show disk space usage of mounted file systems.

reboot: Reboot the system.

shutdown: Shutdown the system.

9. System Information:

uname -a: Display detailed system information including the kernel version.

hostname: Print or set the system's hostname.

date: Display the current date and time.

10. System Information:

name: Print system information.

uname -a: Print detailed system information.

whoami: Display the current user.

df: Show disk space usage.

free: Display memory usage.

uptime: Show system uptime.

GIT Quick reference Cheat-sheet:

Git is a source code management(SCM) tool that handles small as well as large projects with efficiency. It is used to store repositories in remote servers such as Github.

1. Git configuration:

Configuring Git:

  1. Set User Name:

    • git config --global user.name "User name"

    • Sets the user's name globally.

  2. Set User Email:

Checking Configuration:

  1. Check Configuration Settings:

    • git config --list

    • Lists all Git configuration settings.

2. Starting a project

Initializing a Local Repository:

  1. Create a Local Repository:

    • git init

    • Initializes a new local Git repository.

Cloning a Remote Repository:

  1. Clone a Remote Repository:

    • git clone <repository-url>

    • Makes a local copy of a remote server repository.

3. Staging Changes:

Staging Changes:

  1. Stage a Specific File:

    • git add <Filename>

    • Adds a specific file to the staging area.

  2. Stage All Files in the Repository:

    • git add .

    • Adds all files in the repository to the staging area.

Committing Changes:

  1. Commit Changes with a Message:

    • git commit -m "Commit Message"

    • Records the staged changes permanently in the version history with a descriptive message.

4. Track changes:

Tracking Changes:

  1. View Unstaged Changes:

    • git diff

    • Tracks changes that have not been staged.

  2. View Staged Changes:

    • git diff --staged

    • Tracks changes that have been staged but not committed.

  3. View Changes After Committing:

    • git diff HEAD

    • Tracks changes after committing a file.

  4. View Changes Between Commits:

    • git diff <commit1> <commit2>

    • Tracks changes between two commits.

Checking Repository Status:

  1. Check Repository Status:

    • git status

    • Displays the state of the working directory and the staging area.

5. Commit History:

Viewing Commit History:

  1. View Recent Commits and Head Status:

    • git log

    • Displays the most recent commits and the status of the HEAD.

  2. View Commits as One Line Each:

    • git log --oneline

    • Displays commit history with one commit per line.

  3. View Modified Files in Commits:

    • git log --stat

    • Shows a summary of modified files in each commit.

  4. View Modified Files with Changes:

    • git log -p

    • Displays modified files along with their actual changes.

Viewing File Changes:

  1. Display Line Modifications in a File:

    • git blame <file name>

    • Shows the modification history for each line of a specific file.

6. Branching:

Branch Management:

  1. Create a Branch:

    • git branch <branch-name>
  2. List Branches:

    • git branch
  3. Delete a Local Branch:

    • git branch -d <branch-name>
  4. Delete a Remote Branch:

    • git push origin --delete <branch-name>
  5. Rename a Branch:

    • git branch -m <new-branch-name>

Branch Switching:

  1. Switch to a Branch:

    • git checkout <branch-name>
  2. Create and Switch to a New Branch:

    • git checkout -b <new-branch-name>
  3. Checkout a Remote Branch:

    • git checkout --track origin/<branch-name>

Stashing Changes:

  1. Stash Current Work:

    • git stash
  2. Stash with a Message:

    • git stash save "Your Message"
  3. List Stored Stashes:

    • git stash list
  4. Apply a Stash:

    • git stash apply
  5. Show Stash Contents:

    • git stash show
  6. Pop the Last Stash:

    • git stash pop
  7. Drop a Stash:

    • git stash drop
  8. Clear All Stashes:

    • git stash clear
  9. Stash Work on a Separate Branch:

    • git stash branch <branch-name>

Cherry-Picking Commits:

  1. Cherry-Pick a Commit:

    • git cherry-pick <commit-hash>

7. Merging:

Merging:

  1. Merge Branches:

    • git merge <branch-name>

    • Merges the specified branch into the currently active branch.

  2. Merge Specific Commit:

    • git merge <commit-hash>

    • Merges the changes from the specified commit into the currently active branch.

Rebasing:

  1. Rebase Branches:

    • git rebase <branch-name>

    • Applies a sequence of commits from the specified branch onto the current branch, creating a linear history.

  2. Continue Rebase:

    • git rebase --continue

    • Continues the rebasing process after resolving conflicts.

  3. Abort Rebase:

    • git rebase --skip

    • Aborts the rebasing process if needed.

Interactive Rebase:

  1. Interactive Rebase:

    • git rebase -i

    • Allows various operations like editing, rewriting, reordering, and more on existing commits interactively.

8. Remote:

Managing Remote Repositories:

  1. Check Remote Configuration:

    • git remote -v

    • Displays the configuration of the remote server, including its URL.

  2. Add a Remote:

    • git remote add <name> <url>

    • Adds a remote connection to the repository with a given name and URL.

  3. Fetch Data from Remote:

    • git fetch

    • Retrieves data and updates references from the remote server.

  4. Remove a Remote:

    • git remote rm <name>

    • Removes a remote connection from the repository by specifying its name.

  5. Rename a Remote:

    • git remote rename <old-name> <new-name>

    • Renames an existing remote to a new name.

  6. Show Remote Information:

    • git remote show <name>

    • Displays additional information about a particular remote, such as the remote's URL and tracking branches.

  7. Change Remote URL:

    • git remote set-url <name> <new-url>

    • Changes the URL of an existing remote connection.

9. Pushing Updates:

Pushing Changes:

  1. Push to Remote Repository:

    • git push origin master

    • Transfers the commits from your local repository's "master" branch to the remote server named "origin."

  2. Force Push to Remote Repository:

    • git push -f

    • Pushes the changes to the remote server forcefully, potentially overwriting history.

Deleting a Remote Branch:

  1. Delete a Remote Branch:

    • git push origin --delete <branch-name>

    • Deletes a remote branch named <branch-name> from the remote server specified as "origin."

10. Pulling updates:

Pulling Changes:

  1. Pull Data from Remote:

    • git pull origin master

    • Pulls data from the remote server's "master" branch and updates the local repository.

  2. Pull a Remote Branch:

    • git pull

    • Pulls changes from the currently tracked remote branch.

Fetching Data:

  1. Fetch Remote Repository:

    • git fetch <repository-url>

    • Downloads branches and tags from the specified remote repository URL.

  2. Fetch a Specific Branch:

    • git fetch <repository-url> <branch-name>

    • Fetches a specific branch from the remote repository.

  3. Fetch All Branches Simultaneously:

    • git fetch --all

    • Fetches all branches and tags from the remote repository.

  4. Synchronize the Local Repository:

    • git fetch origin

    • Synchronizes the local repository with the remote repository named "origin" by fetching all available branches and tags.

11. Undo changes:

Reverting Changes:

  1. Undo the Last Commit:

    • git revert HEAD

    • Undoes the last commit by creating a new commit that reverses the changes made in the last commit.

  2. Revert a Particular Commit:

    • git revert <commit-hash>

    • Creates a new commit that undoes the changes introduced by the specified commit.

Resetting Changes:

  1. Hard Reset to a Specific Commit:

    • git reset --hard <commit-hash>

    • Resets the current branch and working directory to the specified commit, discarding all changes.

  2. Soft Reset to a Specific Commit:

    • git reset --soft <commit-hash>

    • Resets the current branch to the specified commit while keeping the changes in the working directory as uncommitted.

  3. Mixed Reset to a Specific Commit:

    • git reset --mixed <commit-hash>

    • Resets the current branch to the specified commit while keeping the changes in the working directory but not staged for commit.

12. Removing files:

Removing Files:

  1. Remove Files from the Working Tree and Index:

    • git rm <file-name>

    • Removes the specified file from both the working tree and the Git index (staging area).

  2. Remove Files from Git but Keep Locally:

    • git rm --cached <file-name>

    • Removes the specified file from Git's tracking but retains it in your local repository. This is useful when you want to stop tracking a file while keeping it in your working directory.

Thank you for reading :-)

Priyanka varshney

More from this blog

Untitled Publication

39 posts