Skip to content
Endi S. Dewata edited this page Aug 3, 2023 · 3 revisions

Overview

This page describes commonly used git commands.

Pulling Changes

To pull the latest changes:

$ git pull

Branches

To checkout an existing branch:

$ git checkout <branch>

To create a new branch from HEAD:

$ git checkout -b <branch>

To create a tracking branch:

$ git checkout -b <branch> <repository>/<branch>

To configure the current branch to track a remote branch:

$ git branch -u <repository>/<branch>

To delete a remote branch:

$ git push <repository> --delete <branch>

To compare files from different branches with GUI tool:

$ git config --global diff.tool meld
$ git difftool <branch1>:<file1> <branch2>:<file2>

To list recently modified branches:

$ git for-each-ref --sort=-committerdate refs/heads/ | less

Stashing Changes

To stash uncommitted changes:

$ git stash

To list stash entries:

$ git stash list

To show the stash content:

$ git stash show -p <stash ID>

To restore uncommitted changes:

$ git stash pop

Committing Changes

To create an empty commit:

$ git commit --allow-empty

To amend the changes:

$ git commit --amend --no-edit

To amend the author:

$ git commit --amend --author="Author Name <[email protected]>" --no-edit

To amend the date:

$ git commit --amend --date="$(date -R)" --no-edit

Cherry-Picking Changes

To cherry-pick all new changes in a branch into master:

$ git checkout master
$ git cherry-pick master..mybranch

To checkout a file from another branch

$ git checkout <branch> <path>

Generating a Patch

To generate a patch file for the last commit:

$ git format-patch -1

To generate a patch file for a specific commit:

$ git format-patch -1 <commit>

To generate patch files for each commit since a tag:

$ git format-patch <tag>

To generate a single patch file for all commits since a tag:

$ git format-patch --stdout <tag> > <patch>

Applying a Patch

To apply a patch:

$ git am <path to patch>

If it fails due to conflict, try 3-way merge:

$ git am -3

If it still fails, check the failed files with the following command:

$ git status

Fix the file manually, then add the fixed file:

$ git add <path to file>

Then continue the commit:

$ git am --continue

Confirm the commit with the following command:

$ git log

Pushing Changes

To push a local branch to a remote repository:

$ git push <repo> <local branch>

To push a local branch to a remote repository with a different name:

$ git push <repo> <local branch>:<remote branch>

Tags

To list tags:

$ git tag

To list tag messages:

$ git tag -l -n1

To create a tag:

$ git tag <name> [<branch/commit>] [-m "<message>"]

To push a tag:

$ git push origin <name>

To fetch tags:

$ git fetch --tags

To delete a tag:

$ git tag -d <name>

To delete a remote tag:

$ git push --delete origin <name>

Archive

To create a tarball:

$ git archive --format=tar.gz --prefix <prefix>/ -o <output> <commit>

To verify the tarball:

$ tar tvf <output>

Logs

To view one-line logs:

$ git log --pretty=oneline --abbrev-commit --no-decorate

To view one-line logs since a certain tag:

$ git log --pretty=oneline --abbrev-commit --no-decorate v10.5.6..DOGTAG_10_5_BRANCH

Cleaning Local Repository

$ git clean -dxf

Migration

$ git clone --mirror <old URL>
$ cd <repo>
$ git remote add new-origin <new URL>
$ git push new-origin --mirror

See Also

Clone this wiki locally