Introduction
Git is a powerful version control system used by developers worldwide to manage their source code efficiently. Whether you're a beginner or an experienced developer, having a comprehensive understanding of Git commands is essential for effective collaboration and project management. This cheat sheet provides a detailed overview of the most commonly used Git commands, organized into categories for easy reference. From basic operations like initializing a repository to advanced techniques like rebasing and cherry-picking, this guide covers everything you need to know to become a Git pro.
Commands
Basic Operations:
git init: Initialize a new Git repository.
git clone [url]: Clone a repository from a remote server.
git add [file]: Add file changes to the staging area.
git commit -m "[message]": Commit staged changes with a descriptive message.
git status: Display the current status of the repository.
git log: View commit history.
git diff [file]: Show changes made to a file.
Branch Management:
git branch: List all branches.
git branch [branch_name]: Create a new branch.
git checkout [branch_name]: Switch to a different branch.
git merge [branch]: Merge changes from a branch into the current branch.
git branch -d [branch_name]: Delete a branch.
Remote Operations:
git remote add origin [url]: Add a remote repository.
git remote -v: List all remote repositories.
git pull [remote] [branch]: Fetch changes from a remote repository and merge them.
git push [remote] [branch]: Push changes to a remote repository.
Undoing Changes:
git reset [file]: Unstage changes for a file.
git checkout -- [file]: Discard changes made to a file.
git revert [commit]: Create a new commit to undo changes made in a specific commit.
git reset --hard [commit]: Reset the repository to a specific commit.
Advanced Operations:
git rebase [branch]: Reapply commits from one branch onto another.
git cherry-pick [commit]: Apply changes from a specific commit to the current branch.
git stash: Temporarily store changes that are not ready to be committed.
git bisect: Use binary search to find the commit that introduced a bug.
git submodule: Manage external repositories as submodules within your own repository.
Miscellaneous:
git config: Set configuration options.
git tag: Create, list, delete, or verify a tag object signed with GPG.
git show: Show various types of objects.
git fetch: Download objects and refs from another repository.
git merge --abort: Abort an ongoing merge.
git log --oneline: Display commits in a condensed format.
git remote rm [remote_name]: Remove a remote repository from the list.
git clean -f: Remove untracked files from the working tree.
git push --tags: Push all tags to the remote repository.
git grep [search_term]: Search the working directory for lines matching a regular expression.
git mv [old_file] [new_file]: Move or rename a file, directory, or symlink.
git show-branch: Show branches and their commits.
git rebase -i [branch]: Interactively rebase the current branch onto another.
git describe: Output the most recent tag reachable from a commit.
git pull --rebase: Fetch the latest changes from the remote repository and rebase the current branch.
Conclusion
This Git commands cheat sheet covers a wide range of functionalities, from basic operations like initializing a repository and committing changes to more advanced tasks like branching, managing remote repositories, and undoing changes. Mastering these commands will empower you to effectively manage version control in your projects and collaborate seamlessly with your team.