Top Git Commands (Plus Comparison to Perforce)
What are the most important Git commands? Find out in this blog. Plus, compare Git commands vs. Perforce commands.
Top 20 Git Commands + Examples
Here are some of the most useful Git commands, plus examples.
Using the git config command allows you to set configuration values in the filesystem. Executing this Git command modifies the main configuration text file.
You can use it to set your user name and email:
git config --global user.name “your username” git config --global user.email “[email protected]”
Executing this Git command creates an empty, new repository as a .git subdirectory in your current working directory. Once you create the repo, you can add and commit changes.
git init /home/username/reponame/.git/
git clone is one of the most commonly used commands. It creates a copy of a remote Git repo on your local machine. It adds the remote location to the path to allow you to fetch, pull, and push changes.
Add new and modified files to your working directory. This Git command uses content in the tree, and prepares the files for your next commit.
git add <file path>
The git commit command records changes in your repo’s version history. Think of a commit like a snapshot of your work. You can also use Git commit -a to commit any files you have added since the last commit.
git commit -m “commit message”
Using this Git command you can see changes between commits. This includes changes between the remote repo and the working directory, or changes between trees.
git diff [<options>] [--] [<path>…]
Want to undo some recent changes? Walk back in time and use git reset to revert your working directory back to the your last commit.
git reset –hard HEAD
Use this Git command to show the commits and details of a branch.
git log commit commit <commit number> Author: “author’s name” Date: “date”
Stop tracking files and remove them from working directory.
git rm <file name>
Compare the status of files in the shared repo compared to your working directory. This Git command lists:
- Files that are not tracked and in your working directory.
- Modified files that have not been updated to the branch.
- Staged files that are ready to be committed.
</reponame/branch> git status On branch master: Changes not tagged for commit: (use “git add <file>..” to update what will be committed. (use “git checkout -- <file> to discard changes in working directory. modified: <file path> no changes added to commit (use “git add” and/or “git commit -a”)
Find commits easier by assigning a handle to a commit using the git tag command.
git tag -a v1.1 -m ‘this is version 1.1’
Create, view, and delete your connection to repositories. If you are connecting to a repo remotely, this command lists your connection to other repositories.
git remote [<variable name>] [<origin/remote server link>]
You can use this command to switch between branches. When you check out a branch, the files in your working directory are updating to match the main version. All new commits are recorded on that branch.
git checkout [<branch name>]
Get started working with the git branch command. After you create a feature branch and commit changes, you can delete your feature branch using the same command.
Get a list of all branches in the repo:
[<repo path>] git branch *master
Create new branches:
git branch [<branch name>]
Delete feature branches:
git branch- d [<branch name>]
Use git push to submit changes and modified local files to a remote or local repository.
git push [variable name] [<location>]
You can also use it to push all branches your remote repo:
git push -all [variable name]
Or delete a branch:
git push [variable name]: [<branch name>]
Get the most recent version of your repo using the git pull command. It’s best to do it daily.
git pull [<repo link>]
Merge changes between branches.
git merge [<branch name>]
There’s another way to merge files. Using git rebase cleans up your commit to make all your changes a single commit. This streamlines your history.
git checkout feature git rebase master
Tuck away files and work on them later with git stash. You can also stash changesets.
<file path> git stash save <file path> git stash pop
Back to top
Compare Git Basic Commands vs. Perforce Commands
Review the following Git commands most commonly used in any developers workflow. Check out how they compare to Helix Core commands.
We also have a Git cheat sheet to help you get started.
|Git Commands||Commands Usage||P4 Commands||Streams Commands|
|git config||Set a variety of parameters for the version control system from the client.|
|git init||Create a new repository.|
|git clone||Setup and begin work on a project from a server on your workstation.|
|git fetch||Get files/changes from a remote server onto your workstation|
|git add||Add a new file.|
To update files, use p4 edit.
To update files, use p4 edit.
|git commit||Record changes to the repository.|
|git diff||Compare changes to files.|
|git reset||Reset your working directory to your last commit.|
|git log||Show a list of commits on a branch.|
|git rm||Remove a file.|
|git status||View all files that need to be committed.|
|git tag||Tag/label a specific revision.|
|git remote||Manage your connections to repos.|
|git checkout||Work in a branch.|
Get a list of all branches, create new local branches, and delete feature branches.
NOTE: p4 branches submits to the server.
|git push||Push changes to a remote server.|
|git pull||Get files from a remote server.||p4 sync|
|git merge||Merge changes between branches.|
|git rebase||Merge and squash down your history.||Helix Core preserves your history.||Helix Core preserves your history.|
Temporarily store modified files locally.
NOTE: p4 shelve stores modified files on the server.
* These commands are used for Helix Core distributed versioning. Learn more about using Helix Core Server for distributed versioning.
📘 Related Resource: SVN Commands Cheat SheetBack to top
Use Git Basic Commands and Still Gain Speed
If you have Git repos and teams using Git, you can still get the power of Perforce. Helix4Git + Helix TeamHub allows you to organize your repos into projects, secure your files, and leverage the Helix Core server for faster build and remote performance.
Check Out Our Git Solution
Learn more about how you can leverage Helix4Git and Helix TeamHub to improve Git performance.