Git is a powerful version control system that allows developers to manage and track changes in their codebase efficiently. It provides a suite of commands that help in collaboration, versioning, and maintaining the integrity of projects. This article serves as a comprehensive Git cheat sheet, summarizing essential commands for both beginners and experienced users.
Repository Commands
1. Initialize and Clone Repositories
git init
: This command initializes a new Git repository in the current directory. It creates a hidden.git
folder that stores all the necessary metadata for version control.git clone <repo-url>
: Use this command to create a local copy of a remote repository. Replace<repo-url>
with the URL of the repository you wish to clone.
Basic Commands
2. Check Status and Commit Changes
git status
: This command displays the state of the working directory and the staging area. It shows which changes have been staged, which are still unstaged, and which files aren’t being tracked by Git.git add <file>
: This command stages changes in the specified file for the next commit. To stage all modified files, you can usegit add .
.git commit -m "Message"
: After staging files, this command commits the changes to the repository. The-m
flag allows you to add a concise message that describes the commit.git log
: This command displays the commit history for the current branch. It provides a chronological list of commits, including commit hashes, authors, dates, and messages.
Branching Commands
3. Create and Manage Branches
git branch
: This command lists all the branches in your repository. The currently checked-out branch is highlighted with an asterisk.git branch <branch-name>
: Use this command to create a new branch with the specified name. This is a best practice for developing new features or fixing bugs without affecting the main codebase.git checkout <branch-name>
: This command switches your working directory to the specified branch. After switching, you can continue working on that branch.git merge <branch-name>
: This command merges the specified branch into your current branch. If there are conflicts between branches, you’ll need to resolve them before completing the merge.git branch -d <branch-name>
: This command deletes the specified branch. You can only delete branches that have been fully merged into the current branch.
Remote Repository Commands
4. Interact with Remote Repositories
git remote
: This command lists all remote repositories associated with your local repository. It helps you keep track of which remote servers your repository is connected to.git remote add <name> <url>
: Use this command to add a new remote repository with the specified name and URL. This is useful for linking your local repository to a remote server like GitHub or GitLab.git push <remote> <branch>
: This command uploads your local changes to the specified branch of the remote repository. Replace<remote>
with the name of your remote (e.g.,origin
) and<branch>
with the branch name you want to push.git pull <remote> <branch>
: This command fetches changes from the specified branch of the remote repository and merges them into your current branch. It’s a convenient way to keep your local branch up to date with remote changes.
Undoing Changes
5. Manage Changes and Revisions
git pull
: This command is a shortcut for fetching changes from a remote repository and merging them into your current branch. It’s useful for regularly synchronizing your work with that of your collaborators.git fetch
: Unlikegit pull
, this command retrieves changes from the remote repository without merging them. It allows you to review changes before deciding to integrate them into your branch.git reset --hard HEAD
: This command resets your working directory to the last committed state, discarding all changes in tracked files. Use it cautiously, as it will delete any uncommitted changes.git revert <commit-hash>
: This command creates a new commit that undoes the changes introduced by the specified commit. Instead of deleting history, it adds a new entry that effectively negates the previous changes.
Conclusion
Understanding and mastering Git is crucial for modern software development. This cheatsheet provides a concise reference for common Git commands, helping you navigate your version control tasks with ease. Whether you’re collaborating on a team or working solo, using these commands will streamline your workflow and enhance your productivity.