Introduction

GitHub is a platform where you store your code online using a tool called Git. Git helps you track changes, work in teams, and manage code history. This cheatsheet explains important Git commands in simple terms.

1. Git Setup Commands

These commands help you set Git for the first time.

Bash
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

These set your identity for all Git commits on your machine.

Bash
git config --global core.editor code

This sets VS Code as the default editor.

Bash
git config --global --list

This shows your global Git config values.

2. Create or Clone a Repository

Create a new local repository

Bash
git init

Initializes Git tracking in your project folder.

Clone a remote repository

Bash
git clone https://github.com/user/repo.git

Downloads a GitHub repository to your local machine.

3. Basic Workflow

These are the steps for daily Git work.

Check status

Bash
git status

Shows which files are changed, staged, or untracked.

Add files to staging

Bash
git add filename

Stages a file to be committed.

Bash
git add .

Stages all changed files in the folder.

Commit changes

Bash
git commit -m "your message"

Saves a snapshot of the changes with a message.

4. Branching

Create a new branch

Bash
git branch branch-name

Switch to a branch

Bash
git checkout branch-name

Create and switch to a new branch

Bash
git checkout -b new-branch

List all branches

Bash
git branch

Delete a branch

Bash
git branch -d branch-name

Use -D instead of -d to force delete.

5. Merging and Rebase

Merge another branch into current branch

Bash
git merge branch-name

Combines changes from the target branch.

Rebase current branch onto another

Bash
git rebase branch-name

Moves your branch on top of another to make history cleaner.

6. Remote Commands

Check remotes

Bash
git remote -v

Shows which remotes are connected (like GitHub).

Add a new remote

Bash
git remote add origin https://github.com/user/repo.git

Push code to GitHub

Bash
git push origin branch-name

Sends your code to GitHub on the specified branch.

Pull latest code

Bash
git pull origin branch-name

Gets the latest code from GitHub and merges it.

7. Viewing History

Show commit history

Bash
git log

Shows a list of past commits.

Bash
git log --oneline

Shows commit history in one-line format.

8. Undoing Changes

Unstage a file

Bash
git reset filename

Undo last commit (keep changes)

Bash
git reset --soft HEAD~1

Undo last commit and remove changes

Bash
git reset --hard HEAD~1

9. Stash Changes

Save changes temporarily

Bash
git stash

See all stashes

Bash
git stash list

Apply latest stash

Bash
git stash apply

Remove applied stash

Bash
git stash drop

10. Tagging Versions

Add a tag

Bash
git tag v1.0

Push tags to GitHub

Bash
git push origin v1.0

11. .gitignore File

Tells Git to ignore certain files or folders.

Create a file named .gitignore and add file patterns like:

Bash
*.log
node_modules/
.env

12. Git Aliases (Optional but Useful)

Bash
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

Now you can use:

Bash
git co main
git ci -m "Message"

13. Working with Pull Requests (On GitHub)

  1. Push your branch to GitHub
    Bash
    git push origin feature-branch
  2. Go to GitHub and click Compare & pull request.
  3. Review your changes and click Create pull request.

14. Other Useful Commands

See difference before committing

Bash
git diff

See difference of staged files

Bash
git diff --cached

Blame line by line

Bash
git blame filename

Shows who last changed each line.

Show a specific commit

Bash
git show commit-id

15. Handling Mistakes

Recover a deleted file

Bash
git checkout HEAD filename

View file history

Bash
git log filename

16. Advanced Commands (for Interviews or Complex Projects)

Rebase interactively to edit history

Bash
git rebase -i HEAD~n

Edit, reorder, squash, or remove last n commits.

Clean untracked files

Bash
git clean -fd

Deletes all untracked files and folders.

Conclusion

This GitHub Cheatsheet is your quick guide for all important Git tasks. Once you use these commands a few times, they will become part of your daily workflow.

For teams or real projects, always remember: