Skip to content

Instantly share code, notes, and snippets.

@pirj
Last active March 15, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirj/0c57a0cf596767c0f087 to your computer and use it in GitHub Desktop.
Save pirj/0c57a0cf596767c0f087 to your computer and use it in GitHub Desktop.
Git with ease

Below is a Git usage proposal I compiled to make everyone's live easier

Reason

Our repo is flooded with merge commits, and as team and topic branch number grows, this is going to grow like a snowball and prevail over regular commits (it already does by the way)

Rationale

Keep flat history Pull requests are always up to date with master Pull request commits are always contignous, no other commits in between

You still can use git log --no-merges, but it's unlikely that UI tools (including GitHub's web UI) have that option unfortunately

Synopsis

Below are some git trick I use on a daily basis. This tutor is CLI agnostic, but this approach applies to any git tool.

Checkout and init new feature branch

(previous-branch) $ git checkout master # Checked out
(master) $ git pull --rebase # Now up to date with remote master, it also fetches the rest of the changes
(master) $ git checkout -b fix-error # Checkout new topic branch derived from master

Working on your feature or fix

(fix-error) $ git add app && git commit -m 'WIP attempt 1 at fixing an error' # Fixing, but not yet sure
(fix-error) $ git push # pushing to origin/fix-error to trigger CI build
(fix-error) $ git fetch origin master # Fetch latest changes from remote
(fix-error) $ git rebase origin/master # Get on the same page with those changes. Maybe you will have to merge, but in most cases this is done automatically
(fix-error) $ git add app && git commit -m 'WIP yet another attempt to fix' # Another fix
(fix-error) $ git push --force-with-lease # Push, overwriting the history, but without risk to overwrite someone else changes to same branch

Keep your commits clean and readable

Now we need to clean up the WIP mess

(fix-error) $ git reset origin/master # All your changes will go to 'working directory', e.g. won't be staged or commited. You can rearrange, commit grouping commits by logical portions et c.

Or

(fix-error) $ git rebase -i origin/master

Make sure you add trello card link to at least one of the commits, or a short-hash of it, e.g.

[dFte4gDw] fix stupid typo

for a card https://trello.com/c/dFte4gDw/112-fix-stupid-typo Now it's easy to get to the ticket by just adding that magic number to https://trello.com/c/

Push your shiny fix and shiny history

(fix-error) $ git push --force-with-lease

Wait for CI to pass all checks

It could be someone pushes to master before you, you have to align with latest master again:

(fix-error) $ git fetch origin master
(fix-error) $ git rebase origin master
(fix-error) $ git push --force-with-lease

or with a "single" command:

(fix-error) $ git fetch origin master && git rebase origin master && git push --force-with-lease

Merge your PR from GitHub UI

Bonus

There's one more thing that might help to minimize git log of codeclimate struggle (e.g. several commits named 'removed whitespace') called amend. It's basically as adding more changes to the last commit being made.

Change README.md

Add and commit the change, then push

(change-readme) $ git add README.md
(change-readme) $ git commit -m 'Added Git HowTo section to Readme'
(change-readme) $ git push

CodeClimate detects that there is a trailing whitespace, or line is over 80 chars long.

You fix README file, and this time you don't want to add yet another commit.

(change-readme) $ git add README.md
(change-readme) $ git commit --amend

You add your README.md file changes into a previous commit! Now you can push (with force-lease) and noone will ever see your frictions trying to gratify nasty CodeClimate.

Don't be affraid!

There's nothing lost forever with git. When you commit amend or rebase, those branches that you had previously are still there. You can find references to them on GitHub, using git reflog, and once you have that abbreviated commit name (e.g. 7b4accd9), you can switch HEAD to that commit:

(master) $ git checkout 7b4accd9

and give that branch a name so that you can add commits to it, push and get back to it quickly:

(7b4accd9) $ git checkout -b name-that-branch
(name-that-branch) $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment