Some helpful Git version control commands
4 min readJan 7, 2023
Here are some of the most important Git commands, along with examples of how to use them:
git clone
: This command is used to clone an existing repository from a remote server to your local machine. For example, to clone a repository from GitHub to your local machine, you can use a command like this:git clone https://github.com/user/repo.git
git add
: This command is used to stage changes for commit. For example, to stage all modified and new files in the current directory, you can use a command like this:git add
git commit
: This command is used to commit changes to the local repository. For example, to commit all staged changes with the message "Add new feature", you can use a command like this:git commit -m "Add new feature"
git push
: This command is used to push committed changes to a remote repository. For example, to push changes to themaster
branch of a repository on GitHub, you can use a command like this:git push origin master
git pull
: This command is used to fetch and merge changes from a remote repository to your local repository. For example, to fetch and merge changes from themaster
branch of a repository on GitHub, you can use a command like this:git pull origin master
git branch
: This command is used to create, list, or delete branches in a repository. For example, to create a new branch calledfeature
based on the current branch, you can use a command like this:git branch feature
git merge
: This command is used to merge one branch into another. For example, to merge thefeature
branch into themaster
branch, you can use a command like this:git merge feature
git log
: This command is used to display the commit history for a repository. For example, to display the commit history for themaster
branch in a compact format, you can use a command like this:git log --oneline --decorate --graph --all
git diff
: This command is used to compare differences between commits, branches, or files. For example, to compare the differences between themaster
andfeature
branches, you can use a command like this:git diff master feature
git stash
: This command is used to temporarily store changes that are not ready to be committed. For example, to stash all modified and staged changes, you can use a command like this:git stash
git reset
: This command is used to undo commits or discard changes. For example, to discard all changes made since the last commit, you can use a command like this:git reset --hard
git tag
: This command is used to add tags to specific commits in a repository. Tags are like labels that can be used to mark important points in the commit history, such as releases. For example, to create a tag calledv1.0
at the latest commit, you can use a command like this:git tag v1.0
git checkout
: This command is used to switch between branches or restore files in the working directory. For example, to switch to thefeature
branch, you can use a command like this:git checkout feature
git fetch
: This command is used to retrieve new commits from a remote repository without merging them into the local repository. For example, to fetch new commits from theorigin
repository, you can use a command like this:git fetch origin
git blame
: This command is used to display information about who last modified each line of a file. For example, to display the commit history for each line of a file calledmain.py
, you can use a command like this:git blame main.py
git grep
: This command is used to search for patterns in the files of a repository. For example, to search for the pattern"TODO"
in all Python files in the repository, you can use a command like this:git grep "TODO" -- "*.py"
git gc
: This command is used to clean up the repository and optimize its performance. For example, to remove unreachable objects and compress the file system, you can use a command like this:git gc --aggressive
git cherry-pick
: This command is used to apply the changes from a specific commit to the current branch. For example, to apply the changes from the commit1234567
to the current branch, you can use a command like this:git cherry-pick 1234567
git am
: This command is used to apply patches to the repository in the format of email messages. For example, to apply a patch stored in a file calledpatch.txt
, you can use a command like this:git am patch.txt
Enjoy pals!