Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git (Lunch & Learn at Greane Tree Technology)

Git (Lunch & Learn at Greane Tree Technology)

This was a presentation for the company I work at, Greane Tree Technology, in our Lunch & Learn program. It's a recurring event where we present on something new we've learned recently.

Some features in particular that I reviewed here are interactive adding, interactive rebase, some workflows for cleaning up commits, and more. Interactive adding is a simple way to stage commits with ‘hunks’, small chucks of code, rather than the entire file. Interactive rebase is a great feature of git that allows you to reorganize commits in a number of ways.

Chad Calhoun

June 28, 2013
Tweet

Other Decks in Technology

Transcript

  1. Developer Setup a. Fork project from project owner on GitHub

    b. Clone fork on local machine i. git clone git://github.com/username/app. git c. Add upstream for original project i. git remote add upstream git://github. com/project_owner/app.git
  2. Start a feature a. Start a feature branch i. git

    branch <new_branch> ii. git checkout <new_branch> or the shortcut git checkout -b <new_branch> b. Develop the feature i. def do_some_work ... end
  3. Adding changes a. Commit process i. Selectively add related changes

    into separate commits ii. git add -i i. Interactive staging allows you group commits into related edits easily. ii. Split up files into 'hunks' and stage them separately. iii. git commit -m “Added <some feature enhancement>”
  4. Interactive Add Select patch and stage changes by small 'hunks'.

    There are many options for navigating around the changes.
  5. Commit changes a. When you are finished staging your changes

    commit your changes i. git commit -m “Added <some feature enhancement>”
  6. Unhappy with your commits? a. Compare changes i. git diff

    HEAD changes since last commit ii. git diff HEAD^ or HEAD~1 parent of last commit iii. git diff HEAD^^ or HEAD~2 grandparent commit b. git reset is git's ctrt+z i. git reset HEAD unstage all files ii. git reset HEAD <file> unstage a file iii. git reset --soft HEAD^ remove a commit and keep the changes iv. git reset --hard HEAD^ remove commit(s) and trash changes
  7. Undo your undo i. Didn’t mean to reset that commit?

    That’s Ok! Git doesn’t delete it. ii. git reflog shows all of your history including resets iii. git log --walk-reflog more verbose reflog iv. git cherry-pick <sha> Copies a single commit into the current branch. It works for any commit including commits that have been reset.
  8. Interactive Rebase i. What does it do? 1. Reorder reword,

    edit, delete, or combine commits ii. First, find the commit you want to start from 1. git log --oneline or git reflog iii. Run the rebase 1. git rebase -i <sha> or (respectively) git rebase -i HEAD@{3}
  9. Details on Interactive Rebase i. What can you do? 1.

    pick - chooses a commit 2. reword - rename a commit 3. edit - lets you edit a commit 4. squash - use the commit but meld into previous commit 5. fixup - like “squash” but discards this commit’s log message 6. exec - run command (the rest of the line) using shell 7. Commits can be reordered by moving up/down 8. Commits can also be deleted by removing a line
  10. I committed a migration in with some refactoring code! That

    shouldn’t be in there! i. Mark that commit as ‘edit’ ii. Let the rebase replay commits until it stops on that commit. iii. This has played that commit already. Undo that commit while keeping the changes. git reset HEAD^ iv. Now, you can re-commit your changes properly v. git rebase --continue when you are done Why 'edit' can be helpful ” ”
  11. Integrate upstream a. Integrate changes your coworkers made into your

    <feature_branch> i. git checkout master ii. git fetch upstream iii. git pull upstream master iv. git checkout <feature_branch> v. git rebase master
  12. Need to review changes? git log commit 1f569f4219067bc695761c6ae1ea89027e14361c Author: Chad

    Calhoun <[email protected]> Date: Thu Jun 27 06:21:50 2013 -0400 Adding a User Model git log --oneline 1f569f4 Setting up Authentication 3750cfa Adding a User Model git log -p Shows diff broken down by commit and file git log --stat Shows diff stats broken down by commit and file git show <sha> shows a diff of a specific commit
  13. Pull Requests i. Submit a pull request 1. Have a

    coworker review your work and add comments 2. Make changes and add additional commits as needed 3. git push origin <new_branch> again 4. Coworker accepts the pull request a. Pull merged changes locally i. git checkout master ii. git fetch upstream iii. git pull upstream master