Skip to content

RizwanAhmedIT/git-basic-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Git Cheat Sheet

Table of Contents

Setup

  • Configure user information for all repositories:
    git config --global user.name "Your Name"
    git config --global user.email "your_email@example.com"
    

Starting a Project

  • Initialize a new Git repository:
    git init
  • Clone an existing repository:
    git clone <repository_url>
  • Create a new repository on GitHub:
    1. Go to GitHub and log in to your account.
    2. Click on the "+" icon in the upper right corner and select "New repository".
    3. Enter the repository name and description.
    4. Choose to make it public or private.
    5. Click "Create repository".

Adding Code to a New Repository

  1. Initialize the repository:
    git init
  2. Add the remote repository:
    git remote add origin <repository_url>
  3. Add files to the staging area:
    git add <file>
    git add .  # Add all files
  4. Commit the files:
    git commit -m "Initial commit"
  5. Push the changes to the remote repository:
    git push -u origin main

Basic Snapshotting

  • Check the status of your repository:
    git status
  • Add files to the staging area:
    git add <file>
    git add .          # Add all files
  • Commit changes:
    git commit -m "Commit message"
  • Amend the last commit:
    git commit --amend

Branching and Merging

  • 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>
  • Merge a branch into the current branch:
    git merge <branch_name>
  • Delete a branch:
    git branch -d <branch_name>
  • Delete a branch (force):
    git branch -D <branch_name>

Remote Repositories

  • Add a remote repository:
    git remote add origin <repository_url>
  • Fetch from the remote repository:
    git fetch
  • Push changes to the remote repository:
    git push origin <branch_name>
  • Pull changes from the remote repository:
    git pull
  • Set the remote branch for the current branch:
    git branch --set-upstream-to=origin/<branch_name>

Inspecting and Comparing

  • Show commit history:
    git log
  • Show commit history with diffs:
    git log -p
  • Show a specific commit:
    git show <commit_hash>
  • Compare branches:
    git diff <branch_name>
  • Compare staged changes:
    git diff --staged

Undoing Changes

  • Unstage a file:
    git reset <file>
  • Revert a commit (create a new commit that undoes changes):
    git revert <commit_hash>
  • Reset to a specific commit:
    git reset --hard <commit_hash>
  • Stash changes:
    git stash
  • Apply stashed changes:
    git stash apply
  • List stashes:
    git stash list

Working with Tags

  • Create a tag:
    git tag <tag_name>
  • Push tags to remote:
    git push origin --tags
  • Delete a local tag:
    git tag -d <tag_name>
  • Delete a remote tag:
    git push origin --delete tag <tag_name>

Advanced Commands

  • Rebase a branch:
    git rebase <branch_name>
  • Interactive rebase:
    git rebase -i <commit_hash>
  • Cherry-pick a commit:
    git cherry-pick <commit_hash>

Collaboration

  • Fork a repository:
    • Go to the repository page on GitHub/GitLab and click on the "Fork" button.
  • Create a pull request:
    • Go to your forked repository, switch to the branch you want to merge, and click on the "New pull request" button.

Cleaning Up

  • Remove untracked files:
    git clean -f
  • Remove untracked files and directories:
    git clean -fd

Summary Commands

  • Show the summary of changes:
    git status
    git diff
    git log

This cheat sheet should cover most of the commands you'll need to manage your Git repositories effectively.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published