Git question

Status
Not open for further replies.

dlp

Ars Tribunus Militum
2,220
I'm new to version control so I picked git for a project I'm working on. Let's say I have the following commits:
  • Commit 1: All existing code.
  • Commit 2: Modified files a, b.
  • Commit 3: Modified files c, d.
  • Commit 4: Fixed bug in a from changes in commit 2.
  • Commit 5: Modified files e, f.

Is there an easy way to pull out commit 1 and the changes in 2 & 4 only?
 

AgentHH

Ars Tribunus Militum
2,966
Subscriptor
My understanding is that you want commits 3 and 5 gone?

If that's the case, here's one way (of many) to do it. It's conceptually simple - you make a new branch, starting at commit 1. Then, cherry-pick the changes from commit 2 and commit 4. A cherry-pick is basically a fancy and easy way of taking the diff from the original location and applying it to your current tree.
  • git checkout -b newbranch [commit 1 hash]
  • git cherry-pick [commit 2 hash] [commit 4 hash]
At this point, you'll end up with your original branch and your new branch (which you can view with "git branch -lv"). You can view the sequence of commits on your new branch with "git log". Note that the commit hashes for 2 and 4 will have changed - this is normal!

From here, you're probably going to want to switch back to developing on the original branch, but I will leave that question to the Internet.

Edit: once you've become more (a lot more) familiar with git, I suggest looking into "git rebase -i". Word of warning: you can break everything. Be careful. It's really powerful.
 

dlp

Ars Tribunus Militum
2,220
Cherry pick looks right at first glance. I'll look more into it and come back if I get confused.

What I set out to do with version control is each commit was for changes to a specific feature request for an existing app that I didn't start. Ideally, I'd implement a single feature, test, commit and be done with that feature. So to expand on the example in my first post, commit 2 and 4 both represent the changes to implement feature A. 3 implements feature B, 5 is feature C. Features A, B, and C are unrelated. This is just my version control repo, no one else is working on the app so no one else commits. I do need the code as it is now with all of the committed changes, but I realized I had no way of getting just the app with feature A implemented if I wanted to. Reasons for this might be demoing, withholding other features based on change requests but deplying completed features, business level issues like partial payment, etc. Do I have the right idea?
 

Gandalf007

Ars Tribunus Angusticlavius
6,975
Subscriptor
Another extremely useful git feature is 'git add -p'. Regardless of whether you're in a feature branch or master, sometimes you make an unrelated change in the same file that you'd really rather be in a separate commit (or you forget to commit before you start working on something else).

With 'add -p' you don't have to revert or have everything lumped together in the same commit -- it will interactively prompt you for which hunks you want to commit, à la vimdiff/sdiff -o/mergemaster/etc. (and you can even edit manually if needed!). When you're done, just the parts you want are staged to the index, and git commit (without -a) will commit them.

This is the first git feature that really won me over, as there is no equivalent in subversion. I didn't do much branching/merging in svn, so I didn't really appreciate the better branching in git, and I found enough little deficiencies/differences compared to svn that I wondered if git was worth the switch. With this, it is!
 
Status
Not open for further replies.