20 min read

GitHub and Git Cheat Sheet for Developers


Navigating through the intricacies of Git and GitHub can be daunting for developers, both new and seasoned. This cheat sheet aims to demystify the most common Git commands and GitHub workflows, serving as a quick reference guide to enhance your development process. Let’s dive into the essentials.

Git Basics

  • git init: Initialize a new Git repository in your project directory.
git init
  • git clone [URL]: Clone an existing repository to your local machine.
git clone https://github.com/user/repository.git   
  • git status: Check the status of changes as untracked, modified, or staged.
git status
  • git add [file]: Add a specific file to the staging area, making it ready for commit.
git add filename.txt
  • git add .: Add all new and changed files to the staging area.
git add .
  • git commit -m “[message]”: Commit your staged changes to the repository with a descriptive message.
git commit -m "Add feature X"
  • git push [alias] [branch]: Push your committed changes to the remote repository.
git push origin master
  • git pull [alias] [branch]: Pull changes from the remote repository to your local repository.
git pull origin master
  • git branch: List all the branches in your repository.
git branch
  • git checkout -b [branch name]: Create and switch to a new branch.
git checkout -b feature-branch
  • git checkout [branch name]: Switch from one branch to another.
git checkout master
  • git merge [branch]: Merge the specified branch’s history into the current branch.
git merge feature-branch
  • Forking: Click the “Fork” button on the repository page to create a copy of the repository in your GitHub account.
  • Pull Requests: Propose changes to a project that you’ve forked by navigating to the “Pull Requests” tab and clicking “New Pull Request.”
  • Issues: Report bugs or suggest new features by navigating to the “Issues” tab and clicking “New Issue.”
  • GitHub Pages: Deploy your projects directly from your GitHub repository via the “Settings” tab, navigating to the “Pages” section.

Advanced Git Commands

  • git stash: Temporarily shelve changes so you can work on a different branch.
git stash
  • git stash pop: Apply stashed changes back to your working directory.
git stash pop
  • git reset [commit]: Undo all commits after [commit], preserving changes locally.
git reset 0d1d7fc32
  • git reset —hard [commit]: Discard all history and changes back to the specified commit.
git reset --hard 0d1d7fc32
  • *git log: Display the commit history for the currently active branch.
git log
  • git rebase [branch]: Apply your work on top of another branch’s base. Use with caution.
git rebase master

GitHub and Git Tips

  • Commit Often, Push Less: Make it a habit to commit your changes often locally, but push less frequently to remote to keep the history clean.
  • Use Branches: Leverage branching for new features or bug fixes to avoid disrupting the main codebase.
  • PR Descriptions: When creating pull requests, thoroughly describe the changes and the reason behind them to facilitate reviews.
  • .gitignore: Utilize a ‘.gitignore’ file in your repository to avoid committing unnecessary or sensitive files to your repository.

Conclusion

This cheat sheet covers the foundational elements of Git and GitHub but barely scratches the surface of what’s possible. As you grow more comfortable with these tools, you’ll discover more advanced techniques and workflows to streamline your development process.

Support ❤️
If you have enjoyed my content and code, do support me by buying a couple of coffees. This will enable me to dedicate more time to research and create new content. Cheers!
Share this Article
Share this article with your network to help others!
What's your Feedback?
Do let me know your thoughts around this article.