Git is a free and open-source version control system that helps us write, collaborate on, and build applications. It’s a major part of the development industry, and it’s become the de facto standard for version control in the industry, superseding previous version control systems like SVN and CVS. So, it’s no surprise that nearly all projects use Git for their version control.
If you want to work on these projects, you need to know and understand how to use Git.
This post covers over twenty of the most common Git commands you’ll use and need to know as a developer. Let’s get into it!
Access the Full Git Commands Cheat Sheet!
Get access to all 20+ essential Git commands below in one printable PDF reference guide - perfect for your desk or workspace
Git Commands: Managing Repositories
git init
If you have an existing directory and want to start a new Git repository with it
git remote add <alias> <url>
Allows you to add a remote Git URL as an alias for the repository, often this is used to set the origin
alias when first configuring a repository
git remote set-url <alias> <url>
If you need to update the URL assigned to an alias like origin
to a new URL
git remote -v
Check the current remote destinations configured for a repository, helpful to confirm where the code is being pushed to when using an alias
git clone <url>
Create a clone of a hosted remote repository on your local machine
git config
Used to configure Git on your local machine; can be done on a repository level or on a global level using the --global
flag
Access the Git Commands: Managing Repositories Cheat Sheet.
Git Commands: Collaborating
git fetch <alias>
Fetch all of the remote branches from the specified alias
git merge <branch_name>
Merge a branch and any changes into your current branch
git pull
Perform a git fetch
and git merge
in one command to fetch any changes and update your current branch to include them. You can also pass the --rebase
flag if you’d rather use rebase as a merging strategy instead of a merge commit
git blame
Identifies who made the last change to a given line in a project and in what commit
Access the Git Commands: Collaborating Cheat Sheet.
First time here? Discover what Prismic can do!
👋 Meet Prismic, your solution for creating performant websites! Developers, build with your preferred tech stack and deliver a visual page builder to marketers so they can quickly create on-brand pages independently!
Git Commands: Temporary Commits
git stash
Saves both unstaged and staged changes without making a commit. Can also pass the --include-untracked
flag to include new files that haven’t been committed before
git stash apply
Apply the contents of the last git stash
Access the Git Commands: Temporary Commits Cheat Sheet.
Git Commands: Rewriting History
git reset --hard <commit>
Clears the staging area and any uncommitted changes, reverting your commit history to match the specified commit SHA, deleting any newer commits
git commit --amend
Add any staged changes to the previous commit. If there are no staged changes, it allows you to edit the last commit’s message
git rebase <base>
Allows you to rebase the current branch onto the base
specified (commit SHA, branch name, etc). You can also use the -i
flag to make it an interactive rebase allowing you to edit the list of commits before the rebase happens. Learn more about rebasing
Access the Git Commands: Rewriting History Cheat Sheet.
Git Commands: Logging
git log
Displays the entire commit history of the repository using the default formatting. You can pass various flags to specify how you want to filter, format, or change the presentation of the log. See the entire list of flags
Noteworthy flags
--graph
Create a visual graph of the git history
--oneline
Format the log as single lines
--format=%B
See just the commit messages
-n <number>
Limit the number of commits returned in the log
--after
/ --before
Filter the commits based on their commit date
Access the Git Commands: Logging Cheat Sheet.
Helpful Git Tools
All these commands can be a lot to remember which is why developers have invested time in making tools to help simplify Git; making it easier to use as well as integrate more into tooling like IDEs. So, here are four particularly noteworthy tools.
- Neogit: Integrate Git into Neovim
- GitHub Desktop: Use Git via an easy-to-use GUI
- Lazygit: Terminal UI for Git
- GitLens: Extension for VS Code to build upon the base functionality built into VS Code
Conclusion
At this point, we’ve covered why Git is important and looked at over twenty of the most helpful Git commands as well as some helpful tools to make Git easier and more efficient to use.
But, if you’d like to take your Git knowledge to the next level why not consider checking out the official Git documentation or the GitHub Git learning resources? Or, take the next step and learn which cloud-based Git platform you should be hosting your code on in 2024.
Thank you for reading.