git Cheat Sheet

Info

  • git status
  • git log
    • show commits
  • git diff 93fc6e8
    • show diff of a specific commit
  • git remote show origin
    • show info about remote
  • git remote get-url origin

    • show remote url even if unauthenticated

  • git remote -v

Git Clone

Start to work with a repository

  • git clone [url]

Git Update

  • git fetch
    • Get repository from origin, but do not update files
  • git merge origin/master
    • Merge changes from upstream into your local working space, e.g. when you have no internet connection
  • git pull
    • = git fetch + git merge origin/master (shortcut)

Commit

All changed files, including new ones

  • git commit -a -m 'blabla'

Only updated files:

  • git commit -u -m 'blabla'

Remove all non-versioned files

  • git clean -d --force

Revert

  • git restore mydir/

Rollback a merge

  • git reset --hard c3e...a7e

Rollback a file

Diff for last change:

  • git log -p -1 myfile

  • Note the commit hash: a1b2c3...

Revert to last commit before (~1) the noted commit:

  • git checkout a1b2c3...~1 -- myfile

Stash

  • git stash --include-untracked
    • save local changes which prevent pull or when interrupted
  • git stash push -m my_stash
    • Save with a name
  • git pull or do something else
  • git stash list
  • git stash show
  • git stash pop
    • merge local changes back

Overwrite local repo with all changes from master

WARNING: all local changes are lost!

  • git fetch --all
  • git reset --hard origin/master

A single file:

  • git checkout origin/master myfile.txt

Diff between revisions

  • git diff cbb...705 c3e...a7e

Pull and Rebase

  • git config branch.*branch-name*.rebase true
    • or
  • git config branch.autosetuprebase always

eGit:

  • Open the Git Repositories view and navigate to the local branch
  • Open the context menu and select Configure Branch...
  • In the resulting dialog, select the Rebase checkbox

Simple git ignore

  • apt install git-extras
  • git ignore myfile.txt

Remote / Origin

  • git remote -v
  • git remote show origin
  • Reconfigure origin:

Branches

https://www.nobledesktop.com/learn/git/git-branches

What branch am I on?

  • git status

List branches, also remote

  • git branch --all

Switch to branche

  • git checkout mybranch

Switch to a remote branche (1st time)

  • git pull
  • git checkout --track origin/my-branch-name

Create a new branch

  • git checkout -b mybranch

Create local branche on remote

  • git push -u origin HEAD

Commit local changes into new branche

  • git checkout -b my-new-branch
  • git push origin my-new-branch

Delete branch

Local:

  • git branch --delete mybranch

Remote:

  • git push origin --delete mybranch

Reset messy dev branch with master

  • git reset --hard master

Synchronize branch list

  • git fetch -p

Make a branch to the new master

  • git pull
  • git status
    • we are on branch mybranch
  • git branch -m master old-master
  • git branch -m mybranch master
  • git push
  • git push -u origin HEAD ?
  • git push -f origin HEAD:master ?
  • git pull
  • git checkout master
  • git reset --hard origin/master
  • git branch --set-upstream-to origin/master

 

 

 

.gitignore does not work

  • commit changes or you'll loose them with the next command
  • git rm -rf --cached .
  • git add .
  • git commit -m "fixed untracked files"

Unstage

git rm --cached is used to remove a file from the index. In the case where the file is already in the repo, git rm --cached will remove the file from the index, leaving it in the working directory and a commit will now remove it from the repo as well. Basically, after the commit, you would have unversioned the file and kept a local copy.

git reset HEAD file ( which by default is using the --mixed flag) is different in that in the case where the file is already in the repo, it replaces the index version of the file with the one from repo (HEAD), effectively unstaging the modifications to it.

In the case of unversioned file, it is going to unstage the entire file as the file was not there in the HEAD. In this aspect git reset HEAD file and git rm --cached are same, but they are not same ( as explained in the case of files already in the repo)

Git Config

Show config:

  • git config --list

Set a complex option via cmd

  • git config credential.helper 'cache --timeout=3600'

Homedir

vi ~/.gitconfig

 

Accept Self Signed Certificates

  • git config http.sslverify false

Change Name/Email in last commit

  • git commit --amend --author="John Doe <john@doe.org>"

Checkout a specific revision

  • git checkout d8fj28d

Merge dev into master

I generally like to merge master into the development first so that if there are any conflicts, I can resolve in the development branch itself and my master remains clean.

In the dev branche

  • git pull

  • git merge origin/master -m "merged changes from master"

  • git checkout master

    • (switch to master)

  • git merge --no-ff klemens_dev -m 'merged development branch into master'

    • (create a separate commit note to find out later, who did the actual merge to master and at which time)

  • git push

Cherry pick

  • Pick the latest commit from a branche:
    • git cherry-pick myOtherBranche
  • Pick a specific commit from any other branche
    • git cherry-pick 93fc6e8

Create patch files from range of commits

git format-patch 58ce949^..926dc87

git format-patch --stdout 58ce949^..926dc87 > patch1.patch

Apply patches

  • git am *.patch   -> no fuzzy (solution: "-3" option)
  • If that does not work:
    • Revert patch with
      • git am --abort
    • And try
      • git apply -3 xxx.patch
    • Resolve conflicts manually

 

Move existing, uncommitted work to a new branch

Short:

git switch -c myNewBranche

Old / more control:

  • git checkout -b myNewBranche
  • git add ...
  • git commit -m "bla bla"

Commit emtpy folders

  • mkdir fooFolder
  • vi fooFolder/.gitignore
    • *
      !.gitignore

 

Create private Git Repo on Server

On the server as root:

  • adduser example
  • (adduser example ssh-user)

Connect to the server as user:

  • ssh -p 222 example@foo.com
  • git init --bare example-project
  • mkdir ~/.ssh
  • chmod 700 ~/.ssh
  • vi .ssh/authorized_keys
    • Insert your public ssh key

On your dev machine import existing code

  • cd ~/example-project
  • git init
  • git add *
  • git rm --cached a-file-to-ignore.txt
  • git rm --cached -r a-directorye-to-ignore
  • vi .gitignore
    • a-file-to-ignore.txt
      a-directorye-to-ignore/
  • git ad .gitignore
  • git commit -m 'Initial commit'
  • git remote add origin ssh://example@foo.com:222/home/example/example-project
  • git push --set-upstream origin master

 

Import from Subversion (svn)

  • cd /projects/foo_svn
  • svn log --xml --quiet | grep author | sort -u |   perl -pe 's/.*>(.*?)<.*/$1 = /' > ../authors.txt
  • cd ..
  • vi authors.txt
    • joe = Joe <joe@example.com>
      mary = Mary <mary@example.com>
  • git svn clone http://www.example.com/svn/foo/ --no-metadata -A authors.txt --stdlayout --branches=branches/* --tags=tags/* --trunk=trunk foo
  • # then push it to origin as described in "Create private Git Repo on Server" above.