This way of working helps to keep work well organized, with readable history.
This in turn makes it easier for project maintainers (that might be you) to see
what you’ve done, and why you did it.
It may sound strange, but deleting your own main branch can help reduce
confusion about which branch you are on. See deleting master on github for
details (replacing master with main).
From time to time you should fetch the upstream (trunk) changes from github:
gitfetchupstream
This will pull down any commits you don’t have, and set the remote branches to
point to the right commit. For example, ‘trunk’ is the branch referred to by
(remote/branchname) upstream/main - and if there have been commits since
you last checked, upstream/main will change after you do the fetch.
Make a new feature branch
When you are ready to make some changes to the code, you should start a new
branch. Branches that are for a collection of related edits are often called
‘feature branches’.
Making an new branch for each set of related changes will make it easier for
someone reviewing your branch to see what you are doing.
Choose an informative name for the branch to remind yourself and the rest of us
what the changes in the branch are for. For example add-ability-to-fly, or
buxfix-for-issue-42.
# Update the mirror of trunkgitfetchupstream# Make new feature branch starting at current trunkgitbranchmy-new-featureupstream/maingitcheckoutmy-new-feature
Generally, you will want to keep your feature branches on your public github
fork of scikit-image. To do this, you git push this new branch up to your
github repo. Generally (if you followed the instructions in these pages, and by
default), git will have a link to your github repo, called origin. You push
up to your own repo on github with:
gitpushoriginmy-new-feature
In git >= 1.7 you can ensure that the link is correctly set by using the
--set-upstream option:
gitpush--set-upstreamoriginmy-new-feature
From now on git will know that my-new-feature is related to the
my-new-feature branch in the github repo.
The editing workflow
Overview
# hack hackgitaddmy_new_filegitcommit-am'NF - some message'gitpush
In more detail
Make some changes
See which files have changed with gitstatus (see git status).
You’ll see a listing like this one:
# On branch ny-new-feature# Changed but not updated:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: README## Untracked files:# (use "git add <file>..." to include in what will be committed)## INSTALLnochangesaddedtocommit(use"git add"and/or"git commit -a")
Check what the actual changes are with gitdiff (git diff).
Add any new files to version control gitaddnew_file_name (see
git add).
To commit all modified files into the local copy of your repo,, do
gitcommit-am'Acommitmessage'. Note the -am options to
commit. The m flag just signals that you’re going to type a
message on the command line. The a flag — you can just take on
faith — or see why the -a flag? — and the helpful use-case
description in the tangled working copy problem. The git commit manual
page might also be useful.
To push the changes up to your forked repo on github, do a gitpush (see git push).
Ask for your changes to be reviewed or merged
When you are ready to ask for someone to review your code and consider a merge:
Go to the URL of your forked repo, say
https://github.com/your-user-name/scikit-image.
Use the ‘Switch Branches’ dropdown menu near the top left of the page to
select the branch with your changes:
Click on the ‘Pull request’ button:
Enter a title for the set of changes, and some explanation of what you’ve
done. Say if there is anything you’d like particular attention for - like a
complicated change or some code you are not happy with.
If you don’t think your request is ready to be merged, just say so in your
pull request message. This is still a good way of getting some preliminary
code review.
Some other things you might want to do
Delete a branch on github
gitcheckoutmain# delete branch locallygitbranch-Dmy-unwanted-branch# delete branch on githubgitpushorigin:my-unwanted-branch
If you want to work on some stuff with other people, where you are all
committing into the same repository, or even the same branch, then just
share it via github.
Finally the Fancy log outputlg alias will give you a reasonable text-based
graph of the repository.
Rebasing on trunk
Let’s say you thought of some work you’d like to do. You
Update the mirror of trunk and Make a new feature branch called
cool-feature. At this stage trunk is at some commit, let’s call it E. Now
you make some new commits on your cool-feature branch, let’s call them A, B,
C. Maybe your changes take a while, or you come back to them after a while. In
the meantime, trunk has progressed from commit E to commit (say) G:
A---B---Ccool-feature/D---E---F---Gtrunk
Recovering from mess-ups
Sometimes, you mess up merges or rebases. Luckily, in git it is
relatively straightforward to recover from such mistakes.
If you mess up during a rebase:
gitrebase--abort
If you notice you messed up after the rebase:
# reset branch back to the saved pointgitreset--hardtmp
If you forgot to make a backup branch:
# look at the reflog of the branchgit
Rewriting commit history
There’s an embarrassing typo in a commit you made? Or perhaps the you
made several false starts you would like the posterity not to see.
Remember that links starting with git@ use the ssh protocol.
Your collaborators can then commit directly into that repo with the
usual:
gitcommit-am'ENH - much better code'gitpushoriginmain# pushes directly into your repo
At this stage you consider merging trunk into your feature branch, and you
remember that this here page sternly advises you not to do that, because the
history will get messy. Most of the time you can just ask for a review, and not
worry that trunk has got a little ahead. But sometimes, the changes in trunk
might affect your changes, and you need to harmonize them. In this situation
you may prefer to do a rebase.
rebase takes your changes (A, B, C) and replays them as if they had been made to
the current state of trunk. In other words, in this case, it takes the
changes represented by A, B, C and replays them on top of G. After the rebase,
your history will look like this:
# Update the mirror of trunkgitfetchupstream# go to the feature branchgitcheckoutcool-feature# make a backup in case you mess upgitbranchtmpcool-feature# rebase cool-feature onto trunkgitrebase--ontoupstream/mainupstream/maincool-feature
In this situation, where you are already on branch cool-feature, the last
command can be written more succinctly as:
gitrebaseupstream/main
When all looks good you can delete your backup branch:
If you have made changes to files that have also changed in trunk, this may
generate merge conflicts that you need to resolve - see the git rebase man
page for some instructions at the end of the “Description” section. There is
some related help on merging in the git user manual - see resolving a merge.
reflog
show
cool
-
feature
8630830
cool
-
feature
@
{
0
}:
commit
:
BUG
:
io
:
close
file
handles
immediately
278
dd2a
cool
-
feature
@
{
1
}:
rebase
finished
:
refs
/
heads
/
my
-
feature
-
branch
onto
11
ee694744f2552d
26
aa21a
cool
-
feature
@
{
2
}:
commit
:
BUG
:
lib
:
make
seek_gzip_factory
not
leak
gzip
obj
...
# reset the branch to where it was before the botched rebase
git
reset
--
hard
cool
-
feature
@
{
2
}
Fix
a
few
bugs
+
disable
13
d7934
First
implementation
6
ad92e5
*
masked
is
now
an
instance
of
a
new
object
,
MaskedConstant
29001
ed
Add
pre
-
nep
for
a
copule
of
structured_array_extensions
.
...
and 6ad92e5 is the last commit in the cool-feature branch. Suppose we
want to make the following changes:
Rewrite the commit message for 13d7934 to something more sensible.
Combine the commits 2dec1ac, a815645, eadc391 into a single one.
We do as follows:
# make a backup of the current stategitbranchtmpHEAD# interactive rebasegitrebase-i6ad92e5
This will open an editor with the following text in it:
pick13d7934Firstimplementationpick2dec1acFixafewbugs+disablepicka815645Modifyitsothatitworkspickeadc391Fixsomeremainingbugs# Rebase 6ad92e5..eadc391 onto 6ad92e5## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message## If you remove a line here THAT COMMIT WILL BE LOST.# However, if you remove everything, the rebase will be aborted.#
To achieve what we want, we will make the following changes to it:
This means that (i) we want to edit the commit message for
13d7934, and (ii) collapse the last three commits into one. Now we
save and quit the editor.
Git will then immediately bring up an editor for editing the commit
message. After revising it, we get the output: