Changes between Version 36 and Version 37 of WorkingConventions/Git


Ignore:
Timestamp:
Sep 13, 2013 3:54:30 AM (2 years ago)
Author:
thoughtpolice
Comment:

Refactor new info

Legend:

Unmodified
Added
Removed
Modified
  • WorkingConventions/Git

    v36 v37  
    6565When working with GHC, there are a lot of ways you can use Git to make your life easier. Below are some of them:
    6666
    67  * '''Selectively add changes to commit, darcs-style.''' Do you miss Darcs? Do you hate it when a file contains a bugfix *and* a new feature, and you want to commit both separately? That's OK! Just run:
     67=== Selectively record changes to commit ===
     68
     69Do you miss Darcs? Do you hate it when a file contains a bugfix *and* a new feature, and you want to commit both separately? That's OK! Just run:
    6870{{{
    6971$ git add -p
    7072}}}
    71  This opens the '''interactive diff selector''', which behaves a lot like `darcs record`. It will go through every change you have made in the working tree, asking if you want to `git add` it to the index, so you can commit it afterwords.
    72 
    73  '''Nota bene''': this only adds files ''to the index'', it does not commit them. Afterwords, you may commit the result using `git commit`. '''Do not use `git commit -a`''', or you will just add all the changes to the commit!
    74 
    75  * '''Selectively merge a singular change, darcs-style.''' You still miss Darcs. One thing that would be great is if you could just 'pluck' one commit from a branch into your tree, but not the others. Sounds good - `git cherry-pick` to the rescue!
     73
     74This opens the '''interactive diff selector''', which behaves a lot like `darcs record`. It will go through every change you have made in the working tree, asking if you want to `git add` it to the index, so you can commit it afterwords.
     75
     76'''Nota bene''': this only adds files ''to the index'', it does not commit them. Afterwords, you may commit the result using `git commit`. '''Do not use `git commit -a`''', or you will just add all the changes to the commit!
     77
     78=== Selectively cherry-pick a commit from a branch ===
     79
     80You still miss Darcs. One thing that would be great is if you could just 'pluck' one commit from a branch into your tree, but not the others. Sounds good - `git cherry-pick` to the rescue!
    7681{{{
    7782$ git checkout master
    7883$ git cherry-pick <sha1 id>
    7984}}}
    80  this will checkout to master, and pull in ''only' the commit you refer to. '''It does not create a merge''', it's as if the commit had existed on this branch all along. This is wonderfully useful for selectively plucking changes from someone's Git tree, or branch.
    81 
    82  * '''Merge a branch in a super commit.''' Let's say you have a branch `foo` you would like to merge into master, but you have 10 small commits on `foo`, and you only want to make 1 Big Commit on master. Many times, we land features in a single 'big commit' to keep the history clean. This is easily doable with:
     85this will checkout to master, and pull in ''only'' the commit you refer to. '''It does not create a merge''', it's as if the commit had existed on this branch all along. This is wonderfully useful for selectively plucking changes from someone's Git tree, or branch.
     86
     87=== Merge a branch into a Super Big Commit ===
     88
     89Let's say you have a branch `foo` you would like to merge into master, but you have 10 small commits on `foo`, and you only want to make 1 Big Commit on master. Many times, we land features in a single 'big commit' to keep the history clean. This is easily doable with:
    8390{{{
    8491$ git checkout master
    8592$ git merge --squash foo
    8693}}}
    87  and then you can commit your new, unstaged changes into a big commit after fixing any conflicts. `--squash` basically tells git ''to merge the changes, but not merge the commits''. This is exactly what you want.
    88 
    89  * '''Rebase a branch.''' What if you have a branch that's slightly out of date called `foo`, and you want to bring it up to date with master?
     94and then you can commit your new, unstaged changes into a big commit after fixing any conflicts. `--squash` basically tells git ''to merge the changes, but not merge the commits''. This is exactly what you want.
     95
     96=== Basic rebases ===
     97
     98What if you have a branch that's slightly out of date called `foo`, and you want to bring it up to date with master?
    9099{{{
    91100$ git checkout master
     
    93102$ git rebase master foo
    94103}}}
    95   This will:
    96     * Checkout to master.
    97     * Update master to the latest upstream version.
    98     * Rebase `foo` onto `master`.
    99   Where ''rebasing'' includes:
    100     * Checkout to the branch `foo`.
    101     * Discard all the commits you have made on `foo`, temporarily
    102     * Bring `foo` up to date with `master` (by ''fast-forwarding'' the tree)
    103     * Replay all your previous commits from `foo` onto the New-And-Improved `foo` branch
    104 
    105  This, in effect, will bring `foo` up to date with master, while preserving your commits.
    106 
    107  Q: '''But there was a conflict'''! A: That's OK. If `git rebase` encounters a conflict while replaying your work, '''it will stop and tell you so'''. It will ask you to '''fix the conflict, and `git add` the conflicting files'''. Then you can continue using `git rebase --continue`.
    108 
    109  Q: '''I started to rebase, but I confused myself and don't know how to get out! Help'''! A: You can always run `git rebase --abort`, which will abort the current rebase operation, and return you to your working tree.
    110 
    111  * '''Using the reflog.''' Eventually when working in the repository, you'll invariably do something on accident that will delete work. If you have never committed the changes, then you're out of luck (commit often, commit early - even locally!) But have you ever done something like:
    112    * Accidentally lost a commit, by deleting a branch?
    113    * Accidentally lost a commit through rebasing?
    114    * Amended a commit (`git commit --amend`), only to find out you broke it, and you want to ''undo'' the amendment?
    115    * Accidentally overwrote a branch with dangerous operation, like `git push --force`?
    116 
    117  '''The reflog can save you from all of these, and more'''. In short, the reflog ''is a log that records every modification which Git tracks''. To understand that, first understand this: despite its appearance, the Git data model has a core tenant: ''it is immutable - data is never deleted, only new copies can be made'' (the only exception is when garbage collection deletes nodes which have no outstanding references - much like our own GC!) Not even a rebase - which can rewrite the history - can actually delete old data.
     104
     105This will:
     106
     107  * Checkout to master.
     108  * Update master to the latest upstream version.
     109  * Rebase `foo` onto `master`.
     110
     111Where ''rebasing'' includes:
     112
     113  * Checkout to the branch `foo`.
     114  * Discard all the commits you have made on `foo`, temporarily
     115  * Bring `foo` up to date with `master` (by ''fast-forwarding'' the tree)
     116  * Replay all your previous commits from `foo` onto the New-And-Improved `foo` branch
     117
     118This, in effect, will bring `foo` up to date with master, while preserving your commits.
     119
     120Q: '''But there was a conflict'''! A: That's OK. If `git rebase` encounters a conflict while replaying your work, '''it will stop and tell you so'''. It will ask you to '''fix the conflict, and `git add` the conflicting files'''. Then you can continue using `git rebase --continue`.
     121
     122Q: '''I started to rebase, but I confused myself and don't know how to get out! Help'''! A: You can always run `git rebase --abort`, which will abort the current rebase operation, and return you to your working tree.
     123
     124=== Using the reflog ===
     125
     126Eventually when working in the repository, you'll invariably do something on accident that will delete work. If you have never committed the changes, then you're out of luck (commit often, commit early - even locally!) But have you ever done something like:
     127 * Accidentally lost a commit, by deleting a branch?
     128 * Accidentally lost a commit through rebasing?
     129 * Amended a commit (`git commit --amend`), only to find out you broke it, and you want to ''undo'' the amendment?
     130 * Accidentally overwrote a branch with dangerous operation, like `git push --force`?
     131
     132'''The reflog can save you from all of these, and more'''. In short, the reflog ''is a log that records every modification which Git tracks''. To understand that, first understand this: despite its appearance, the Git data model has a core tenant: ''it is immutable - data is never deleted, only new copies can be made'' (the only exception is when garbage collection deletes nodes which have no outstanding references - much like our own GC!) Not even a rebase - which can rewrite the history - can actually delete old data.
    118133 
    119  Second, we need to understand an '''important part of `git checkout`''': the purpose of `checkout` is ''not'' to switch branches. Checkout, roughly speaking, '''allows you to check out your tree to any state, revision, or copy in the history'''. You don't have to checkout to a branch: you can checkout to a commit from 3 weeks ago, a commit that ''does not exist on a branch'', or a completely empty branch with nothing in common. You can checkout the entire tree, or you could checkout an individual file, or a single directory. The point being: '''checkout takes you to a state in the history.'''
     134Second, we need to understand an '''important part of `git checkout`''': the purpose of `checkout` is ''not'' to switch branches. Checkout, roughly speaking, '''allows you to check out your tree to any state, revision, or copy in the history'''. You don't have to checkout to a branch: you can checkout to a commit from 3 weeks ago, a commit that ''does not exist on a branch'', or a completely empty branch with nothing in common. You can checkout the entire tree, or you could checkout an individual file, or a single directory. The point being: '''checkout takes you to a state in the history.'''
    120135 
    121  So with that in mind, think of `reflog` like the audit log you can use to see what operations were performed on the immutable git history. ''Every'' operation is tracked. Let's look at an example, from Austin's ''validation tree'' he uses to push commits:
     136So with that in mind, think of `reflog` like the audit log you can use to see what operations were performed on the immutable git history. ''Every'' operation is tracked. Let's look at an example, from Austin's ''validation tree'' he uses to push commits:
    122137{{{
    123138$ git reflog --date=relative # this will open an interactive pager
     
    1371521ef941a HEAD@{28 hours ago}: checkout: moving from master to amp
    138153}}}
    139  '''The most recent operations are first, and older operations appear chronologically'''. Let's note a few things:
    140    * The ''work you previously had still exists, and has a commit ID''. It is on the far left.
    141    * The reflog tells you what operation resulted in the commit: in my history, we can see I did:
    142        * At one point, I reset my tree and undid my latest commit (in `70406bc`, using `git reset`.) Then I kept working.
    143        * Several `git cherry-pick` operations.
    144        * Several commits, and some `git commit --amend` operations.
    145        * I checked out to master.
    146        * Then I did a merge of the `amp` branch, which was a fast-forward: my previous changes had rebased the `amp` branch.
    147        * Later on, I pulled my tree and I got some updates from upstream.
    148    * The reflog tells you what was modified; in this case it shows you the commits I changed.
    149 
    150  With this information, '''I can now restore my tree to any of those partial states'''. For example, let's say I `git commit --amend` the AMP patch in `75a9664`, and did some more stuff. But then it turns out I didn't want any of that, '''and I didn't want the amendment either'''. I can easily do:
     154
     155'''The most recent operations are first, and older operations appear chronologically'''. Let's note a few things:
     156  * The ''work you previously had still exists, and has a commit ID''. It is on the far left.
     157  * The reflog tells you what operation resulted in the commit: in my history, we can see I did:
     158      * At one point, I reset my tree and undid my latest commit (in `70406bc`, using `git reset`.) Then I kept working.
     159      * Several `git cherry-pick` operations.
     160      * Several commits, and some `git commit --amend` operations.
     161      * I checked out to master.
     162      * Then I did a merge of the `amp` branch, which was a fast-forward: my previous changes had rebased the `amp` branch.
     163      * Later on, I pulled my tree and I got some updates from upstream.
     164  * The reflog tells you what was modified; in this case it shows you the commits I changed.
     165
     166With this information, '''I can now restore my tree to any of those partial states'''. For example, let's say I `git commit --amend` the AMP patch in `75a9664`, and did some more stuff. But then it turns out I didn't want any of that, '''and I didn't want the amendment either'''. I can easily do:
    151167{{{
    152168$ git checkout -b temp daa9a30
    153169}}}
    154170
    155  Now, I am on the `temp` branch, '''and my HEAD commit points to the patch, without any amendments'''. I've essentially checked out to a point in the tree without any of those changes - because `git` never modifies the original data, this old copy still exists. Now that I am on the `temp` branch, I can do any number of things. Perhaps I can just delete the old `amp` branch, and merge the `temp` branch instead now.
    156 
    157  As you can see, the `reflog` saved me here: I undid some nasty work in my personal tree, which otherwise might have been much more error prone or difficult to perform.
    158 
    159  '''The reflog is not needed often, but it is often indispensable when you need it.'''
     171Now, I am on the `temp` branch, '''and my HEAD commit points to the patch, without any amendments'''. I've essentially checked out to a point in the tree without any of those changes - because `git` never modifies the original data, this old copy still exists. Now that I am on the `temp` branch, I can do any number of things. Perhaps I can just delete the old `amp` branch, and merge the `temp` branch instead now.
     172
     173As you can see, the `reflog` saved me here: I undid some nasty work in my personal tree, which otherwise might have been much more error prone or difficult to perform.
     174
     175'''The reflog is not needed often, but it is often indispensable when you need it.'''
    160176
    161177== Advanced Git tricks ==
     
    163179Finally, there are some '''advanced tips''', not for the faint of heart:
    164180
    165  * '''Rebasing interactively.''' At a certain point of git usage, you'll want to rewrite history by ''rebasing interactively''. This can be done by running:
     181=== Interactive rebases ===
     182
     183At a certain point of git usage, you'll want to rewrite history by ''rebasing interactively''. This can be done by running:
    166184{{{
    167185$ git rebase -i <commit range>
     
    171189$ git rebase -i HEAD~10
    172190}}}
    173   will allow you to interactively rebase the last 10 commits on your branch. This power allows you to:
    174     * '''Reorder patches''', by reordering the entries in the rebase list. If two patches don't touch each other, you can always switch their order and everything will be OK.
    175     * '''Drop patches''', and completely remove them from the history, by removing them from the list.
    176     * '''Squash commits''', which will let you compress a series of commits into one.
    177     * '''Reword commits''', which will let you rewrite the commit message for any commit in the list, without touching anything else. (This is one of the most common ones I - Austin Seipp - use.)
     191
     192will allow you to interactively rebase the last 10 commits on your branch. This power allows you to:
     193  * '''Reorder patches''', by reordering the entries in the rebase list. If two patches don't touch each other, you can always switch their order and everything will be OK.
     194  * '''Drop patches''', and completely remove them from the history, by removing them from the list.
     195  * '''Squash commits''', which will let you compress a series of commits into one.
     196  * '''Reword commits''', which will let you rewrite the commit message for any commit in the list, without touching anything else. (This is one of the most common ones I - Austin Seipp - use.)
    178197
    179198== Workflow with validate ==