Day 12: Git & GitHub Cheatsheet

Day 12: Git & GitHub Cheatsheet

ยท

3 min read

Git Basics:

  1. Initialize a Repository:

     git init
    
  2. Clone a Repository:

     git clone <repository-url>
    
  3. Check Status:

     git status
    
  4. Stage Changes:

     git add <file>
     git add .       # Stage all changes
    
  5. Commit Changes:

     git commit -m "Commit message"
    
  6. View Commit History:

     git log
    
  7. Create a New Branch:

     git branch <branch-name>
    
  8. Switch Branches:

     git checkout <branch-name>
    
  9. Merge Branches:

     git merge <branch-name>
    
  10. Undo Changes (Unstaged):

    git checkout -- <file>
    
  11. Undo Changes (Staged):

    git reset HEAD <file>
    
  12. Undo Last Commit (Keep Changes):

    git reset --soft HEAD~1
    
  13. Undo Last Commit (Discard Changes):

    git reset --hard HEAD~1
    

GitHub Basics:

  1. Push Changes to Remote Repository:

     git push origin <branch-name>
    
  2. Pull Changes from Remote Repository:

     git pull origin <branch-name>
    
  3. Add Remote Repository:

     git remote add origin <repository-url>
    
  4. View Remote Repositories:

     git remote -v
    
  5. Create Pull Request:

    1. Push changes to GitHub.

    2. Go to the repository on GitHub.

    3. Click on "Pull Requests".

    4. Click on "New Pull Request".

    5. Select the branches to compare.

  6. Merge Pull Request:

    1. Go to the Pull Request on GitHub.

    2. Review changes.

    3. Click on "Merge Pull Request".

    4. Confirm merge.

  7. Fetch Changes from Remote:

     git fetch origin
    
  8. View Branches:

     git branch -a
    
  9. Remove Remote Repository:

     git remote remove <repository-name>
    
  10. Sync Forked Repository:

    1. Add upstream repository:

       git remote add upstream <upstream-repo-url>
      
    2. Fetch changes:

       git fetch upstream
      
    3. Merge changes into local branch:

       git merge upstream/<branch-name>
      
  11. Create and Apply Gitignore:

    1. Create .gitignore file in root directory.

    2. Add files or patterns to ignore.

    3. Save changes and commit.

  12. View Gitignore Status:

    git status --ignored
    

Advanced Git:

  1. Rebase Branch:

     git rebase <base-branch>
    
  2. Interactive Rebase:

     git rebase -i <base-branch>
    
  3. Cherry-Pick a Commit:

     git cherry-pick <commit-hash>
    
  4. Stash Changes:

     git stash
    
  5. Apply Stashed Changes:

     git stash apply
    
  6. View Stash List:

     git stash list
    
  7. Drop Specific Stash:

     git stash drop <stash-index>
    
  8. Clean Untracked Files:

     git clean -fd
    
  9. View Differences Between Branches:

     git diff <source-branch> <target-branch>
    
  10. Resolve Merge Conflicts:

    1. Open the conflicted file.

    2. Manually resolve conflicts.

    3. Add resolved files.

    4. Commit changes.

This cheat sheet covers essential Git and GitHub commands for beginners and more advanced users. Use it as a quick reference guide to streamline your workflow and collaboration processes.

"Fuel my passion and support my journey by clicking 'Buy me a coffee' today!"

~Dipen : )

Did you find this article valuable?

Support Dipen's Blog by becoming a sponsor. Any amount is appreciated!

ย