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.
git config
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]”
git init
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
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.
git clone https://github.com/username/gitexample.git
git add
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>
git commit
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”
git diff
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>…]
git reset
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
git log
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”
git rm
Stop tracking files and remove them from working directory.
git rm <file name>
git status
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”)
git tag
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’
git remote
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>]
git checkout
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>]
✨ Switch faster using Perforce Streams ✨
git branch
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>]
git push
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>]
git pull
Get the most recent version of your repo using the git pull command. It’s best to do it daily.
git pull [<repo link>]
git merge
Merge changes between branches.
git merge [<branch name>]
📘 Related Resource: Compare Git Rebase vs. Merge
git rebase
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
git stash
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. | P4CONFIG | P4CONFIG |
git init | Create a new repository. | p4 init* | |
git clone | Setup and begin work on a project from a server on your workstation. | p4 sync | p4 sync |
git fetch | Get files/changes from a remote server onto your workstation | p4 sync | p4 sync |
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. | p4 submit | p4 submit |
git diff | Compare changes to files. | p4 diff | p4 diff |
git reset | Reset your working directory to your last commit. | p4 clean | p4 clean |
git log | Show a list of commits on a branch. | p4 changes | p4 changes |
git rm | Remove a file. | p4 delete | p4 delete |
git status | View all files that need to be committed. | p4 status | p4 status |
git tag | Tag/label a specific revision. | p4 tag | p4 tag |
git remote | Manage your connections to repos. | p4 remote* | p4 remote* |
git checkout | Work in a branch. |
| p4 switch |
git branch | Get a list of all branches, create new local branches, and delete feature branches.
NOTE: p4 branches submits to the server. | p4 branches | p4 switch -l |
git push | Push changes to a remote server. | p4 push* | p4 submit |
git pull | Get files from a remote server. | p4 pull* | 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. |
git stash | Temporarily store modified files locally.
NOTE: p4 shelve stores modified files on the server. | p4 shelve | p4 shelve |
* These commands are used for Helix Core distributed versioning. Learn more about using Helix Core Server for distributed versioning.
📘 Related Resource: SVN Commands Cheat Sheet
Back to topUse 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.
And if you have Mercurial or SVN repos, we support them too.
Check Out Our Git Solution
Learn more about how you can leverage Helix4Git and Helix TeamHub to improve Git performance.