Skip to content

Git is your version control best friend, and GitHub is where collaboration magic happens! Below is a guide to the most useful Git & GitHub commands you’ll use daily. Let’s master the flow of commits, branches, and merges! πŸš€

Notifications You must be signed in to change notification settings

shihabshahrier/Git-GitHub-Command-Cheat-Sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 

Repository files navigation

πŸ™ Git & GitHub Command Cheat Sheet

Master Git and GitHub with this comprehensive cheat sheet. From basics to advanced tricks, this guide has everything you need to manage and collaborate on code like a pro! πŸš€


🌟 Getting Started with Git

1. Check Git Status

git status

πŸ›‘ View modified, untracked, or staged files in your working directory.

2. Initialize a Git Repository

git init

πŸ“‚ Turn a project directory into a Git-tracked repository.

3. Clone a GitHub Repository

git clone <repository_url>

πŸ“₯ Copy a remote repository to your local machine.


πŸ“€ Making Changes Locally

4. Add Files to Staging Area

git add <file_name>  # Add a specific file
git add .            # Add everything

πŸ“‚ Prepare changes for commit.

5. Commit Changes

git commit -m "Your message"

πŸ’Ύ Save staged changes with a descriptive message.

6. Unstage Files

git reset <file_name>

🚧 Remove files from staging without discarding changes.


🌳 Branching & Merging

7. Create and Switch to a Branch

git checkout -b <branch_name>

🌿 Create and move to a new branch in one step.

8. Merge a Branch

git merge <branch_name>

πŸ”€ Combine changes from another branch into your current branch.

9. Delete a Branch

git branch -d <branch_name>

πŸ—‘οΈ Remove a branch you no longer need.


🌐 Syncing with GitHub

10. Push Changes

git push origin <branch_name>

🌐 Upload your local branch to the remote repository.

11. Pull Updates

git pull origin <branch_name>

πŸ“₯ Fetch and merge changes from the remote branch.


πŸ”§ Advanced Git Commands

12. Rebase a Branch

git rebase <branch_name>

πŸͺ„ Move your branch to start from the latest changes in another branch.

Example Workflow:

  1. Switch to the feature branch: git checkout feature-branch
  2. Rebase onto main: git rebase main

✨ This creates a cleaner commit history by replaying your commits on top of the target branch.


13. Squash Commits

git rebase -i HEAD~<number_of_commits>

πŸ“¦ Combine multiple commits into one to simplify history.

Example Workflow:

  1. Use git rebase -i HEAD~3 to squash the last 3 commits.
  2. Mark commits as squash or s in the interactive editor.
  3. Save and exit to merge the commits.

14. Cherry-Pick a Commit

git cherry-pick <commit_hash>

πŸ’ Apply a specific commit from one branch to another.

Example Workflow:

  1. Find the commit hash using git log.
  2. Apply it to your branch: git cherry-pick abc1234.

15. Stash Changes Temporarily

git stash

🧳 Save your changes for later without committing.

Retrieve Stashed Changes:

git stash pop

πŸͺ„ Apply and remove the latest stash.


16. Amend the Last Commit

git commit --amend -m "Updated commit message"

✏️ Edit the last commit message or add files you forgot to include.


πŸ” Tracking and Troubleshooting

17. View Commit History

git log
git log --oneline

πŸ“œ View detailed or one-line commit history.

18. Check File Changes

git diff <file_name>

πŸ” Compare changes in a specific file.

19. Find the Commit That Introduced a Bug

git bisect start
git bisect bad
git bisect good <commit_hash>

🐞 Automates binary search to pinpoint the bad commit.


βš™οΈ Customizing Git

20. Set Global User Info

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

πŸ“‡ Identify yourself for all repositories.

21. Alias Common Commands

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

⚑ Create shortcuts for frequent commands.


πŸŽ‰ Bonus Tips

  • Clean Up Old Branches Locally and Remotely

    git branch -d <branch_name>      # Delete local branch
    git push origin --delete <branch_name>  # Delete remote branch
  • Force Pull (Override Local Changes)

    git fetch origin
    git reset --hard origin/<branch_name>

    ⚠️ Warning: This overwrites your local changes.

  • Track a Remote Branch Locally

    git checkout --track origin/<branch_name>

    πŸ‘£ Follows a remote branch for easy pulls and pushes.


πŸš€ Mastering Git Workflow

  1. Create a branch for new features:

    git checkout -b feature/new-feature
  2. Commit your work:

    git add .
    git commit -m "Add new feature"
  3. Push to GitHub:

    git push origin feature/new-feature
  4. Open a pull request on GitHub to merge into main or develop.


✨ That's a Wrap!

You’re now equipped with beginner and advanced Git skills. Start applying them to your projects, squash your bugs, and keep your commit history neat and clean. πŸ™βœ¨

Got feedback or more advanced topics you'd like to cover? Let me know! 😊


About

Git is your version control best friend, and GitHub is where collaboration magic happens! Below is a guide to the most useful Git & GitHub commands you’ll use daily. Let’s master the flow of commits, branches, and merges! πŸš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published