These are some of the most common Git commands.
You can also downloadable our Printable Git Sheet
Basic Commands:
Command | Description |
---|---|
git init | Initializes a new Git repository in the current directory |
git clone <repo_url> git clone git@git.com:Path/name.git git clone ssh://git@git.com/Path/name.git | Clones an existing Git repository from the specified URL |
git add <filename> git add . git add -A | Adds a file to the staging area |
git commit -m "message" git commit --ammend | Commits changes to the repository with a commit message |
git status | Shows the status of the repository |
git log | Shows the commit history of the repository |
git diff | Shows the changes made to files |
git branch | Shows a list of branches |
git switch <branch_name> | Switches to the specified branch |
git merge <branch_name> | Merges the specified branch into the current branch |
git push git push --force | Pushes changes to a remote repository |
git push <remote> <branch> git push -fu origin <branch> | Forces a push of the local branch to the specified remote branch. |
git pull git pull upstream <branch> | Pulls changes from a remote repository |
git config pull.rebase true | Configure Git to use the --rebase option by default whenever you run git pull. |
Branching:
Command | Description |
---|---|
git branch git branch -a | Lists all local branches in the repository |
git branch <new_branch> | Creates a new branch with the given name |
git switch <branch> git checkout <branch> | Switches to the specified branch In Git version 2.23 and above, git switch has been introduced as a replacement for git checkout . |
git merge <branch> | Merges the specified branch into the current branch |
git branch -d <branch> | Deletes the specified branch |
git push –set-upstream origin <branch> | Push the current branch and set the remote as upstream |
git branch --set-upstream-to=upstream/<branch > | Sets the upstream branch for the current local branch to the specified remote branch. |
Working with Remotes:
Command | Description |
---|---|
git remote -v | Lists all remotes currently configured for the repository |
git remote add <name> <url> git remote add upstream <repo_url> git remote add upstream ssh://git@git.com/Repo/name | Adds a new remote with the given name and URL |
git push <remote> <branch> | Pushes changes to the specified branch on the remote repository |
git pull <remote> <branch> | Pulls changes from the specified branch on the remote repository |
git fetch upstream | Fetches the changes from the remote repository named "upstream" into your local repository. The main difference between git fetch upstream and git pull <remote> <branch> is that git fetch only downloads the changes from the remote repository into your local repository, while git pull not only fetches the changes but also merges them into your current branch. |
Undoing Changes:
Command | Description |
---|---|
git reset <filename> | Unstages a file |
git checkout <filename> | Discards changes to a file |
git revert <commit> | Creates a new commit that undoes the changes made in the specified commit |
git reset --hard | Resets your current branch to the state of the previous commit and discards any changes that you have made since then. |
git reset --hard <commit> | Resets the repository to the specified commit, discarding all changes made after that point. |
git reset --hard upstream/main | Resets your local branch to match the state of the remote branch named “main” in the remote repository named “upstream”. |
git stash -m "message" | Stashes your local changes in a temporary area called the stash, and adds a message to describe the changes that you are stashing. |
git stash list | Shows a list of all stashes that you’ve created in the current repository. |
git stash apply 0 | Applies the changes from the stash with index 0 to the working directory. |
git rebase | Allows you to change the base of a branch. Here’s what it does: It works by taking the changes made in one branch and replaying them on top of another branch. It essentially re-writes the history of the branch, so that it appears as if the changes were made on top of the other branch from the beginning. This can be useful if you want to integrate changes from one branch into another, while preserving a linear history. |
git rebase HEAD~10 -i | Initiates an interactive rebase operation that allows you to modify the commit history of a branch. Here’s what each part of the command does: git rebase is the command that tells Git to start the rebase operation. HEAD~10 is the reference to the commit that is 10 commits before the current HEAD (the tip of the branch). This means that the rebase operation will include the last 10 commits on the current branch. -i stands for “interactive” and tells Git to start an interactive rebase operation, which allows you to manually select and modify the commits that will be included in the new branch history. |
git rebase --continue | Continues a rebase after you have resolved a conflict or made changes to a commit during the rebase process. |
git rebase --abort | Abort a git rebase that’s in progress as shown by a git status |
git restore –staged <file> | Unstage changes from a git commit. The <file> specifies the filename to be removed from the commit and leaves others that may be in the commit. |
There are many more commands and options available, so be sure to check the Git documentation for more information.