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 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 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 remote branch

  • git push origin --delete mybranch

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

Homedir

vi ~/.gitconfig

 

Accept Self Signed Certificates

 

  • git config --global http.sslverify false

 

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 development (create a separate commit note to find out later, who did the actual merge to master and at which time)

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"