Comprehensive Git Commands Cheat Sheet

Git is a distributed version control system that tracks changes in source code during software development. This cheat sheet provides detailed reference commands for developers and DevOps engineers, covering everything from setting up repositories to managing advanced workflows like rebasing, cherry-picking, and reflog.

Git Setup

Before using Git, you need to configure your identity and initialize a repository.

  • Set username: git config --global user.name "Your Name"
  • Set email: git config --global user.email "your.email@example.com"
  • Initialize a repository: git init
  • Check configuration: git config --list

Cloning Repositories

Use git clone to copy a remote repository to your local machine.

git clone https://github.com/user/repo.git
  • Clone with specific branch: git clone -b branch_name https://github.com/user/repo.git
  • Clone with SSH: git clone git@github.com:user/repo.git

Branching

Branches in Git allow you to work on different features or fixes independently. Manage your branches with the following commands:

  • Create a new branch: git branch branch_name
  • Switch to a branch: git checkout branch_name
  • Create and switch to a new branch: git checkout -b branch_name
  • List all branches: git branch
  • Rename a branch: git branch -m old_branch_name new_branch_name
  • Delete a branch: git branch -d branch_name
  • Force delete a branch: git branch -D branch_name

Committing Changes

Committing captures a snapshot of changes in the repository. Make sure to commit related changes together and write meaningful commit messages.

  • Stage all changes: git add .
  • Stage specific files: git add file1 file2
  • Commit staged changes: git commit -m "Commit message"
  • Amend the last commit: git commit --amend -m "Updated commit message"
  • Skip the staging area: git commit -a -m "Commit message"

Merging Branches

Merging combines the changes from one branch into another. It's commonly used to integrate feature branches into the main branch.

  • Merge a branch into the current branch: git merge branch_name
  • Abort a merge: git merge --abort
  • Resolve merge conflicts: Manually edit the conflicted files, git add to mark as resolved, then git commit.

Rebasing

Rebase allows you to move or combine a sequence of commits from one branch onto another. It can create a cleaner commit history by applying changes on top of a branch, avoiding merge commits.

  • Rebase the current branch onto another branch: git rebase branch_name
  • Interactive rebase: git rebase -i HEAD~n (where n is the number of commits to rebase)
  • Abort a rebase: git rebase --abort
  • Continue after resolving conflicts: git rebase --continue

Cherry-Picking Commits

Cherry-pick allows you to apply a specific commit from one branch into another without merging the entire branch.

  • Cherry-pick a commit: git cherry-pick commit_hash
  • Continue after resolving conflicts: git cherry-pick --continue
  • Abort a cherry-pick: git cherry-pick --abort

Stashing Changes

Stash allows you to temporarily save your work without committing it, so you can switch branches or pull updates without losing your changes.

  • Stash current changes: git stash
  • List all stashes: git stash list
  • Apply the latest stash: git stash apply
  • Apply and remove the latest stash: git stash pop
  • Apply a specific stash: git stash apply stash@{n}
  • Drop a stash: git stash drop stash@{n}

Resetting Changes

Git reset allows you to undo changes by resetting the current state of the branch or working directory to a previous commit.

  • Soft reset (keep changes in working directory): git reset --soft HEAD~1
  • Mixed reset (unstage changes): git reset --mixed HEAD~1
  • Hard reset (discard changes in working directory): git reset --hard HEAD~1

Reflog and Recovering Lost Commits

Reflog is a powerful Git feature that records all changes made to the tips of branches and references, allowing you to recover commits even after they are deleted or reset.

  • View reflog: git reflog
  • Recover a lost commit: git reset --hard commit_hash

Git Best Practices

Following these best practices will improve your Git workflow, collaboration efficiency, and codebase management:

  • Write Descriptive Commit Messages: Ensure your commit messages are clear and describe the changes made.
  • Commit Often, Commit Small: Make frequent, small commits with related changes instead of large, infrequent commits.
  • Use Feature Branches: Create separate branches for features, bug fixes, or experiments and merge them back when ready.
  • Avoid Large Merge Conflicts: Regularly pull changes from the main branch to stay up-to-date and avoid major conflicts.
  • Review Code Before Merging: Use pull requests to review changes before integrating them into the main branch.