Git

From Dogtag
Jump to: navigation, search

Pulling 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

Stash

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

Patches

Applying Patches

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

Generating Patches

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>

Commits

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@address.com>" --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>

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

Submodules

Retrieving files in a submodule

$ git submodule update --init -- <submodule path>

Merging submodule

$ mv <submodule path> <submodule path>.tmp
$ git submodule deinit -f -- <submodule path>
$ rm -rf .git/modules/<submodule path>
$ rm -rf <submodule path>
$ rm -rf skins/.gitignore
$ mv <submodule path>.tmp <submodule path>

Subtrees

$ git subtree add --prefix=lib/commons-lang-3.11 https://github.com/apache/commons-lang.git rel/commons-lang-3.11 --squash
$ git subtree add --prefix=lib/jaxb-api-2.3.3 https://github.com/eclipse-ee4j/jaxb-api.git 2.3.3 --squash
$ git subtree add --prefix=lib/jaxb-ri-2.3.3-RI https://github.com/eclipse-ee4j/jaxb-ri.git 2.3.3-RI --squash
$ git subtree add --prefix=lib/commons-io-2.8.0 https://github.com/apache/commons-io.git rel/commons-io-2.8.0 --squash
$ git subtree add --prefix=lib/Resteasy-3.0.26.Final https://github.com/resteasy/Resteasy.git 3.0.26.Final --squash

Migration

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

References