CN

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

  1. Repository (Repo): A storage location for your project. It can be local on your computer or hosted on platforms like GitHub.

  2. 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.

  3. Branch: A parallel version of a repository. Branches allow you to work on different features or fixes without affecting the main codebase.

  4. Merge: Combining changes from one branch into another, integrating new features or fixes into the main project.

  5. Clone: Creating a local copy of a remote repository, allowing you to work on the project locally.

  6. Fork: Creating a personal copy of someone else's repository. Forking allows you to experiment and make changes without impacting the original project.

  7. Pull Request (PR): A request to merge your changes into another repository's branch. PRs facilitate code reviews and discussions before integration.

  8. Push: Uploading your committed changes to a remote repository, making them available to others.

  9. Pull: Fetching and integrating changes from a remote repository into your local repository.

  10. 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

Initialize a new repository
git init

Cloning an Existing Repository

Clone a repository
git clone <url>

Branching

Branches allow you to work on different parts of a project simultaneously.

Creating a New Branch

Create a new branch
git branch <branch-name>

Switching to a Specific Branch

Switch to a branch
git checkout <branch-name>

Modern Alternative to git checkout

Switch to a branch using git switch
git switch <branch-name>

Staging

Staging prepares your changes for a commit.

Adding a Specific File to Staging

Add a specific file
git add <file>

Adding All Changes to Staging

Add all changes
git add .

Staging Hunks Interactively

Interactively stage changes
git add -p

Committing

Committing saves your staged changes to the repository.

Commit with a Message

Commit changes with a message
git commit -m "message"

Add All Tracked Changes to Staging and Commit

Add and commit tracked changes
git commit -am "message"

Remote Operations

Managing remote repositories is essential for collaboration.

Adding a Remote Repository

Add a remote repository
git remote add origin <url>

Pulling Changes from Remote

Pull changes from remote
git pull origin <branch>

Pushing Changes to Remote

Push 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

  1. Generate a personal access token on GitHub.
  2. Add the remote repository by replacing [TOKEN], [USERNAME], and [REPOSITORY] with your GitHub token, username, and repository name respectively.
Add remote using HTTPS and token
git remote add origin https://[TOKEN]@github.com/[USERNAME]/[REPOSITORY].git
git push origin main

Miscellaneous

Check the Status of Changes

Check repository status
git status

View Commit History

View commit history
git log

Revert to a Previous Commit

Revert to a specific 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

Merge a branch
git merge <branch>

Fetch Latest Changes from Remote (Without Merging)

Fetch latest changes
git fetch

Fetch and Rebase Local Commits on Top

Pull with rebase
git pull --rebase

Stashing

Stashing allows you to temporarily save changes without committing them.

Stash Changes

Stash current changes
git stash

Apply Stashed Changes

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.

Want to read more articles like that?

Sign up to get notified when I publish more.

No spam. One click unsubscribe.

Read More on Fado Code Camp