DEV Community

box
box

Posted on • Edited on

Git Command Cheat Sheet

Environment

  • Git
    • Version: 2.45.0

Commands

git add

Add file contents to staging.

git add <pathspec>
Enter fullscreen mode Exit fullscreen mode

We can specify a file path for <pathspec>.
Or we can add all files insted of a specific file.

git add .
Enter fullscreen mode Exit fullscreen mode

git branch

List local branches.

git branch
Enter fullscreen mode Exit fullscreen mode

We can use option --remotes in order to list remote branches.

git branch --remotes
Enter fullscreen mode Exit fullscreen mode

We can use -r instead of --remotes.

git branch -r
Enter fullscreen mode Exit fullscreen mode

delete

Option --delete can be used to delete a branch.

git branch --delete <branch>
Enter fullscreen mode Exit fullscreen mode

We can use -d instead of --delete.

git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode

If a branch has not been fully merged into its upstream branch, we cannot delete it.
However, combined with option --force, we can delete it irrespective of its merged status.

git branch --delete --force <branch>
Enter fullscreen mode Exit fullscreen mode

We can use -f instead of --force.

git branch -d -f <branch>
Enter fullscreen mode Exit fullscreen mode

Furthermore, we can use -D instead of -d -f.

git branch -D <branch>
Enter fullscreen mode Exit fullscreen mode

git checkout

Switch current branch to another branch on local.

git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

If the branch specified by <branch> does not exist, the switching will fail of course.
However, with option -b, a new branch is created based on current branch.

git checkout -b <new-branch>
Enter fullscreen mode Exit fullscreen mode

Or we can explicitly specify a base branch or commit hash as <start-point>.

git checkout -b <new-branch> <start-point>
Enter fullscreen mode Exit fullscreen mode

We can also specify a remote branch as base branch.

git checkout -b <new-branch> origin/<remote-branch>
Enter fullscreen mode Exit fullscreen mode

git clone

Clone a repository into current directory.

git clone <repository>
Enter fullscreen mode Exit fullscreen mode

The cloned repository is named origin, and we specify that name as <repository> when we execute git push.
Instead of the name origin, we can give it another name.

git clone --origin <name> <repository>
Enter fullscreen mode Exit fullscreen mode

We can use -o instead of --origin.

git clone -o <name> <repository>
Enter fullscreen mode Exit fullscreen mode

git commit

Apply changes to the repository.

git commit
Enter fullscreen mode Exit fullscreen mode

We can use option --message in order to write commit message.

git commit --message <msg>
Enter fullscreen mode Exit fullscreen mode

We can use -m instead of --message.

git commit -m <msg>
Enter fullscreen mode Exit fullscreen mode

git log

Show commit logs.

git log
Enter fullscreen mode Exit fullscreen mode

git merge

Merge the commit history of a branch to current branch.

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

git push

Update remote branch using local branch.

git push <repository> <refspec>
Enter fullscreen mode Exit fullscreen mode

<refspec> can be specified in the format <src>:<dst>.

git push <repository> <src>:<dst>
Enter fullscreen mode Exit fullscreen mode

We can specify local branch for <src> and remote branch for <dst>.

git push <repository> <local-branch>:<remote-branch>
Enter fullscreen mode Exit fullscreen mode

If the names <local-branch> and <remote-branch> are the same, we can be written once.

git push <repository> <branch>
Enter fullscreen mode Exit fullscreen mode

force

If the commit history has been updated by git rebase, the push might fail.
In this case, option --force-with-lease can be used to overwrite the commit history of the remote branch.

git push --force-with-lease <repository> <refspec>
Enter fullscreen mode Exit fullscreen mode

If someone else had updated the remote branch, even if we used --force-with-lease, the push will fail.
Even if someone else has done, using option --force instead of --force-with-lease, we can update if we want.

git push --force <repository> <refspec>
Enter fullscreen mode Exit fullscreen mode

We can use -f instead of --force.

git push -f <repository> <refspec>
Enter fullscreen mode Exit fullscreen mode

git pull

Incorporate changes from a remote repository into the current branch.

git pull
Enter fullscreen mode Exit fullscreen mode

git rebase

Reapply commits on the commit history of the specified branch.

git rebase <base-branch>
Enter fullscreen mode Exit fullscreen mode

git reset

Reset file contents from staging.
This command is opposite of git add.
We can specify a file path for <pathspec>.

git reset <pathspec>
Enter fullscreen mode Exit fullscreen mode

We can use this command to roll back the current branch head as well.

The current branch head is represented as HEAD.
Using this notation, we can specify the commit we want to roll back to relatively.

One method is to use the format HEAD~\d+.
\d+ indicates the number of generations relative to the current branch head.
For example, HEAD~2 refers to the commit 2 generations prior to the current branch head, and HEAD~0 has the same meaning as HEAD.

git reset HEAD~2
Enter fullscreen mode Exit fullscreen mode

Another method is to use the format HEAD\^+.
^ represents the parent commit of the starting commit.
For instance, HEAD^^ refers to the parent of the parent commit of the current branch head.
This is equivalent to HEAD~2.

git reset HEAD^^
Enter fullscreen mode Exit fullscreen mode

The changed files are removed from staging and returned to the working tree.
This is the same as specifying --mixed option.

git reset --mixed  HEAD^^
Enter fullscreen mode Exit fullscreen mode

If we want to leave them in staging, use --soft option.

git reset --soft  HEAD^^
Enter fullscreen mode Exit fullscreen mode

If we want to discard them, use --hard option.

git reset --hard  HEAD^^
Enter fullscreen mode Exit fullscreen mode

git revert

Revert changes in a commit.

git revert <commit>
Enter fullscreen mode Exit fullscreen mode

We specify a commit hash as <commit>.

git stash

Save uncommitted changes and revert working dir to match the HEAD commit.

git stash push
Enter fullscreen mode Exit fullscreen mode

push can be omitted.

git stash
Enter fullscreen mode Exit fullscreen mode

To save paths that are not tracked in Git (e.g. newly added ones) as well, we can use option --include-untracked.

git stash --include-untracked
Enter fullscreen mode Exit fullscreen mode

We can use -u instead of --include-untracked.

git stash -u
Enter fullscreen mode Exit fullscreen mode

Saved changes can be displayed by executing with list.

git stash list
Enter fullscreen mode Exit fullscreen mode

A saved changes are named stash@{n}.
n is an index starting from 0 and newer saved change has smaller index.

We can restore a change by specifying the name above.

git stash pop stash@{n}
Enter fullscreen mode Exit fullscreen mode

A restored change is deleted from stash list.
If we want to keep that in the list, use apply instead of pop.

git stash apply stash@{n}
Enter fullscreen mode Exit fullscreen mode

Then we delete it when we want.

git stash drop stash@{n}
Enter fullscreen mode Exit fullscreen mode

git status

Show the working tree status include following.

  • Changes to be committed.
    paths that have differences between the staging file and the current HEAD commit.

  • Changed not staged for commit.
    paths that have differences between the working tree and the staging file.

  • Untracked files.
    paths in the working tree that are not tracked by Git.

The former one is committed when git commit is executed, but the latter two are not.

git status
Enter fullscreen mode Exit fullscreen mode

git --version

Show the version of Git installed in a local environment.

git --version
Enter fullscreen mode Exit fullscreen mode

We can use -v instead of --version.

git -v
Enter fullscreen mode Exit fullscreen mode

Top comments (0)