Skip to content

Instantly share code, notes, and snippets.

@JettIsOnTheNet
Created July 19, 2023 03:45
Show Gist options
  • Save JettIsOnTheNet/c39b718af1c916462f13256d8e9e1484 to your computer and use it in GitHub Desktop.
Save JettIsOnTheNet/c39b718af1c916462f13256d8e9e1484 to your computer and use it in GitHub Desktop.
Github Cheat Sheet

GitHub Cheat Sheet

Basic Git Commands:

  1. Initialize a repository:

    git init
    
  2. Clone a repository:

    git clone <repository_url>
    
  3. Add files to staging area:

    git add <file_name>
    
  4. Commit changes:

    git commit -m "commit message"
    
  5. Push changes to remote repository:

    git push <remote_name> <branch_name>
    
  6. Pull latest changes from remote:

    git pull <remote_name> <branch_name>
    

Branching and Merging:

  1. Create a new branch:

    git branch <branch_name>
    
  2. Switch to a branch:

    git checkout <branch_name>
    
  3. Create a new branch and switch to it (shortcut):

    git checkout -b <branch_name>
    
  4. Merge a branch into the current branch:

    git merge <branch_name>
    
  5. Delete a branch:

    git branch -d <branch_name>
    

Remote Repository Management:

  1. Add a remote repository:

    git remote add <remote_name> <repository_url>
    
  2. View remote repositories:

    git remote -v
    
  3. Remove a remote repository:

    git remote remove <remote_name>
    

Checking Status and History:

  1. Check the status of the repository:

    git status
    
  2. View commit history:

    git log
    
  3. View a compact commit history:

    git log --oneline
    
  4. View changes made in a specific commit:

    git show <commit_hash>
    

Undoing Changes:

  1. Discard changes in a file (restore from the last commit):

    git checkout -- <file_name>
    
  2. Undo the last commit (keeping changes in the working directory):

    git reset HEAD~1
    
  3. Undo the last commit and discard changes:

    git reset --hard HEAD~1
    

Collaboration and Pull Requests:

  1. Create a Pull Request (PR) on GitHub website:

    • Push your changes to a new branch on GitHub
    • Click on "Pull Request"
    • Review and submit the PR
  2. Review and merge a Pull Request:

    • On GitHub, go to the Pull Request page
    • Review changes and discussions
    • Merge the PR when ready
  3. Update your local branch with changes from the PR (after it's merged):

    git pull <remote_name> <branch_name>
    

Working with Remotes:

  1. Fetch changes from a remote repository (without merging):

    git fetch <remote_name>
    
  2. Push changes to a specific branch on the remote repository:

    git push <remote_name> <local_branch_name>:<remote_branch_name>
    
  3. Set up the default remote repository and branch for push and pull:

    git push -u <remote_name> <local_branch_name>
    

Resolving Conflicts:

  1. When a merge conflict occurs, open the conflicted file and manually resolve the conflicts, then commit the changes.

  2. Abort a merge or rebase (in case of conflicts):

    git merge --abort
    git rebase --abort
    

Tags:

  1. Create an annotated tag:

    git tag -a <tag_name> -m "tag message"
    
  2. Push tags to the remote repository:

    git push --tags
    

Stashing:

  1. Temporarily save changes in a "stash" (useful for switching branches):

    git stash save "stash message"
    
  2. Apply the most recent stash and remove it from the stash list:

    git stash pop
    
  3. Apply a specific stash by index and remove it from the stash list:

    git stash pop stash@{<index>}
    

GitHub Actions:

  1. Automate workflows with GitHub Actions. Create a .github/workflows directory and add YAML files defining your workflows.

Git Ignore:

  1. Create a .gitignore file to exclude specific files or directories from version control.

GitHub CLI (gh):

  1. Install the GitHub CLI (gh) and authenticate:

    gh auth login
    
  2. Create a new repository using the GitHub CLI:

    gh repo create <repository_name> --public --description "<description>"
    

Collaboration:

  1. Fork a repository on GitHub website:

    • Click on "Fork" button on the top-right corner of the repository page.
  2. Keep your forked repository synced with the original repository:

    • Add the original repository as an upstream remote.
    • Fetch and merge changes from the upstream remote into your local repository.

Git Configurations:

  1. Set your name and email (global configuration):

    git config --global user.name "Your Name"
    git config --global user.email "youremail@example.com"
    
  2. Set default editor (e.g., for commit messages):

    git config --global core.editor "editor_name"
    

Git Rebase:

  1. Rebase your current branch onto another branch:

    git rebase <branch_name>
    
  2. Resolve conflicts during a rebase:

    • Open the conflicted file and manually resolve the conflicts.
    • Use git add to stage the resolved changes.
    • Continue the rebase with git rebase --continue.

Git Cherry-Pick:

  1. Apply a specific commit from one branch to another:
    git cherry-pick <commit_hash>
    

Git Reset:

  1. Undo commits by removing them from the branch history:

    git reset <commit_hash>
    
  2. Soft reset: move branch pointer to a previous commit without modifying the working directory or staging area:

    git reset --soft <commit_hash>
    
  3. Hard reset: move branch pointer and discard all changes after the specified commit:

    git reset --hard <commit_hash>
    

Git Bisect:

  1. Use binary search to find the commit that introduced a bug:
    git bisect start
    git bisect bad  # Set current commit as bad
    git bisect good <commit_hash>  # Set known good commit
    

Git Submodules:

  1. Add a submodule to your repository:

    git submodule add <repository_url> <path>
    
  2. Clone a repository along with its submodules:

    git clone --recurse-submodules <repository_url>
    

GitHub Pages:

  1. Host a static website using GitHub Pages:
    • Create a branch named gh-pages and push your static files to it.
    • Go to the repository's settings on GitHub, scroll down to the GitHub Pages section, and select the gh-pages branch as the source.

Git LFS (Large File Storage):

  1. Track large files using Git LFS:
    • Install Git LFS: git lfs install
    • Track a file with LFS: git lfs track <file_name>
    • Commit and push changes as usual.

Git Hooks:

  1. Use Git hooks to automate tasks at various stages of the Git workflow. Hooks are scripts placed in the .git/hooks directory of a repository.

Git Aliases:

  1. Create custom Git aliases for frequently used commands:
    git config --global alias.<alias_name> <git_command>
    

GitHub Issues:

  1. Create an issue on GitHub:

    • Go to the repository on GitHub and click on the "Issues" tab.
    • Click on "New Issue" and provide the necessary details.
  2. Reference an issue in a commit message:

    git commit -m "Commit message. Fixes #<issue_number>"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment