Git Commands Cheat Sheet: Essential Guide for Version Control
Understand the fundamental concepts and commands of Git, the distributed version control system. Comprehensive explanations and examples for essential Git commands.
Git is a distributed version control system that enables developers to track and manage changes to their codebase efficiently. It supports collaborative workflows, allowing multiple contributors to work on a project simultaneously without conflicting with each other's progress.
By maintaining a history of changes, Git allows developers to revert to previous versions when necessary and provides insights into the project's evolution over time. As a cornerstone of modern software development, Git facilitates collaboration, code versioning, and the preservation of a project's integrity.
Key Git Terms
-
Repository (Repo): A storage location for your project. It can be local on your computer or hosted on platforms like GitHub.
-
Commit: An individual change to a file or set of files. Think of it as a snapshot of your project at a specific point in time.
-
Branch: A parallel version of a repository. Branches allow you to work on different features or fixes without affecting the main codebase.
-
Merge: Combining changes from one branch into another, integrating new features or fixes into the main project.
-
Clone: Creating a local copy of a remote repository, allowing you to work on the project locally.
-
Fork: Creating a personal copy of someone else's repository. Forking allows you to experiment and make changes without impacting the original project.
-
Pull Request (PR): A request to merge your changes into another repository's branch. PRs facilitate code reviews and discussions before integration.
-
Push: Uploading your committed changes to a remote repository, making them available to others.
-
Pull: Fetching and integrating changes from a remote repository into your local repository.
-
Remote: A version of your repository hosted on a server, such as GitHub. Remotes are used to collaborate and share changes.
Initializing & Cloning
Initializing a New Repository
git init
Cloning an Existing Repository
git clone <url>
Branching
Branches allow you to work on different parts of a project simultaneously.
Creating a New Branch
git branch <branch-name>
Switching to a Specific Branch
git checkout <branch-name>
Modern Alternative to git checkout
git switch <branch-name>
Staging
Staging prepares your changes for a commit.
Adding a Specific File to Staging
git add <file>
Adding All Changes to Staging
git add .
Staging Hunks Interactively
git add -p
Committing
Committing saves your staged changes to the repository.
Commit with a Message
git commit -m "message"
Add All Tracked Changes to Staging and Commit
git commit -am "message"
Remote Operations
Managing remote repositories is essential for collaboration.
Adding a Remote Repository
git remote add origin <url>
Pulling Changes from Remote
git pull origin <branch>
Pushing Changes to Remote
git push origin <branch>
Adding Remote with HTTPS and Token (GitHub)
If you prefer not to set up SSH keys, you can use HTTPS with a personal access token.
Steps to Add Remote Using HTTPS and Token
- Generate a personal access token on GitHub.
- Add the remote repository by replacing
[TOKEN]
,[USERNAME]
, and[REPOSITORY]
with your GitHub token, username, and repository name respectively.
git remote add origin https://[TOKEN]@github.com/[USERNAME]/[REPOSITORY].git
git push origin main
Miscellaneous
Check the Status of Changes
git status
View Commit History
git log
Revert to a Previous Commit
git revert <commit-hash>
Sync & Merge
Keeping your branches up-to-date is crucial for smooth collaboration.
Merge Another Branch into the Active Branch
git merge <branch>
Fetch Latest Changes from Remote (Without Merging)
git fetch
Fetch and Rebase Local Commits on Top
git pull --rebase
Stashing
Stashing allows you to temporarily save changes without committing them.
Stash Changes
git stash
Apply Stashed Changes
git stash pop
Conclusion
This cheat sheet covers essential Git commands and concepts vital for effective version control in software development. Understanding and utilizing these commands will enhance your ability to manage codebases, collaborate with team members, and maintain the integrity of your projects.
For more detailed information and advanced Git functionalities, refer to the Git Documentation.