Introduction
Git is a powerful version control system that allows developers to track changes in their code, collaborate with others, and manage different versions of their projects. Mastering Git commands is essential for efficient and effective code management. This cheat sheet provides a quick reference to some of the most commonly used Git commands.
Git Commands Cheat Sheet
Here’s a handy cheat sheet of the most commonly used Git commands, ordered by their usage and popularity:
Git Command | Description |
---|---|
git init |
Initializes a new Git repository. |
git clone <repo> |
Clones a repository into a new directory. |
git status |
Displays the state of the working directory and staging area. |
git add <file> |
Adds a file to the staging area. |
git add . |
Adds all changes in the working directory to the staging area. |
git commit -m "message" |
Commits the staged changes with a descriptive message. |
git log |
Displays a log of all commits in the repository. |
git diff |
Shows changes between commits, commit and working directory, etc. |
git branch |
Lists all branches in the repository. |
git branch <branch_name> |
Creates a new branch. |
git checkout <branch> |
Switches to the specified branch. |
git checkout -b <branch> |
Creates and switches to a new branch. |
git merge <branch> |
Merges the specified branch into the current branch. |
git pull |
Fetches and merges changes from the remote repository to the current branch. |
git push |
Pushes local changes to the remote repository. |
git remote -v |
Shows all remote repositories associated with the local repository. |
git fetch |
Downloads objects and refs from another repository. |
git reset --hard <commit> |
Resets the current branch to the specified commit, discarding all changes in the working directory and staging area. |
git rm <file> |
Removes a file from the working directory and the staging area. |
git stash |
Stashes changes in the working directory that are not ready to be committed. |
git stash pop |
Applies the latest stashed changes and removes them from the stash list. |
git stash list |
Lists all stashed changes. |
git rebase <branch> |
Reapplies commits on top of another base tip. |
git cherry-pick <commit> |
Applies the changes introduced by the specified commit. |
git tag <tag_name> |
Creates a tag for the specified commit. |
git show <tag_name> |
Displays information about the specified tag. |
git blame <file> |
Shows what revision and author last modified each line of a file. |
git log --oneline |
Shows a condensed log where each commit is displayed on a single line. |
git config --global user.name "name" |
Sets the name to be used for commits. |
git config --global user.email "email" |
Sets the email to be used for commits. |
git reflog |
Shows a log of references, including commits that have been moved or deleted. |
git bisect |
Uses binary search to find the commit that introduced a bug. |
git submodule add <repo> |
Adds a new submodule to the repository. |
git submodule update --init |
Initializes, fetches, and checks out submodules. |
git clean -f |
Removes untracked files from the working directory. |
git grep <pattern> |
Searches for the specified pattern in the working directory. |
Conclusion
Mastering Git commands is essential for efficient and effective code management. This cheat sheet provides a quick reference to some of the most commonly used Git commands, helping you streamline your workflow and collaborate with others more effectively. By understanding and using these commands, you can simplify your version control process, enhance your productivity, and ensure your projects are well-managed. Keep this guide handy to make the most of Git. Happy coding!