Skip navigation

Git was the first version control system I’ve used. I started off just using three basic commands:

git add .
git commit -a
git push origin master

And that was it! It was simple, I was happy. But I knew there was more to it. And, there certainly is. I maybe know just a small subset, but I’m learning every day and I love it!

Branches, stashes, commits, everything. It’s plain cool.

Git saved me more than a few times recently.

Example: I’m doing some experimenting. I’m making a change, then another one, then another one… And I get carried away. I’ve got a bunch of changes and I want to test parts of them only. If I could only split this one big diff… But I can! There are git add –interactive or git stash save –patch –no-keep-tree. Simple as that. So what I did was I fired off git stash –save –no-keep-tree. It asked me for every hunk (part of the diff). I picked out the ones I wanted and stashed them away. And I didn’t even commit (I could’ve used git add –interactive for that)! Then I stashed away everything else. And my diff is split in two parts.
There’s so much beauty in git, that I would say this is one of my favourite pieces of software. That’s why I couldn’t stand using p4 at work and got the git-p4 wrapper (which sucks but mostly works). And I was born again. p4 sucks balls. The only reason I see to use Perforce is performance with big files and big repositories.

But p4 is a huge pain to use every day. There’s no way to stash your changes, for crying out loud!

What happens if you need to make change A, test it, then *without throwing away A*, make change B, test it *in isolation*; then test A and B together? Good luck with that.

Git (there’s more than one way to do it):
First way:
$ git checkout -b changeA master
$ # make change A and test it
$ git commit -a # git add . && git commit # One or more commits
$ git checkout -b changeB master
$ # make change B and test it
$ git commit -a # git add . && git commit # One or more commits
$ git checkout -b changesAandB master
$ git merge changeA
$ git merge changeB
$ # Now choose what to merge to master!
$ git checkout master && git merge changesAandB # Both

Second way (faster, less flexible):
$ git checkout -b changesAandB
$ # make change A and test it
$ git stash # One time
$ # make change B and test it
$ git commit -a # changeB feels like a “master” change, it’s commited, but changeA is stashed
$ git stash pop # Get changeA
$ # test together
$ # If you choose changeA
$ git revert <commit-sha of changeB>
$ # If you choose changeB
$ git stash # Keeping changeA stashed, though. You might want to use git checkout . to throw it away
$ # If you choose both
$ git commit -a # Commiting changeA

P4: ???

What happens if while working on Feature 1, you get interrupted and need to start working on Feature 2, because it has a higher priority?

Git:
$ git checkout -b Feature1 master
$ # work, commit; get interrupted
$ # Either use git stash or git commit; you can later create a new commit with git revert that reverts this temporary one or you can use git reset –hard to delete the commit.
$ # If you chose git commit, git checkout -b Feature2 master
$ # Work on Feature2.. When finished, merge to master branch. If you want to work on Feature1, just commit to the Feature2 branch, and checkout Feature1, then stash pop the change from the second step if you want to and if you did a stash at all.

Another way in git, if you’re working on master branch (which you shouldn’t be doing, but let’s say you did.)
$ git commit / git stash # Temporary commit or a stash; btw, you can squash commits into one, so even if you do a bunch of them you can still push only one!
$ git log # see which is the last commit that’s not from work on Feature1
$ git checkout <sha-1 of that commit> -b Feature2
$ # work on feature 2

Most people would have multiple clients so that they can work on those… Which is downright stupid. Especially if the tree is 10GB for example (and after all, this will probably be the case because p4 is used for such big trees, because of its performance).

How about differences between changes ?

Git: git diff <commit1> <commit2>

P4: ???

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.