Skip to content

Making a change in multiple commits

Michael Kavulich edited this page Jan 12, 2017 · 2 revisions

Often, we make complicated changes to the code. One major benefit of Git over Subversion is that commits can be made locally: we do not need to bother with committing our changes to the "main" repository as we did with Subversion.

Phase 1: Create a branch, make some commits

The first stage in making changes should always be to create a new branch. I already have a clone of the repository on my local machine, so I don't have to make a new one: I can just create a new branch from my existing repository clone! (making sure I'm on the master first). In this case, the existing clone is a clone of my own Github fork of the repository (https://github.com/mkavulich/WRF instead of https://github.com/wrf-model/WRF), but this will only become important at the pull request stage.

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git branch typo_fixes
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git checkout typo_fixes
Switched to branch 'typo_fixes'

Note that I didn't even have to be in the top-level directory of my repository (the repository clone I'm using is on my machine at /Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF): you can still create and switch between branches just the same.

I made some changes to fix some long-standing typos in the WRFDA code. I added those files, and then committed them.

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git status
On branch typo_fixes
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   da_solve.inc
	modified:   da_update_firstguess.inc
	modified:   da_wrfvar_main.f90

no changes added to commit (use "git add" and/or "git commit -a")

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git add da_solve.inc  da_update_firstguess.inc da_wrfvar_main.f90
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git commit

This time I didn't specify a commit message, so git opened a text editor for me (in my case, vim).

Entering a commit message in vim

I entered a commit message there, saved the file, and exited, finalizing the commit:

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da/da_main>git commit
[typo_fixes 1d22441] Fixing some typos in WRFDA code
 3 files changed, 3 insertions(+), 3 deletions(-)

Phase 2: Make some more commits on this branch

After making this commit, I realized there were a few more typos in the code that needed fixing. Since this was related to the previous commit, there was no need to create a new branch just for these commits, so I went ahead and made the changes and committed them:

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>vi da_setup_structures/da_setup_be_ncep_gfs.inc
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>vi da_transfer_model/da_transfer_wrftltoxa_adj.inc
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>git status
On branch typo_fixes
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   da_setup_structures/da_setup_be_ncep_gfs.inc
	modified:   da_transfer_model/da_transfer_wrftltoxa_adj.inc

no changes added to commit (use "git add" and/or "git commit -a")
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>git add da_setup_structures/da_setup_be_ncep_gfs.inc da_transfer_model/da_transfer_wrftltoxa_adj.inc
/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>git commit -m 'Fixing more typos in WRFDA code'
[typo_fixes 04d0151] Fixing more typos in WRFDA code
 2 files changed, 7 insertions(+), 7 deletions(-)

Phase 3: Push changes to main repository (origin), open pull request

Satisfied that I have permanently solved all typos in the WRFDA code, I pushed my branch back to the origin (main repository):

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF/var/da>git push -u origin typo_fixes
Counting objects: 16, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (16/16), 1.32 KiB | 0 bytes/s, done.
Total 16 (delta 14), reused 0 (delta 0)
remote: Resolving deltas: 100% (14/14), completed with 11 local objects.
To https://github.com/mkavulich/WRF
 * [new branch]      typo_fixes -> typo_fixes
Branch typo_fixes set up to track remote branch typo_fixes from origin.

And since my changes were ready for wider scrutiny by the developers, I opened a pull request from this new branch. Remember, I did all this on my Github fork of the repository (https://github.com/mkavulich/WRF) rather than in the main repository (https://github.com/wrf-model/WRF). However, the pull request procedure is the same regardless:

First, go to the Github website to see the repository of interest (in this case, it was my fork https://github.com/mkavulich/WRF of the main repository)

Click New pull request Nothing to commit??

At first, it shows that there is nothing to commit! This is because we are opening a pull request from a fork, and we haven't yet specified which branch on our fork we want to pull from. I selected the "typo fixes" branch, and voila, saw the changes I expected!

The changes we expect to see are now there

Now all we have to do is enter the standard commit message, and we're ready to open the pull request!

Enter a pull request message

All done!

Phase 4: Amending a pull request with new commits

Since a pull request is tied to a specific branch, if you need to update the pull request after it is opened, this is very easy: just make more commits on the same branch!

In this case, I got some helpful comments after opening the pull request that there were actually some more typos that I missed! I made these changes on my local machine in the same copy of the code I had been working on (making sure I was on the same branch named "typo fixes"), committed them, and pushed them back to the origin.

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF>git status
On branch typo_fixes
Your branch is up-to-date with 'origin/typo_fixes'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   var/da/da_buoy/da_get_innov_vector_buoy.inc
	modified:   var/da/da_buoy/da_transform_xtoy_buoy.inc
	modified:   var/da/da_metar/da_transform_xtoy_metar.inc
	modified:   var/da/da_minimisation/da_write_diagnostics.inc
	modified:   var/da/da_physics/da_transform_xtopsfc.inc
	modified:   var/da/da_ships/da_get_innov_vector_ships.inc
	modified:   var/da/da_ships/da_transform_xtoy_ships.inc
	modified:   var/da/da_synop/da_get_innov_vector_synop.inc
	modified:   var/da/da_tools/da_intpsfc_tem.inc
	modified:   var/da/da_transfer_model/da_transfer_wrftoxb.inc
	modified:   var/da/da_transfer_model/da_transfer_wrftoxb_lite.inc

no changes added to commit (use "git add" and/or "git commit -a")

Notice for a moment that last printout from git (use "git add" and/or "git commit -a"). Since I have a large number of files changed, and I am 100% confident that no unwanted changes are contained here, I will just use git commit -a to commit all changes, rather than adding each file individually. This is a powerful but dangerous command, as you can accidentally commit things you didn't want to commit! Use it wisely!

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF>git commit -a
[typo_fixes 6aff1b7] Based on pull request comments, fixing persistent typo in several files (assmiilation -> assimilation). Found a few more typos along the way as well.
 11 files changed, 16 insertions(+), 16 deletions(-)

Now I push the changes back to the origin, same as before:

/Users/kavulich/WRFDA_REGTEST/WORKDIR/WRF>git push -u origin
Counting objects: 23, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (23/23), 1.75 KiB | 0 bytes/s, done.
Total 23 (delta 22), reused 0 (delta 0)
remote: Resolving deltas: 100% (22/22), completed with 22 local objects.
To https://github.com/mkavulich/WRF
   04d0151..6aff1b7  typo_fixes -> typo_fixes
Branch typo_fixes set up to track remote branch typo_fixes from origin.

When I go to the pull request on the Github website, I will now see that the pull request has been updated!

The pull request has been updated