DEV Community

V Sai Harsha
V Sai Harsha

Posted on • Updated on

Git Cheatsheet

Git is a powerful distributed version control system used for tracking changes in source code during software development. Whether you're new to Git or looking for a quick reference, this cheat sheet provides essential Git commands and concepts.

Table of Contents

  1. Configuring Git
  2. Creating and Cloning Repositories
  3. Basic Git Workflow
  4. Branching and Merging
  5. Checking Status and History
  6. Staging and Committing
  7. Undoing Changes
  8. Collaboration and Remote Repositories
  9. Advanced Git

Configuring Git

Configure User Information

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Enter fullscreen mode Exit fullscreen mode

Set Default Text Editor

git config --global core.editor "nano"  # Set your preferred text editor
Enter fullscreen mode Exit fullscreen mode

Creating and Cloning Repositories

Initialize a New Repository

git init
Enter fullscreen mode Exit fullscreen mode

Clone a Repository

git clone repository_url
Enter fullscreen mode Exit fullscreen mode

Basic Git Workflow

Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

Update Local Repository

git pull origin branch_name
Enter fullscreen mode Exit fullscreen mode

Push Changes to Remote

git push origin branch_name
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Create a New Branch

git checkout -b new_branch
Enter fullscreen mode Exit fullscreen mode

Switch Branches

git checkout branch_name
Enter fullscreen mode Exit fullscreen mode

Merge Branch into Current Branch

git merge branch_name
Enter fullscreen mode Exit fullscreen mode

Delete a Branch

git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode

Checking Status and History

View Changes in Files

git diff
Enter fullscreen mode Exit fullscreen mode

View Commit History

git log
Enter fullscreen mode Exit fullscreen mode

View Remote Branches

git remote -v
Enter fullscreen mode Exit fullscreen mode

Staging and Committing

Stage Changes for Commit

git add filename
Enter fullscreen mode Exit fullscreen mode

Commit Changes

git commit -m "Descriptive message"
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

Discard Changes in Working Directory

git checkout -- filename
Enter fullscreen mode Exit fullscreen mode

Unstage Changes

git reset filename
Enter fullscreen mode Exit fullscreen mode

Undo the Last Commit (Keep Changes)

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Undo the Last Commit (Discard Changes)

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Collaboration and Remote Repositories

Add a Remote Repository

git remote add remote_name repository_url
Enter fullscreen mode Exit fullscreen mode

Fetch Changes from a Remote Repository

git fetch remote_name
Enter fullscreen mode Exit fullscreen mode

Create a Pull Request

After pushing your branch to a remote repository, create a pull request via the repository's web interface.

Review and Merge Pull Request

Review and merge pull requests on the repository's web interface.

Advanced Git

Rebase Commits

git rebase branch_name
Enter fullscreen mode Exit fullscreen mode

Resolve Merge Conflicts

Edit conflicted files, then commit changes.

Tag a Commit

git tag tag_name
Enter fullscreen mode Exit fullscreen mode

Cherry-Pick Commits

git cherry-pick commit_hash
Enter fullscreen mode Exit fullscreen mode

Amend the Last Commit

git commit --amend
Enter fullscreen mode Exit fullscreen mode

This Git cheatsheet covers essential Git commands and concepts for everyday use. Refer to it as you work with Git to manage version control in your software development projects.

Estimated Reading Time: 5 minutes

Top comments (0)