Git Cheat Sheet

Cloning a repository

git clone https://github.com/casacore/casacore.git

Show Repository Origin

git remote show origin

Pretty logs

This will not only display the tree, but also the current labels (including HEAD) and the file modification status

git log --graph --decorate --all --name-status

Log with filenames

git log --stat

Add and remove changes

Add changes

git add <filename or directory>

View staged changes
git diff --cached

Remove staged changes
git reset

Discard changes in the working directory
git checkout -- .

Verify changes before pushing to the remote

Changed files
git log origin/master..HEAD
Diff
git diff origin/master..HEAD

Merging

Simple merge: Checkout the branch you want to merge to and issues the following command
 git merge <branch to merge from>

Show conflicting files
 git diff --name-only --diff-filter=U

Helpful tools

gitk, gitx, tig

Tags

Show tags
git tag

Show tags with commit hashes
git show-ref --tags

Find out which tags point to a references
git tag --points-at 7b33475bd0bf00829e1202f23845e01a31406e04

"Nearest" tag

git describe --abbrev=0 f7b618ae45cf93adee8f01ed98082102324b38b9

Find out the commit that a tag points to
git rev-parse 5.0-mas-6

Submodules

Checkout a source tree with submodules

git clone --recursive git@gitlab.nrao.edu:casa/integration.git  

Update submodule to the latest version (as committed to a given branch)

git submodule update 

Check which commit submodule is pointing to

git submodule 

Check which commit submodule was pointing to at a specific commit

git ls-tree 04a58603be939a1495c53bdd3e6aac99ac15af83

You can also use this to get tree for the latest commit

git ls-tree HEAD^{tree} 

If you forgot to use the recursive when cloning the repository, you can get the submodules with:
cd casa
git submodule update --init --recursive

Fetching pull (GitHub) or merge (GitLab) requests

Open .git/config with your favorite text editor and find the equivalent of
[remote "origin"]
url = http://gitlab.nrao.edu/casa/casa.git
fetch = +refs/heads/*:refs/remotes/origin/*

GitHub: Add the following below the fetch line:

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

GitLab: Add the following the fetch line:

fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

List refs

git show-ref

List only the current branch

git rev-parse --abbrev-ref HEAD

Remove stale remote branch references

Use this if you see non-existent remote branches with "git branch --all"
git remote prune origin

Creating a Mirror

Note: This is not a typical use case as this clones the bare repository as is (i.e. the clone is bare also)

git clone --mirror https://github.com/casacore/casacore.git
cd casacore.git
git push --mirror git@gitlab.nrao.edu:github/casacore.git

The target repository (here git@gitlab.nrao.edu:github/casacore.git) must be empty, i.e. created but with no commits.

Topic revision: r18 - 2016-09-21, VilleSuoranta
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding NRAO Public Wiki? Send feedback