Skip to content

Commit 7dfca29

Browse files
committed
adding the instructions for getting back to a clean master branch for students.
1 parent 75a8462 commit 7dfca29

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

source/additional/git_cleanup.rst

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
Steps to get a clean master branch for your git fork
2+
====================================================
3+
4+
Command line instructions for getting back to a pristine master branch
5+
6+
7+
1. Make sure that you have the UWPCE repostitory set up as an additional
8+
remote for your local repository::
9+
10+
$ git remote add uwpce [email protected]:UWPCE-PythonCert/training.python_web.git
11+
12+
This will give you direct access to the original copy of the repository from
13+
the command line.
14+
15+
2. Verify this worked by checking your remotes::
16+
17+
$ git remote
18+
origin
19+
uwpce
20+
21+
Now you have *two* remote repositories connected to your local repository.
22+
23+
* *Origin* represents the copy of your fork of the UW PCE repository *on
24+
github's servers*.
25+
* *Uwpce* is the original UW PCE repository *on github's servers*.
26+
27+
State the Problem
28+
-----------------
29+
30+
You have a series of changes *you* have made to the *master* branch of your
31+
repository, both the local and the *origin* remote.
32+
33+
Every time you make new changes for a homework and then submit a pull request,
34+
all these old changes are included in the pull request.
35+
36+
State the Goal
37+
--------------
38+
39+
You would like to get a *master* branch of your repository that exactly matches
40+
the *master* branch of the UW PCE remote (*uwpce*).
41+
42+
Once you have this, you can then keep that branch up to date with the UW PCE
43+
copy
44+
45+
And you can continue to make clean branches for each homework *starting from
46+
that clean master*.
47+
48+
Steps to get there
49+
------------------
50+
51+
Preserve your Old Work
52+
++++++++++++++++++++++
53+
54+
First, make a branch on your local machine of your current *master*, this will
55+
be a branch you keep that contains all your homework up until today::
56+
57+
$ git branch -a
58+
* master
59+
remotes/origin/HEAD -> origin/master
60+
remotes/origin/gh-pages
61+
remotes/origin/instructor
62+
remotes/origin/master
63+
remotes/origin/week-long-format
64+
remotes/uwpce/master
65+
$ git branch keep-old-work
66+
$ git branch -a
67+
keep-old-work
68+
* master
69+
remotes/origin/HEAD -> origin/master
70+
remotes/origin/gh-pages
71+
remotes/origin/instructor
72+
remotes/origin/master
73+
remotes/origin/week-long-format
74+
remotes/uwpce/master
75+
76+
Now, you have a copy of all the work you've done to date. It's on the
77+
*keep-old-work* branch. You have not yet pushed this branch up to your github
78+
account, so let's do that next, making it safe::
79+
80+
$ git push -u origin keep-old-work
81+
Total 0 (delta 0), reused 0 (delta 0)
82+
To [email protected]:cewing/training.python_web.git
83+
* [new branch] keep-old-work -> keep-old-work
84+
Branch keep-old-work set up to track remote branch keep-old-work from origin.
85+
86+
Okay, now there's a copy of your old work safe in a branch on *your* github
87+
repository.
88+
89+
Revert Your Master
90+
++++++++++++++++++
91+
92+
The next step is to *roll back your master* to a point *before you made any
93+
changes to it*.
94+
95+
The key here is understanding that every change you commit to a repository in
96+
git is associated with a *hash*, which is a big, unique identification number
97+
you can use to refer to that specific change. You can see these numbers when
98+
you look at the list of commits in github.
99+
100+
You need to find the number of a commit by me that happened before you began
101+
making changes.
102+
103+
First, open the 'commits' page on github of your fork of the class repository.
104+
105+
Then, scroll down until you find your first commit, which should be part of
106+
work for session01 homework.
107+
108+
Then, find the last commit *before* that commit, and click on the number in the
109+
far right of that commit listing (it should be something like `b60ea2bb70`)
110+
111+
This will open up that specific commit, and in the URL for that commit you will
112+
find the full hash: `b60ea2bb7052a5bd300772d7d9d40b19b27f7a1b`. Copy that value.
113+
114+
Now, we are going to reset your local *master* branch to that commit,
115+
abandoning all the changes you (and I) have made between then and now::
116+
117+
$ git branch
118+
keep-old-work
119+
* master
120+
$ git reset --hard b60ea2bb7052a5bd300772d7d9d40b19b27f7a1b
121+
122+
Now, your *local master* has been reverted to a state before you did any work.
123+
All your changes have been deleted, but so have all the changes I've made since
124+
the start of class.
125+
126+
Luckily, we can fix that. Our next step is to fetch the *uwpce* *master*
127+
branch, which contains all those changes I've made, but none of the changes you
128+
made:
129+
130+
$ git fetch uwpce master
131+
remote: Counting objects: 10, done.
132+
remote: Compressing objects: 100% (10/10), done.
133+
remote: Total 10 (delta 3), reused 7 (delta 0)
134+
Unpacking objects: 100% (10/10), done.
135+
From github.com:UWPCE-PythonCert/training.python_web
136+
* branch master -> FETCH_HEAD
137+
8873ba1..75a8462 master -> uwpce/master
138+
139+
And finally, we can merge the changes in the *uwpce* master into our local
140+
*master*::
141+
142+
$ git branch
143+
keep-old-work
144+
* master
145+
$ git merge uwpce/master
146+
Merge made by the 'recursive' strategy.
147+
source/presentations/session04.rst | 7 +
148+
source/presentations/session06.rst | 1624 +-----------------------------------
149+
2 files changed, 40 insertions(+), 1591 deletions(-)
150+
151+
152+
Forcibly Update
153+
+++++++++++++++
154+
155+
Now, what we have is a situation where your local master has a history that is
156+
completely different from the *origin* to which it is attached. Your
157+
*origin/master* still has your work on it, interleaved with the changes I've
158+
made along the way, but your *local* master contains only my work.
159+
160+
If you were to try to push these changes up to *origin* (your repository) it would
161+
fail because there's no way to reconcile the two histories.
162+
163+
But we don't care about the history on your *origin*, we only want to keep the
164+
history that is represented by what is currently in your *local* master branch.
165+
To do that, we can push with the `--force` option::
166+
167+
$ git push --force origin master
168+
Counting objects: 25, done.
169+
Delta compression using up to 8 threads.
170+
Compressing objects: 100% (11/11), done.
171+
Writing objects: 100% (11/11), 2.04 KiB | 0 bytes/s, done.
172+
Total 11 (delta 7), reused 0 (delta 0)
173+
To [email protected]:cewing/training.python_web.git
174+
+ 782d17e...5fb97f3 master -> master (forced update)
175+
176+
Okay. This means that now *master* both on your local machine and on the
177+
*origin* remote (your github repository) is identical to (and up to date with)
178+
the master in the *uwpce* repository.
179+
180+
181+
Going Forward
182+
-------------
183+
184+
From now on, when you want to get the very latest copies of the *uwpce*
185+
repository, you can issue these commands::
186+
187+
$ git checkout master
188+
$ git fetch uwpce master
189+
$ git merge uwpce/master
190+
$ git push origin master
191+
192+
That will fetch the changes from the *uwpce* remote *master* branch, merge them
193+
into your *local* repository *master* branch, and then push those changes up to
194+
your *origin* repository *master* branch.
195+
196+
And when you are ready to start work on a new homework assignment, you can
197+
simply start a new branch::
198+
199+
$ git checkout -b session05-homework
200+
201+
Once you've completed your homework, and committed all the changes to your
202+
*local* homework branch, you can push that branch up to your *origin*
203+
repository::
204+
205+
$ git push origin session05-homework
206+
207+
And then, when you open a pull request for me to review your homework, you can
208+
select your *homework branch* as the source of the pull request, and my
209+
*master* branch as the destination. The request will contain only those changes
210+
that are germane to your homework.
211+

0 commit comments

Comments
 (0)