My GitHub (git) cheat sheet

  1. clone the repo from GitHub
  2. check out a development branch - make all changes with this branch
  3. push the development branch to GitHub
  4. create a Pull Request at GitHub

Use git at the command line

Create a new repository on GitHub. Then locally inside the source directory

#echo "# openqmail" >> README.md
$ git init
$ git add README.md
$ git commit -m "initial commit"
$ git remote add origin https://github.com/kai-peter/openqmail.git
$ git push -u origin master
$ git remote add origin https://gitlab.com/kaili/qwebsite.git
$ git push origin master

Config Settings

$ git config user.name "Kai Peter"
$ git config user.email user@example.org
$ GIT_COMMITTER_NAME='kp' git commit --author="Kai Peter <user@example.org>" -m "Initial version"
$ git remote add origin https://github.com/kp-org/eQmail.git

Branches

Create a new branch “dev” (from master) on GitHub. Clone the repo and switch to branch “dev”:

$ git clone https://github.com/kp-org/eQmail.git
$ git checkout -b dev
$ git branch
$ * dev
$  master

Merge a dev branch into master

$ git checkout master
$ git merge dev

Delete the merged (old) branch (example: dev):

$ git branch -d dev

Checkout an older state: Create a new branch and check it out:

$ git checkout -b <branch> a9c146a09505837ec03b

Make changes and upload to remote GitHub repo:

$ git add *
$ git add .gitignore
$ git commit -a -m "<comment>"
$ git push origin dev

Hint::  git add  doesn't show what was added. Use  git status  to see what was added.

Do not track changes at '.gitignore':  git update-index –assume-unchanged .gitignore  Thus '.gitignore' have not be committed, but changes are the repo.

Remove files

Remove file from .git only:

$ git rm --cached <file>

Remove file completely: This removes <file> from filesystem too!

git rm <file>

Remove all untracked files and directories (-d):

git clean -fd

Handle commits

Remove files from/after git add:

$ git reset [*|file]

Edit comment of last commit or (optional) an older commit:

$ git commit --amend [-c <commit-hash>]

Undo last commit:

$ git commit -m "Something terribly misguided"              (1)
$ git reset HEAD~                                           (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)

Reset to an older commit:

git reset <commit-hash>

Rebase

Get changes from remote repository (rebase, re-staging):

$ git pull https://github.com/kp-org/xdkim.git