Below is a Git usage proposal I compiled to make everyone's live easier
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)
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
Below are some git trick I use on a daily basis. This tutor is CLI agnostic, but this approach applies to any git tool.
(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
(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
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/
(fix-error) $ git push --force-with-lease
(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
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) $ git add README.md
(change-readme) $ git commit -m 'Added Git HowTo section to Readme'
(change-readme) $ git push
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.
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) $