Skip to content

Instantly share code, notes, and snippets.

@weiland
Last active June 12, 2022 15:40
Show Gist options
  • Save weiland/831677d42ab356b73a7f to your computer and use it in GitHub Desktop.
Save weiland/831677d42ab356b73a7f to your computer and use it in GitHub Desktop.
Git Cookbook

Git Cookbook

install git

On Mac:

brew install git # homebrew

sudo port install git-core +bash_completion+credential_osxkeychain # Macports

On other *NIX:

apt-get install git # aptitude (ubuntu, debian)

On windows: "xD"

And get a github.com (more social, used for hosting many open source projects) or bitbucket.org (free private repositories)

set up git config

see your current configuration:

git config --list # current directory

git config --list --global # global git settings (can be stored in ~/.gitconfig)

set your configuration:

git config user.name 'author name' # your name (visible in each commit)

git config user.email author@email.com # i prefer using my github/bitbucket email

basic flow of a new git repository

git init

you initialized a git repository in the current folder. now you are on master (you are working in the master branch which is the default branch)

touch yourfile

the file yourfile is created but not checked in (staging) or even commited

git add yourfile

the file is checked in (added or on staging, but not commited)

git commit -a -m 'i added a file'

the file is commited (in your local git repository)

git remote add origin git://git@github.com/username/repository

an 'online' remote (to github) with the name 'origin' is added

git push -u origin master

the commit is pushed to the remote's master branch and due to -u master is the tracking branch so the next time git push is enough (to push to origin master remote)

Tipp for a new repository start with adding a .gitignore which contains files and folders you don't want to have in git (e.g. secret config files, generated content such as compiled and/or minifed css/js) gitignore.io will help you.

And add a README.md where you can deliver certain information about your project. (github.com shows readmes by default)

cloning a repository (from githuby, bitbucket etc)

if you have a 2 factor authentication enabled and you want to clone a private repository, use git@github.com:user/repo.git over https://github.com/user/repo (which i do generally)

git clone git@github.com:user/repo.git # clones into the new directory repo/

git clone git@github.com:user/repo.git own-folder-name # specify the local folder name for the cloned content

git clone git@github.com:user/repo.git . # clones into current directory

when you clone, it also adds the remote origin pointing to the source repository git@github.com:user/repo.git

after cloning one might want to change the origin uri:

git remote set-url origin git@github.com/youruser/repo.git

helpful commands for logging, current status

(each commit has a unique SHA1 code, to identify it)

git status

git diff # non-staged and non-added changes

git diff --cached # staged but non-commited changes

git diff SHA1 # changes between a commit's SHA1 and HEAD

git diff --name-status master..mybranch

git diff SHA1 otherSHA1 # comparing two commits (via their SHA1 codes)

git diff --name-only SHA1 otherSHA1 # receive only the names of the changed files

git log -p filename # chnages of a certain file (by filename)

git log # standard git log

git log --pretty=oneline # show each commit in one line

git reflog

git branch -r # list all branches (local and remote branches)

git remote -v # list all remotes

git show COMMITSHA1

when you want to remove or move a file in a git repository, use:

git rm file # to remove a file

git rm -rf directory # to remove a directoy recursivly

git mv here there # to move a file to a new destination

over the normal rm, mv commands!

Helpful commands and tricks

** Create and checkout local branch**
git checkout -b branch

** Checkout a remote branch**
git checkout -b localBranch origin/remoteBranch

** get all branches from origin**
git fetch origin

** Change last commit message (in case of typo)**
git commit --amend

** Undo last commit**
git revert HEAD^ # see also git reset below

** delete remote branch**
git push origin :branch

** delete local branch (you cant be in that branch)**
git branch -d branch
git branch -D branch # -D is forcing it (in case -d faile, which means there are some untracked changes in the brachn)

** deleting remote**
git remote remove remotename

** Merging 2 branches**
git checkout master
git merge finished-working-branch

** On that lovely message "your branch is 3 commits.."**
git fetch --depth=HEAD

** fetch the lastest things of a remote shared branch**
git fetch <remote>

** changes checked in files**
git add --edit

** ignore/remove last changes of file**
git checkout -- file

** removes file from staging (unadding it)**
git reset FILE

** cherry-pick a commit of a branch**
git checkout working-branch
git log
# Get the commit's SHA1
git checkout master
git cherry-pick the_sha1

** git rebase is always good!**
git pull --rebase origin master
git rebase --abort # to abort the rebasing
# or adding a resolved file
git add editedFile
git rebase --continue

** Set tracking branch (to remember) -u**
git push -u origin parent

** Git: 'Clone' only a certain commit of a branch**
(ok we fetch actually)

add the remote
git remote add origin git@github.com:user/repo

fetch the commit (sha1 of the commit is required)
git fetch origin commit's-sha1

reset master to the fetched commit
git reset --hard FETCH_HEAD

** undo non pushed commits**
git reset origin/master

** Get certain file from from another branch**
git checkout another-branch app/newCoolFile.py

** reset your branch with the remote (all your local changes get lost)**
git fetch origin
git reset --hard origin/master

Working: Pull Requests (Collaboration in own projects)

git checkout master
git pull
git fetch
git reset --hard origin/master

git pull
git checkout -b newPRBranch
touch test
git add .
git commit -a -m 'commit message'
git push

Then on GitHub.com there is that lovely green button

Forking Workflow & Pull Request

  • Fork it ( https://github.com/user/repo/fork )

  • Create your feature branch (git checkout -b my-new-feature)

  • Commit your changes (git commit -am 'Add some feature')

  • Push to the branch (git push origin my-new-feature)

  • Create a new Pull Request

  • hit fork button (on github etc)

  • clone your fork of that repository git clone https://github.com/yourUsername/reponame.git # you are origin

  • add original git remote add parent https://github.com/originalUser/reponame.git

  • fetch from it git fetch parent
    (in case you dont want to cotribute to the original one: push the changes only to your repo git push origin master)

  • create a working branch git checkout -b my-cool-feature

  • do changes touch feature.pl

  • add & commit them git add . && git commit -a -m 'added my cool feautue'

  • push it to GitHub git push origin my-cool-feature
    on gh, you can request the PullRequest

  • fetch new changes from the original repo (in case there were new changes:
    git fetch upstream
    git merge upstream/master

Git reset soft/hard

git reset --soft HEAD^ # resets previous commit, you still have the local changes (uncommited)
git reset --hard HEAD^ # undo previous commit and delete local changes
git reset --hard HEAD~1
git reset --hard commitSHA1 # reset hard to certain commit, get the (right) SHA1 via git log
git reset --mixed HEAD~1 is git reset HEAD~1 # undo last commit, preserving local changes in index

Git stash

use case: oops i worked on master instead in an own working branch,
git stash # stashes out my (not commited) changes
git checkout branch my-working-branch # creates the working branch
git stash pop # puts back my changes

or (in your working branch): oops i forgot to pull from master before (so this is very likely not to be mergeable later )
git stash # stashes out my (not commited) changes
git pull origin master # pulls from master git stash pop # puts back my changes (perhaps there are some conflicts)

or you just remove your stashed changes :
git stash drop

Git hard reset (in case of passwords or other sensible data was commited and pushed)

(if not pushed, a git reser hard/soft is enough)

(get them via git log)
A -> B -> C

B introduces passwords

you are on C

git reset --hard A
git cherry-pick C
git push --force origin <branch>

Dealing with passwords or secret configs

You have two files, the real one which contains the passwords that the app can run, and a sample one which shows the structure, so co-workers can copy the sample to the 'real name' and fill in their own passwords.
By adding the 'real file' to .gitignore we make sure, that this file is not controlled or checked in via git.

contentns of .gitignore:
password.file

contents of password.file: password: 'my real password'

contents of password.file.sample
password: 'here goes your password

cp password.file.sample password.file # copying the sample to the real one
vim password.file # adding your own password git status # will not show any changes due to the changed file is listed in the .gitignore

in the end: clean up with

git clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment