A list of useful git commands
$ git add .
$ git clean -f -d
$ git branch foo
$ git checkout foo
$ git checkout -b foo
$ git branch -D my-branch
$ git branch -av
(and does not delete history)
$ git revert <sha>
$ git reset <sha> --hard
$ git checkout master
$ git merge my-branch1
$ git merge my-branch2
Now resolve conflicts and then:
$ git add .
$ git commit
$ git reset --merge HEAD~1
$ git push git@github.com:goblinhack/zorbash.git <which-branch>
$ git remote add origin git@github.com:goblinhack/zorbash.git
so we can now do
$ git push origin master
$ git pull origin master
$ git remote -v
"origin git@github.com:goblinhack/zorbash.git (fetch)"
"origin git@github.com:goblinhack/zorbash.git (push)"
$ git checkout master
$ git pull origin master
$ git checkout -b my-branch1
Now add some files and stuff
$ git commit -m "..."
$ git push origin my-branch1 # you can repeat this add/push
Then do a pull request
Git fetch only does a download of the latest changes. They are not integrated into your HEAD.
$ git fetch origin
Git pull on the other hand, downloads and integrate and merges with your HEAD.
$ git pull origin master
This replays all commits onto the master and so avoids messy branch history
$ git checkout master
$ git pull origin master
$ git rebase my-branch
$ git commit --ammend -m "my-branch"
$ git add .
$ git commit --ammend -m "my-branch"
$ git log # to get SHAs
$ git checkout -b feature-branch
$ git cherry-pick <SHA>
$ git master
$ git reset --soft <SHA> # resets, but leaves changes in staging
$ git reset <SHA> # resets, but leaves changes in working area
$ git reset --hard <SHA> # resets, and removes changes in staging
# also removes tracked but leaves untracked files
$ git clean -df # gets rid of untracked files (handy to remove
$ an accidental untar/zip in a git repo)
The ‘reflog’ command keeps a track of all changes made in a repository.
$ git reflog
$ git checkout -b test-branch
$ git checkout master
$ git branch -D test-branch
$ git reflog
542359ca (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: checkout: moving from master to test-branch
$ git checkout -b test-branch HEAD@{1}
$ git log -1 HEAD
$ git log --oneline
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
$ git log --after="apr 1" --oneline --decorate
$ git log --after="apr 1" --before="yesterday" --oneline --decorate
$ git log --committer="My name" --oneline
$ git log -n 3
$ git log -n1 -p --format=fuller
$ git log -p --format=fuller
$ git diff -a --submodule=diff origin
$ find .git | grep obj | tail -1
.git/objects/13/2fc0aa1789ce26a97b1bed0a9eefa58b12a96e
$ git cat-file -p 132fc0aa1789ce26a97b1bed0a9eefa58b12a96e
$ echo hello | git hash-object --stdin
$ git merge-base some-branch master
$ git log -n 2 --oneline | tail -2
$ git diff-patch 455f4b22..542359ca
Note, place the older commit first
$ git log -n 2 --oneline | tail -2
$ git format-patch 455f4b22..542359ca -o diffs
diffs/0001-add-goblin.patch
diffs/0002-notes.patch