Some git workflows

In this post I will describe some of the git workflows I use. Stay tuned for more of these. Please comment and tell me if I do something really stupid and also please add suggestions on how I can optimize these tasks.

Fork a branch, do some development and merge it back to master.

Assume you have written some code that should be developed further in another branch, or assume you are going are to begin writing such code.

Fork the current branch
git checkout -b mynewfeature

Do some hacking – commit, commit commit. You should know how this works.

Now you want to merge it back to master.
git checkout master
git pull --rebase . mynewfeature

‘git merge mynewfeature’ should be enough but you may have done some work in your local master and forgot to push it. In such case we get a merge which is a quite ugly compared to a rebase since we then get a nonlinear history.

Now your local master has your new code and it’s safe to delete mynewfeature
git branch -d mynewfeature

Rebase from the server and push the code to the server.
git pull --rebase # You may have to add ‘origin master’ here if your tracking config is missing
git push

Split a large commit into smaller commits before pushing

Assume that you have made a large commit that you
must split before you push it to the server.

Undo the commit
git reset --soft HEAD^1

Now all of your commit are in the index. Move them back to the unstaged area.
git reset


Now issue a git status and check what your commit contains
git status

Start adding parts using the -p flag. Answer ‘y’ or ‘n’ depending on whether the chunk should be added or not
git add -p # You may add a folder or file path here

Look at what’s in the index
git diff --cached # I have an alias 'git dc' for this

Once happy with your index, make a commit of it
git commit -m “A tiny feature”

Go back to (1) until there is nothing more to add. Then merge the branch back to master according to the previous workflow description.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s