GIT – Secrets (Cheat Sheet)

Init from the scratch

$> mkdir /path/to/your/project
$> cd /path/to/your/project
$> git init
$> git remote add origin
$> git fetch

First checkout

$> git checkout origin/master
$> git pull origin master

Push to an existing repo

$> cd /path/to/my/repo
$> git remote add origin
$> git push -u origin --all # pushes up the repo and its refs for the first time
$> git push -u origin --tags # pushes up any tags

Copy reposetory

to copy a local reposetory to an external one (PUSH)

$> cd /path/to/my/repo
$> git remote add origin
$> git push -u origin --all # to push changes for the first time

Force branch / commit

$> git fetch --all
$> git reset --hard origin/master [Rev-SHA]
$> git fetch //downloads the latest from remote without trying to merge or rebase anything. Then the git reset resets the master branch to what you just fetched.

Change user:

To change the user that interacts width the reposetory:

$> git config --global "Billy Everyteen"
# Set a new name
$> git config --global
# Verify the setting
# Billy Everyteen

Delete remote branch:

$> git push origin --delete <BRANCH>

Delete all remote branches

$> git branch --merged | grep -v * | xargs git push origin --delete

This deletes all branches in the remote repository that where already merged locally.  Without –merged all branches accept the currently checked out are deleted. The branches have to exist locally.

Delete all local branches

$> git branch --merged | grep -v * | xargs git branch -D

This deletes all already merged local branches. Without –merged all branches accept the currently checked out are deleted.

Delete all orphaned local branches

$> git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

This deletes all local branches, that do not exist on the respective remote repository.

Create new branch

$> git fetch 
$> git checkout -b NewFeature
$> git push origin NewFeature

Checkout single file

For the lastest version:

$> git fetch 
$> git checkout HEAD name_of_file

From a special branch:

$> git fetch
$> git checkout remote/branch path/to/file

For a special version: If you already have a copy of the git repo, you can always checkout a version of a file using a git log to find out the hash-id and then you simply type:

$> git fetch
$> git checkout hash-id path-to-file

Checkout tag

$> git fetch --all --tags
$> git checkout tags/MYTAG -b BRANCHNAME

Revert to revision

$> git checkout commit-id

Revert single file

$> git checkout -- filename

Remove file

File is only removed from the repository. If you skip –cached, the local file will be droped too.

$> git rm --cached myfile.ext

Remove folder

Folder is only removed from the repository. If you skip –cached, the local folder will be droped too.

$> git rm -r --cached myfolder

Completely remove file or folder from repository (also history)

If you in fact want to delete the file / folder from all revisions you can use the following command, that will walk through all commits an deletes the file / folder (-r).

$> git filter-branch --tree-filter 'rm -rf file_or_folder_to.delete' --prune-empty HEAD

–prune-empty will  drop the whole commit, if it was empty after deleting the file / folder.

Afterwards you have to push it to the remote and everybody who was using the repository before has to pull the changes.

Clean local working copy

Resets the local working copy completely to the status of the repository and removes unversioned files too.

$> git clean -fd
# n == dry-run, shows what will be deleted
# -d == delete unversioned folders
# -X == removes only ignored files

Show remote reposetories

$> git remote -v

Get current remot branch

To get current remot branch and corresponding remote branch with last revision number and comment pushed to this branch:

$> git branch -vv


* own 987a3d7 [origin/own] Update

To get a List of all branches and their corresponding remote branches:

$> git branch -avv


* own                   987a3d7 [origin/own] Update
  remotes/dsbackup/own  987a3d7 Update
  remotes/origin/extern 047a76b added manapro.tar.gz
  remotes/origin/own    987a3d7 Update

List remotes

$> git remote -v


PiwikPlugin (fetch)
PiwikPlugin (push)
esoTalkOrg (fetch)
esoTalkOrg (push)
origin (fetch)
origin (push)

Add Identity

If adding a identity is nesseccary but didn’t work with –global:

$> git config ""
$> git config "Max Example"

or add the following lines to .git/config file:

        email =
        name = Max Example

1 thoughts on “GIT – Secrets (Cheat Sheet)

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.