about 3 years ago

Today I was be asked by my colleague "How to maintain (Git) commits which is local only such configurations?".

Commits like these should always be with the development branch, however, we don't want them to be pushed back to repo.

The simplest way is to put those file paths in .gitignore file - a text file indicates Git to ignore files from revision tracking. However, sometimes we can't just ignore them from revisioning, ex: config.php. We need them to be tracked by Git, and also part of the changes stay in local at the same time.

For this circumstance, I'll maintain two branches locally, one is master and another is config. Both of them are setting upstream to origin/master, but the config branch will never push commits back to repo, instead, it pulls only. And the config branch is always leading some commits which you don't want them to be pushed back.

For further explaination, let's create the config branch and set the its upstream to origin/master. ↓↓↓

$ git branch --set-upstream origin/master config
$ git checkout config

And the history tree will looks like this after checked-in several local commits.

m0---m1---m2---m3---m4   <---[master]
                      \
                       \
                        c0---c1---c2   <<===[[config]]

The local commts may contain the necessary configurations which make your development enviroment works, so we coding/developing on the branch config. This is the tree graph it looks like after we've done some works. (remember we still on branch config which contains both local commits and works)

m0---m1---m2---m3---m4   <---[master]
                      \
                       \
                        c0---c1---c2---w0---w1   <<===[[config]]
                                    ^^^^^^^^^^

Now, assume we've done some works (w0, w1), and want then to be pushed back to repo. Here is the instructions: checkout the master branch, pick up the works we've done from config branch (NOTE: be avoid the local commits), then push them back.

$ git checkout master
$ git cherry-pick config~2...config
$ git push origin master
m0---m1---m2---m3---m4---`w0---`w1   <<===[[master]]
                     \^^^^^^^^^^^^
                      \
                       c0---c1---c2---w0---w1   <---[config]

Then check-out config branch and pull again.

$ git checkout config
$ git pull

Finally we got works to be pushed back and local commits still stay in local.

m0---m1---m2---m3---m4---`w0---`w1   <---[master]
                                 \
                                  \
                                   c0---c1---c2   <<===[[config]]
← 突破 YouTube「喜歡的影片」數量 100 個的限制
 
comments powered by Disqus