Git Cheat Sheet: Essential Commands and Tips

blank
Git cheat sheet

Git is a powerful version control system that helps developers manage code changes, collaborate with teams, and maintain project history. Whether you’re a beginner or an experienced developer, this Git cheat sheet will save you time and effort.


Essential Git Commands

1. Repository Setup

CommandDescription
git initInitializes a new Git repository in the current directory.
git clone <repository-url>Clones a remote repository to your local machine.
git remote add origin <repository-url>Adds a remote repository URL (e.g., GitHub) to your local repo.
git remote -vLists all remote repositories linked to your local repo.

2. Basic Workflow

CommandDescription
git statusShows the status of your working directory (untracked, modified, or staged files).
git add <file>Stages a specific file for commit.
git add .Stages all changes in the working directory.
git commit -m "commit message"Commits staged changes with a descriptive message.
git push origin <branch>Pushes local commits to a remote repository.
git pull origin <branch>Fetches and merges changes from a remote repository to your local branch.

3. Branching and Merging

CommandDescription
git branchLists all local branches.
git branch <branch-name>Creates a new branch.
git checkout <branch-name>Switches to the specified branch.
git checkout -b <branch-name>Creates and switches to a new branch.
git merge <branch-name>Merges the specified branch into the current branch.
git branch -d <branch-name>Deletes a local branch.
git push origin --delete <branch-name>Deletes a remote branch.

4. Viewing History and Changes

CommandDescription
git logDisplays the commit history.
git log --onelineShows a simplified commit history (one line per commit).
git diffShows unstaged changes in the working directory.
git diff <file>Shows changes in a specific file.
git diff <commit1> <commit2>Compares changes between two commits.
git show <commit>Displays details of a specific commit.

5. Undoing Changes

CommandDescription
git restore <file>Discards changes in the working directory for a specific file.
git restore --staged <file>Unstages a file but keeps changes in the working directory.
git reset --hardDiscards all local changes and resets to the last commit.
git revert <commit>Creates a new commit that undoes the changes of a specific commit.
git commit --amendModifies the most recent commit (e.g., to update the commit message).

Advanced Git Commands

1. Stashing Changes

CommandDescription
git stashTemporarily saves changes in the working directory.
git stash listLists all stashed changes.
git stash applyApplies the most recent stashed changes.
git stash popApplies and removes the most recent stashed changes.
git stash dropDeletes the most recent stash.

2. Rebasing

CommandDescription
git rebase <branch>Reapplies commits from the current branch onto another branch.
git rebase --continueContinues a rebase after resolving conflicts.
git rebase --abortAborts an ongoing rebase.

3. Tagging

CommandDescription
git tag <tag-name>Creates a lightweight tag for the current commit.
git tag -a <tag-name> -m "message"Creates an annotated tag with a message.
git push origin <tag-name>Pushes a specific tag to the remote repository.
git push origin --tagsPushes all tags to the remote repository.

4. Submodules

CommandDescription
git submodule add <repository-url>Adds a submodule to your repository.
git submodule update --init --recursiveInitializes and updates submodules.

Common Git Challenges and Solutions

ChallengeSolution
Accidentally committed to the wrong branchUse git stash to save changes, switch to the correct branch, and apply the stash.
Merge conflictsResolve conflicts manually in the affected files, then use git add and git commit to complete the merge.
Lost commit historyUse git reflog to find the lost commit and reset to it.
Large files in the repositoryUse git filter-branch or tools like BFG Repo-Cleaner to remove large files from history.

FAQs About Git

What is the difference between git pull and git fetch?

git fetch downloads changes from the remote repository but does not merge them.
git pull downloads changes and automatically merges them into your current branch.

How do I squash multiple commits into one?

Use git rebase -i HEAD~<number-of-commits> and mark commits as squash in the interactive editor.

Can I recover a deleted branch?

Yes, use git reflog to find the commit hash of the deleted branch and then run git checkout -b <branch-name> <commit-hash>.

How do I ignore files in Git?

Create a .gitignore file in your repository and list the files or patterns you want to ignore.


Pro Tips for Using Git

  1. Write meaningful commit messages: Use clear and concise messages to describe changes.
  2. Use branches for new features: Always create a new branch for feature development to avoid disrupting the main branch.
  3. Regularly pull changes: Sync your local repository with the remote repository to avoid merge conflicts.
  4. Leverage Git hooks: Automate tasks like linting or testing using Git hooks.
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
blank

Terraform Cheat Sheet: Quick Reference Guide

Next Post
Ansible Cheatsheet

Ansible Cheat Sheet: Quick Reference Guide

Related Posts