iTsanak / Git-Commands

Git commands cheat sheet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Git-Commands

Git commands cheat sheet

Git Basics

Setup git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

Create a New Repository

git init

Clone an Existing Repository

git clone <repository-url>

#Working with Changes

Check Status

git status

Add Changes

git add <file>
git add .           # Add all changes

Commit Changes

git commit -m "Commit message"

Remove Files

git rm <file>

Move/Rename Files

git mv <old-name> <new-name>

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

git checkout <target-branch>
git merge <source-branch>

Delete a Branch

git branch -d <branch-name>

Remote Repositories

Add a Remote Repository

git remote add <remote-name> <remote-url>

Fetch Changes from Remote

git fetch <remote-name>

Push Changes to Remote

git push <remote-name> <branch-name>

Pull Changes from Remote

git pull <remote-name> <branch-name>

Inspecting Changes

Show Commit History

git log

Show Changes in Files

git diff

Show a Specific Commit

git show <commit-hash>

Stashing Changes

Stash Changes

git stash

Apply Stashed Changes

git stash apply

List Stashes

git stash list

Resetting and Reverting

Undo Changes in Working Directory

git checkout -- <file>

Reset to Last Commit

git reset --hard HEAD

Revert a Commit

git revert <commit-hash>

Tagging

Create a Tag

git tag <tag-name>

Push Tags to Remote

git push <remote-name> --tags

Collaboration Tips

Update Local Repository

git pull origin main

Push Your Branch

git push origin <branch-name>

Create a Pull Request (PR)

  1. Push your branch to the remote repository.
  2. Go to the repository on GitHub.
  3. Click "Compare & pull request".
  4. Add a description and click "Create pull request".

Advanced Commands

Interactive Rebase

git rebase -i <commit-hash>

Squash Commits

git rebase -i HEAD~<number-of-commits>

Cherry Pick a Commit

git cherry-pick <commit-hash>

About

Git commands cheat sheet