Skip to content

Instantly share code, notes, and snippets.

@NicholasTD07
Last active August 29, 2015 14:24
Show Gist options
  • Save NicholasTD07/6d6e3bbd7f9787a5d393 to your computer and use it in GitHub Desktop.
Save NicholasTD07/6d6e3bbd7f9787a5d393 to your computer and use it in GitHub Desktop.
My Git Workflow

My Git Workflow

g = git

About Fine Grained Git Commits

  • Easier to tell what's changed in one line, preferbally in 50 - 72 characters
  • Easier to revert unwanted, published and shared changes by git revert
  • TO BE CONTINUED

Git Branches

Working on something in a branch shared with others? For example, develop

Create a new branch based on the development base branch, e.g. develop and work there. This way:

  • Everyone shares a common start point for new features or bug fixes
  • Your commits won't influence the shared branch
  • You can easily rewrite the history in your own branch without worrying breaking others' code base

Want commits shared between two branches which have common base point?

Create a branch from the common base point like this:

git checkout -b new-branch `git merge-base one-branch another-branch`

For example, assume the following commits graph, the command above will create the new-branch based on commit B.

A --- B --- C one-branch
       \
        D --- E --- F another-branch

After making all the changes need to be shared, merge this branch from one-branch and another-branch.

Git Commands

Don't know the command/option

How I learned Git, mostly.

All the commands

g help --all

Options of a command

g some-command --help

Check n times before commiting changes

  • Check status g s = git status --short --branch
  • Check diff g d = git diff
  • Add changes
    • Add file(s) g a ... = git add ...
    • Partially Add g ap [...] = git add --patch
    • Add all g aa = git add --all
      • Do this only when you know what will be added in this commit.
  • View cached changes g dc = git cache --diff
  • Commit AND check again when editing commit message g c = git commit --verbose
  • OR, skip adding, commit all changes g ca = git commit --all --verbose (check again when editing commit message)
  • (Optional) Check changes in head g hp = git show --patch --pretty="tformat:"

YES, I check about 5 times before I commit any changes everytime. And, YES, I use git in command line.

File in another branch

View

g show branch:file/path

Checkout

g co branch file/path = g checkout branch file/path

Undo

git revert <commit>...

Use this to revert public and shared commits. It will not mess up your commit history.

Following undos are for commits which are either non-public(un-pushed) or not shared between you and someone else(your own branch).

Undo changes to a file

g co file = git checkout file

Undo staging

g rs = g reset

Undo last commit but keep the changes

g rs "HEAD^" = git reset "HEAD^"

Without the "" zshell will complain.

Undo changes

Use with care. This will also undo all non-staged changes too.

g undo = git reset --hard

Clean out untracker stuff

g clean -d -f

Time Travel, sort of

g undo [<commit>] Go to that commit.

The commit could be a commit in the past, but also one in the "future" (in a remote branch).

For example, assume the following commits graph and local master is one commit behind master.

Running git undo C on local master will bring the branch up to date, if C is fetched.

A --- B --- C Remote master
      \
       Local master

Real Time Travel

Undo undo | Undo anything, almost

g reflog find out the commit you want to go back, possiblly before an undo.

Every change in git has a log/commit in reflog, including reset --hard. So you can undo almost anything with the help of reflog.

g undo the-commit Welcome to 1956 where git does not exist. Just kidding.

Check commits on all branches with graph

10 commits only, fit for one screen

g r = GIT_NO_PAGER=1 git l -10 g ra = g r --all

Full page view in less

g l ~= g log --oneline --graph --format='%C(bold red)%d%Creset %s %C(yellow)%h%Creset' g la = g l --all

Result:

*  (HEAD -> demo/clean-db-show-the-row, origin/demo/clean-db-show-the-row) HACKY code to show the demo row when status bar is tapped ab9970d
*  Clean up database when starting the App for Demo purpose 87503f5
*    (origin/develop, develop) Merge branch 'feature/show-last-read-news' into develop 4627487
|\
| *  (tag: 1.7.4, origin/feature/show-last-read-news, feature/show-last-read-news) Bump version number 1.3.4/1.7.4, #43 2ddbcb9
| *  Extract `didLoadNewsInWebViewWithNewsId` in `RealmNewsViewController`, #43 2b8353d

Original result has color which leads to much better readability. Try the command above to see how actual result look like. See my .gitconfig for the real git la command. The command above is just a approximation.


Thanks for reading. Any feedback is welcomed.
APPENDIX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment