Day 12: Git Cheat Sheet

Table of contents

Sure! Here's a Git cheat sheet covering each topic with 5-6 commands:

  1. Setup and Init:

    • git config --globaluser.name"Your Name": Set your name globally.

    • git config --globaluser.email"youremail@example.com": Set your email globally.

    • git init: Initialize a new Git repository in the current directory.

    • git clone <repository-url>: Clone an existing repository from a URL.

    • git remote add origin <repository-url>: Add a remote repository.

  2. Stage & Snapshot:

    • git add <file>: Stage changes in <file> for the next commit.

    • git reset HEAD <file>: Unstage changes in <file>.

    • git commit -m "Commit message": Commit staged changes with a message.

    • git commit --amend: Amend the last commit with new changes.

    • git restore <file>: Discard changes in <file>.

  3. Branch and Merge:

    • git branch: List all branches in the repository.

    • git branch <branch-name>: Create a new branch.

    • git checkout <branch-name>: Switch to <branch-name>.

    • git merge <branch-name>: Merge <branch-name> into the current branch.

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

  4. Inspect & Compare:

    • git status: Show the current status of the repository.

    • git log: Display commit history.

    • git diff: Show changes between commits, branches, or files.

    • git show <commit-id>: Show changes made in a specific commit.

    • git blame <file>: Show who made changes to each line of <file>.

  5. Share and Update:

    • git push: Upload local changes to a remote repository.

    • git pull: Fetch changes from the remote repository and merge them into the current branch.

    • git fetch: Fetch changes from the remote repository without merging.

    • git remote -v: List all remote repositories and their URLs.

    • git push -u origin <branch-name>: Push the current branch to the remote repository and set up tracking.

  6. Tracking Path Changes:

    • git add -u: Stage all changes to tracked files.

    • git clean -n: Show which files would be removed by git clean.

    • git mv <old-path> <new-path>: Move or rename a file and stage the change.

    • git rm <file>: Remove a file from the working directory and stage the deletion.

    • git diff --cached: Show changes staged for the next commit.

  7. Rewrite History:

    • git commit --amend: Amend the last commit.

    • git rebase <branch-name>: Rebase the current branch onto <branch-name>.

    • git reset --hard HEAD~<number>: Reset the current branch to <number> commits ago.

    • git reflog: Show a log of changes to the HEAD.

    • git cherry-pick <commit-id>: Apply changes from a specific commit to the current branch.

  8. Temporary Commits:

    • git stash: Temporarily save changes in the working directory.

    • git stash apply: Apply the most recent stash to the working directory.

    • git stash list: List all stashed changes.

    • git stash drop: Remove the most recent stash.

    • git stash clear: Remove all stashed changes.

  9. Ignore Patterns:

    • .gitignore: Create a file named .gitignore to specify patterns of files to ignore.

    • git check-ignore -v <file>: Check if <file> is ignored and by which rule.

    • git add -f <file>: Force-add an ignored file.

    • git update-index --assume-unchanged <file>: Mark <file> as unchanged.

    • git update-index --skip-worktree <file>: Ignore changes to <file>.

This cheatsheet covers a wide range of Git commands for various tasks.

0
Subscribe to my newsletter

Read articles from Yashraj Singh Sisodiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yashraj Singh Sisodiya
Yashraj Singh Sisodiya

I am Yashraj Singh Sisodiya, a 3rd Year CSE student at SVVV, born and raised in Shujalpur. Currently residing in Indore, I'm passionate about pursuing a career in DevOps engineering. My tech journey began with an internship at Infobyte, honing my skills as an Android Developer intern. Alongside my academic pursuits, I actively participate in co-curriculars, holding roles as Technical Lead at Abhyudaya and Cloud Lead at GDSC SVVV, while also serving as an MLSA of my college. I have a keen interest in Cloud Computing, demonstrated through projects such as User management and Backup using shell scripting Linux, Dockerizing applications, CI/CD with Jenkins, and deploying a 3-tier application on AWS. Always eager to learn, I'm committed to expanding my knowledge and skills in the ever-evolving tech landscape.