Git Commands Cheat Sheet: A Comprehensive Guide to Version Control
As a developer, mastering Git is essential for efficient version control and collaboration. Whether you’re just starting or looking to expand your knowledge, this comprehensive Git commands cheat sheet will be your go-to reference. We’ve organized the commands into different categories, each with a brief explanation, to make your version control journey smoother.
Introduction to Git
Git is a distributed version control system designed to track changes in your code over time. It allows multiple developers to work on the same project simultaneously, making collaboration seamless and reducing the risk of conflicts. With Git, you can create branches, manage commits, and synchronize with remote repositories, ensuring your code remains organized and secure.
Basic Commands
These commands form the foundation of your Git workflow, helping you manage your local repository effectively.
Command | Description |
---|---|
git init | Initialize a new Git repository in the current directory. |
git clone <url> | Clone a remote repository to your local machine. |
git add <file> | Stage changes for the next commit. |
git status | Show the status of your working directory. |
git commit -m “msg” | Create a new commit with staged changes and a message. |
git log | View the commit history. |
git diff | Show the differences between the working directory and staging. |
git reset <file> | Unstage changes for a specific file. |
git checkout <file> | Discard changes in a specific file. |
git rm <file> | Remove files from the working directory and staging area. |
git mv <old> <new> | Move or rename files and directories. |
git config | Set configuration options like username and email. |
git help | Get help on any Git command. |
git grep <pattern> | Search for a specified pattern in the repository. |
git bisect | Find the commit that introduced a bug using binary search. |
git clean | Remove untracked files from the working directory. |
git archive | Create a tar or zip archive of the repository. |
git submodule | Manage Git submodules within your repository. |
git show-ref | Display references (e.g., branches, tags) in the repository. |
git fsck | Check the integrity of the repository. |
Working with Branches
Branches allow you to work on different features independently and keep your codebase organized.
Command | Description |
---|---|
git branch | List all branches. |
git branch <name> | Create a new branch. |
git checkout <branch> | Switch to the specified branch. |
git checkout -b <branch> | Create and switch to a new branch. |
git merge <branch> | Merge the specified branch into the current branch. |
git branch -d <branch> | Delete a branch. |
git branch -m <old> <new> | Rename a branch. |
git stash | Temporarily save changes for later use. |
git stash list | List all stashes. |
git stash apply | Apply the most recent stash. |
git stash pop | Apply and remove the most recent stash. |
git stash drop | Remove the most recent stash. |
git cherry-pick | Apply a specific commit from one branch to another. |
git rebase | Reapply commits on top of another base commit. |
git reflog | Show a log of all references (e.g., branches) in the repository. |
Synchronizing with Remote
These commands help you interact with remote repositories, making it easy to collaborate with others.
Command | Description |
---|---|
git remote -v | List all remote repositories and their URLs. |
git remote add <name> <url> | Add a new remote repository. |
git fetch <remote> | Download objects and refs from a remote repository. |
git pull <remote> <branch> | Fetch and merge changes from a remote branch. |
git pull –rebase | Fetch and rebase your current branch on top of the remote. |
git push <remote> <branch> | Push local commits to a remote branch. |
git push –force | Force push changes to a remote branch (use with caution!). |
git push –all <remote> | Push all branches to a remote repository. |
git push –tags <remote> | Push all tags to a remote repository. |
git push <remote> –delete <branch> | Delete a remote branch. |
git remote prune <remote> | Remove remote branches that no longer exist on the remote. |
Inspecting Changes
Use these commands to inspect and compare changes made to your codebase.
Command | Description |
---|---|
git show <commit> | Show metadata and changes of a specific commit. |
git log | View the commit history. |
git log –oneline | Show abbreviated commit history with each commit on one line. |
git log –graph | Show commit history as a graph. |
git log –author=<author> | View commits by a specific author. |
git log –grep=<pattern> | Filter commits by a specific message pattern. |
git blame <file> | Show who last modified each line of a file. |
git tag | List all tags in the repository. |
git tag <tagname> | Create a new tag for the current commit. |
git diff <commit> | Compare changes between the working directory and a commit. |
git diff <commit1>..<commit2> | Compare changes between two commits. |
Advanced Techniques
Explore these advanced commands to take your Git skills to the next level.
Command | Description |
---|---|
git rebase -i | Interactively rebase commits to squash, edit, or reorder them. |
git cherry-pick -x | Apply a commit and record the original commit ID. |
git reflog expire | Remove expired entries from the reflog. |
git bisect run | Automate the process of binary search for a bug. |
git bisect reset | Terminate a git bisect session and return to the original |
git rerere | Reuse recorded resolution of conflicted merges. |
git remote set-url | Update the URL of a remote repository. |
git filter-branch | Rewrite branches to apply custom filters. |
git worktree | Manage multiple working trees for the same repository. |
git log –pretty | Customize the output of git log< |
git clean -f | Remove untracked files forcefully. |
git blame -L | Show the author of a specific range of lines in a file. |
Tip
If you want to see all possible parameters for a specific command just white the command and write –help or -h at end, for example if we want to see all possible parameters of git log command so to git log –help or git log -h
Conclusion
Now that you have this extensive Git commands cheat sheet, you can confidently navigate version control, collaborate seamlessly with others, and effectively manage your codebase. Experiment with these commands in your projects and become a Git pro!
Remember, version control is a powerful tool, and mastering Git will undoubtedly elevate your development skills. Happy coding and collaborating!
Comments