Git & GitHub Cheat Sheet

·

3 min read

Git & GitHub Cheat Sheet

Git Commands

Creating a Repository

1.Initialize a new Git repository:

git init

2.Clone an existing Git repository:

git clone <repository_URL>

Staging Changes

1.Add changes to the staging area:

git add .

Commiting Changes

1.Commit changes with a message:

git commit -m "Commit message"

2.Reset to a specific commit

git reset <commit hash>

3.Revert to a previous commit

git revert <commit hash>

Branching

  1. Create a new branch:

      git branch <branch name>
    
  2. Switch to an existing branch:

      git checkout <branch name>
      git switch <branch name>
    
  3. Showing all remote branch

      git branch -a
    
  4. Create and switch to a new branch:

      git checkout -b <branch name>
    
  5. Merge a branch into the current branch:

      git merge <branch name>
    
  6. Undo local changes

      git checkout -- <file_name>
    
  7. Delete a branch

      git branch -d <branch name>
    

Remote Repositories

  1. Add a remote repository:

      git remote add <remote name> <remote URL>
    
  2. Push changes to a remote repository:

      git push origin <branch name>
    
  3. Pull changes from a remote repository:

      git pull origin <branch name>
    
  4. Show remote origin URL

      git remote -v
    
  5. Add remote origin URL

      git remote add origin <your/remote/git/URL>
    
  6. Remove remote origin URL

      git remote remove origin
    
  7. Fetch all the remote branches

      git fetch
    

Rewrite History

  1. Rebase apply any commits of the current branch ahead of the specified one

      git rebase <branch_name>
    
  2. Reset clear staging area, rewrite the working tree from the specified commit

      git reset --soft <commit>
    

Temporary Commits

  1. Save modified and staged changes

      git stash
    
  2. List stack order of stashed file changes

      git stash list
    
  3. Write working from the top of the stash stack

      git stash pop
    
  4. Discard the changes from the top of the stash stack

      git stash drop
    

Miscellaneous

  1. Check the status of your Git repository:

      git status
    
  2. Set global username and email for Git (Locally)

      git config --global user.name "<your username>"
      git config --global user.email "<your email>"
    
  3. Restore the file from being modified to being Tracked

      git restore <filename>
      git checkout <filename>
    
  4. View the commit history of your Git repository:

      git log
    
  5. View the difference between the two commits:

      git diff <commit 1> <commit 2>
    

GitHub Commands


Creating a Repository

  1. Create a new repository on GitHub and copy the URL.

  2. Clone the repository to your local machine:

      git clone <repository_URL>
    

Collaborating with Others

  1. Add a collaborator to the repository:

    Settings -> Manage access -> Invite a collaborator

  2. Accept a collaborator invitation:

    Notifications -> Invitations

  3. Fork a repository:

    Click the "Fork" button on the repository's GitHub page

  4. Create a pull request:

    Click the "New pull request" button on the repository's GitHub page

  5. Review and merge a pull request:

    Click the "Merge pull request" button on the repository's GitHub page

Thanks for Reading!