September 21, 2024
Devops

GitHub Cheat Sheet: Commands & Best Practices

GitHub, built upon the Git version-control system, is a crucial platform for developers worldwide. This cheat sheet will guide you through some of the most common GitHub commands and best practices to streamline your coding and collaboration.

1. Setting up & Configuration:

  • Initialize a new repository:
  git init
  • Clone an existing repository:
  git clone [URL]
  • Configure user information:
  git config --global user.name "Your Name"
  git config --global user.email "[email protected]"

2. Basic Commands:

  • Check the status of your files:
  git status
  • Add files to staging area:
  git add [file-name]
  git add .   # Add all files
  • Commit your changes:
  git commit -m "Commit message"
  • Link your repository to a remote URL:
  git remote add origin [URL]
  • Push changes to GitHub:
  git push origin [branch-name]
  • Pull latest changes from remote repository:
  git pull origin [branch-name]
  • Create a new branch:
  git branch [branch-name]
  • Switch to a branch:
  git checkout [branch-name]
  • Merge a branch into the active branch:
  git merge [branch-name]

3. Advanced Commands:

  • List all branches:
  git branch
  • Delete a branch:
  git branch -d [branch-name]
  • See commit history:
  git log
  • Revert to a previous commit:
  git revert [commit-id]
  • Fetch changes from remote without merging:
  git fetch origin

4. Collaboration & Forks:

  • Forking a repository: This is a GitHub feature, so you simply click the ‘Fork’ button on the GitHub page of the desired repository. It creates a copy of the repo under your GitHub account.
  • Clone your fork:
  git clone [URL-of-fork]
  • Add the original repo as a remote to fetch from it:
  git remote add upstream [URL-of-original-repo]
  • Fetch latest changes from the original repo:
  git fetch upstream
  • Merge changes from the original repo into your local branch:
  git merge upstream/main   # or upstream/master based on the primary branch name

5. Best Practices:

  1. Commit Often: This ensures that changes are modular and easier to understand.
  2. Write Meaningful Commit Messages: Your future self (and other collaborators) will thank you.
  3. Sync Regularly: Regularly pull from the main repo (or master branch) to stay updated and avoid merge conflicts.
  4. Avoid Committing Directly to Main (or Master) Branch: Use feature branches and merge them after review.
  5. Address Merge Conflicts: If there are conflicts between your changes and the main repo, resolve them locally before pushing.

Conclusion:

This cheat sheet is by no means exhaustive but offers a handy reference for the most common GitHub operations. Whether you’re just starting out or need a quick refresher, these commands and best practices will help you maintain efficient workflows and effective collaboration on GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *