Changes between Version 34 and Version 35 of WorkingConventions/Git


Ignore:
Timestamp:
Sep 13, 2013 3:44:51 AM (18 months ago)
Author:
thoughtpolice
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WorkingConventions/Git

    v34 v35  
    55 
    66Existing darcs users see: [wiki:GitForDarcsUsers Git For Darcs Users]. If you have an existing source tree in darcs and need to convert patches, see [wiki:DarcsToGit Darcs To Git]. Simon PJ's git notes are [wiki:WorkingConventions/GitSPJ GIT SPJ]. 
     7 
     8= Setup = 
    79 
    810== General Guidelines == 
     
    3840Please try to follow the general convention for the [http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html Git commit message structure] as many Git tools rely on this. Moreover, take into account that the commit message text is interpreted as WikiFormatting in Trac. 
    3941 
    40 For GHC, we have a simple convention for commit log messages: 
    41  
    42  * If your patch fixes breakage in the build, then begin the patch name with `"FIX BUILD"`. e.g. 
    43 {{{ 
    44   FIX BUILD Use the right find on Windows systems; fixes bindist creation 
    45 }}} 
    46  * If your patch addresses or fixes a bug/ticket, then include the ticket number in the form "`#NNNN`" in the commit message, e.g. 
     42In particular, if your patch addresses or fixes a bug/ticket, then include the ticket number in the form "`#NNNN`" in the commit message, e.g. 
    4743{{{ 
    4844  withMVar family have a bug (fixes #767) 
    4945}}} 
    50  '''''Git will then add a link to the commit from the ticket''''' (as soon as the commit becomes reachable from the `master` HEAD), so that people watching the ticket can see that a fix has been committed, and in the future we can easily find the patch that addressed the ticket.  When navigating the Git history on Trac, you will also be able to jump directly to the ticket from the commit. 
     46'''''Git will then add a link to the commit from the ticket''''' (as soon as the commit becomes reachable from the `master` HEAD), so that people watching the ticket can see that a fix has been committed, and in the future we can easily find the patch that addressed the ticket.  When navigating the Git history on Trac, you will also be able to jump directly to the ticket from the commit. 
    5147 
    5248== Line endings == 
     
    6258}}} 
    6359Do this before you commit them! 
     60 
     61= Working with the tree = 
     62 
     63== Git tricks == 
     64 
     65When working with GHC, there are a lot of ways you can use Git to make your life easier. Below are some of them: 
     66 
     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: 
     68{{{ 
     69$ git add -p 
     70}}} 
     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! 
     76{{{ 
     77$ git checkout master 
     78$ git cherry-pick <sha1 id> 
     79}}} 
     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: 
     83{{{ 
     84$ git checkout master 
     85$ git merge --squash foo 
     86}}} 
     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? 
     90{{{ 
     91$ git checkout master 
     92$ git pull origin master 
     93$ git rebase master foo 
     94}}} 
     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. 
     118  
     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.''' 
     120  
     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: 
     122{{{ 
     123$ git reflog --date=relative # this will open an interactive pager 
     124ad15c2b HEAD@{5 hours ago}: pull -tu origin master: Fast-forward 
     12575a9664 HEAD@{27 hours ago}: merge amp: Fast-forward 
     1261ef941a HEAD@{27 hours ago}: checkout: moving from amp to master 
     12775a9664 HEAD@{27 hours ago}: commit (amend): Implement the AMP warning (#8004) 
     128daa9a30 HEAD@{28 hours ago}: rebase -i (finish): returning to refs/heads/amp 
     129daa9a30 HEAD@{28 hours ago}: rebase -i (pick): Implement the AMP warning (#8004) 
     130b20cf4e HEAD@{28 hours ago}: rebase -i (pick): Fix AMP warnings. 
     1311ef941a HEAD@{28 hours ago}: checkout: moving from amp to 1ef941a82eafb8f22c19e2643685679d2454c24a 
     1323e8c33e HEAD@{28 hours ago}: commit: Fix AMP warnings. 
     13370406bc HEAD@{28 hours ago}: reset: moving to HEAD~ 
     134d2afc83 HEAD@{28 hours ago}: cherry-pick: Fix most AMP warnings. 
     13570406bc HEAD@{28 hours ago}: commit (amend): Implement the AMP warning (#8004) 
     136697f9da HEAD@{28 hours ago}: cherry-pick: Implement the AMP warning (#8004) 
     1371ef941a HEAD@{28 hours ago}: checkout: moving from master to amp 
     138}}} 
     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. 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: 
     151{{{ 
     152$ git checkout -b temp daa9a30 
     153}}} 
     154 
     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.''' 
     160 
     161== Advanced Git tricks == 
     162 
     163Finally, there are some '''advanced tips''', not for the faint of heart: 
     164 
     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: 
     166{{{ 
     167$ git rebase -i <commit range> 
     168}}} 
     169 For example: 
     170{{{ 
     171$ git rebase -i HEAD~10 
     172}}} 
     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.) 
    64178 
    65179== Workflow with validate ==