Skip to content

Commit 980e3cc

Browse files
committed
added iteractive adding and rebasing docs
1 parent 2a2247f commit 980e3cc

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
11
## Interactive Rebasing ##
22

3+
You can also rebase interactively. This is often used to re-write your
4+
own commit objects before pusing them somewhere. It is an easy way to
5+
split, merge or re-order commits before sharing them with others. You
6+
can also use it to clean up commits you've pulled from someone when
7+
applying them locally.
8+
9+
If you have a number of commits that you would like to somehow modify
10+
during the rebase, you can invoke interactive mode by passing a '-i' or
11+
'--interactive' to the 'git rebase' command.
12+
13+
$ git rebase -i origin/master
14+
15+
This will invoke interactive rebase mode on all the commits you have made
16+
since the last time you have pushed (or merged from the origin repository).
17+
18+
To see what commits those are beforehand, you can run log this way:
19+
20+
$ git log github/master..
21+
22+
Once you run the 'rebase -i' command, you will be thrown into your editor
23+
of choice with something that looks like this:
24+
25+
pick fc62e55 added file_size
26+
pick 9824bf4 fixed little thing
27+
pick 21d80a5 added number to log
28+
pick 76b9da6 added the apply command
29+
pick c264051 Revert "added file_size" - not implemented correctly
30+
31+
# Rebase f408319..b04dc3d onto f408319
32+
#
33+
# Commands:
34+
# p, pick = use commit
35+
# e, edit = use commit, but stop for amending
36+
# s, squash = use commit, but meld into previous commit
37+
#
38+
# If you remove a line here THAT COMMIT WILL BE LOST.
39+
# However, if you remove everything, the rebase will be aborted.
40+
#
41+
42+
This means that there are 5 commits since you last pushed and it gives you
43+
one line per commit with the following format:
44+
45+
(action) (partial-sha) (short commit message)
46+
47+
Now, you can change the action (which is by default 'pick') to either 'edit'
48+
or 'squash', or just leave it as 'pick'. You can also reorder the commits
49+
just by moving the lines around however you want. Then, when you exit the
50+
editor, git will try to apply the commits however they are now arranged and
51+
do the action specified.
52+
53+
If 'pick' is specified, it will simply try to apply the patch and save the
54+
commit with the same message as before.
55+
56+
If 'squash' is specified, it will combine that commit with the previous one
57+
to create a new commit. This will drop you into your editor again to merge
58+
the commit messages of the two commits it is now squashing together. So,
59+
if you exit the editor with this:
60+
61+
pick fc62e55 added file_size
62+
squash 9824bf4 fixed little thing
63+
squash 21d80a5 added number to log
64+
squash 76b9da6 added the apply command
65+
squash c264051 Revert "added file_size" - not implemented correctly
66+
67+
Then you will have to create a single commit message from this:
68+
69+
# This is a combination of 5 commits.
70+
# The first commit's message is:
71+
added file_size
72+
73+
# This is the 2nd commit message:
74+
75+
fixed little thing
76+
77+
# This is the 3rd commit message:
78+
79+
added number to log
80+
81+
# This is the 4th commit message:
82+
83+
added the apply command
84+
85+
# This is the 5th commit message:
86+
87+
Revert "added file_size" - not implemented correctly
88+
89+
This reverts commit fc62e5543b195f18391886b9f663d5a7eca38e84.
90+
91+
Once you have edited that down into once commit message and exit the editor,
92+
the commit will be saved with your new message.
93+
94+
If 'edit' is specified, it will do the same thing, but then pause before
95+
moving on to the next one and drop you into the command line so you can
96+
amend the commit, or change the commit contents somehow.
97+
98+
If you wanted to split a commit, for instance, you would specify 'edit' for
99+
that commit:
100+
101+
pick fc62e55 added file_size
102+
pick 9824bf4 fixed little thing
103+
edit 21d80a5 added number to log
104+
pick 76b9da6 added the apply command
105+
pick c264051 Revert "added file_size" - not implemented correctly
106+
107+
And then when you get to the command line, you revert that commit and create
108+
two (or more) new ones. Lets say 21d80a5 modified two files, file1 and file2,
109+
and you wanted to split them into seperate commits. You could do this after
110+
the rebase dropped you to the command line :
111+
112+
$ git reset HEAD^
113+
$ git add file1
114+
$ git commit 'first part of split commit'
115+
$ git add file2
116+
$ git commit 'second part of split commit'
117+
$ git rebase --continue
118+
119+
And now instead of 5 commits, you would have 6.
120+
121+
The last useful thing that interactive rebase can do is drop commits for you.
122+
If instead of choosing 'pick', 'squash' or 'edit' for the commit line, you
123+
simply remove the line, it will remove the commit from the history.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,104 @@
11
## Interactive Adding ##
22

3+
Interactive Adding is a really nice way of working with and visualizing
4+
the Git index. To start it up, simply type 'git add -i'. Git will show
5+
you all the modified files you have and their status.
6+
7+
$>git add -i
8+
staged unstaged path
9+
1: unchanged +4/-0 assets/stylesheets/style.css
10+
2: unchanged +23/-11 layout/book_index_template.html
11+
3: unchanged +7/-7 layout/chapter_template.html
12+
4: unchanged +3/-3 script/pdf.rb
13+
5: unchanged +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
14+
15+
*** Commands ***
16+
1: status 2: update 3: revert 4: add untracked
17+
5: patch 6: diff 7: quit 8: help
18+
What now>
19+
20+
In this case, we can see that there are 5 modified files that have not been
21+
added to our index yet (unstaged), and even how many lines have been added to
22+
or removed from each. It then shows us an interactive menu of what we can
23+
do in this mode.
24+
25+
If we want to stage the files, we can type '2' or 'u' for the update mode.
26+
Then I can specify which files I want to stage (add to the index) by typing
27+
in the numbers of the files (in this case, 1-4)
28+
29+
What now> 2
30+
staged unstaged path
31+
1: unchanged +4/-0 assets/stylesheets/style.css
32+
2: unchanged +23/-11 layout/book_index_template.html
33+
3: unchanged +7/-7 layout/chapter_template.html
34+
4: unchanged +3/-3 script/pdf.rb
35+
5: unchanged +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
36+
Update>> 1-4
37+
staged unstaged path
38+
* 1: unchanged +4/-0 assets/stylesheets/style.css
39+
* 2: unchanged +23/-11 layout/book_index_template.html
40+
* 3: unchanged +7/-7 layout/chapter_template.html
41+
* 4: unchanged +3/-3 script/pdf.rb
42+
5: unchanged +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
43+
Update>>
44+
45+
If I hit enter, I will be taken back to the main menu where I can see that
46+
the file status has changed:
47+
48+
What now> status
49+
staged unstaged path
50+
1: +4/-0 nothing assets/stylesheets/style.css
51+
2: +23/-11 nothing layout/book_index_template.html
52+
3: +7/-7 nothing layout/chapter_template.html
53+
4: +3/-3 nothing script/pdf.rb
54+
5: unchanged +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
55+
56+
Now we can see the first four files are staged and the last one is still not.
57+
This is basically a compressed way to see the same information we see when
58+
we run 'git status' from the command line:
59+
60+
$ git status
61+
# On branch master
62+
# Changes to be committed:
63+
# (use "git reset HEAD <file>..." to unstage)
64+
#
65+
# modified: assets/stylesheets/style.css
66+
# modified: layout/book_index_template.html
67+
# modified: layout/chapter_template.html
68+
# modified: script/pdf.rb
69+
#
70+
# Changed but not updated:
71+
# (use "git add <file>..." to update what will be committed)
72+
#
73+
# modified: text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
74+
#
75+
76+
There are a number of useful things we can do, including unstaging files (3: revert),
77+
adding untracked files (4: add untracked), and viewing diffs (6: diff). Those
78+
are all pretty straightforward. However, there is one command that is pretty
79+
cool here, which is staging patches (5: patch).
80+
81+
If you type '5' or 'p' in the menu, git will show you your diff patch by patch
82+
(or hunk by hunk) and ask if you want to stage each one. That way you can
83+
actually stage for a commit a part of a file edit. If you've edited a file
84+
and want to only commit part of it and not an unfinished part, or commit
85+
documentation or whitespace changes seperate from substantive changes, you can
86+
use 'git add -i' to do so relatively easily.
87+
88+
Here I've staged some changes to the book_index_template.html file, but not all
89+
of them:
90+
91+
staged unstaged path
92+
1: +4/-0 nothing assets/stylesheets/style.css
93+
2: +20/-7 +3/-4 layout/book_index_template.html
94+
3: +7/-7 nothing layout/chapter_template.html
95+
4: +3/-3 nothing script/pdf.rb
96+
5: unchanged +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
97+
6: unchanged +85/-0 text/15_Interactive_Adding/0_ Interactive_Adding.markdown
98+
99+
When you are done making changes to your index through 'git add -i', you simply
100+
quit (7: quit) and then run 'git commit' to commit the staged changes. Remember
101+
**not** to run 'git commit -a', which will blow away all the careful changes
102+
you've just made and simply commit everything.
103+
3104
[gitcast:c3_add_interactive]("GitCast #3: Interactive Adding")

text/16_Stashing/0_ Stashing.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ After that, you can go back to what you were working on with
2121

2222
$ git stash apply
2323

24+
25+
### Stash Queue ###
26+
27+
You can also use stashing to queue up stashed changes.
28+
If you run 'git stash list' you can see which stashes you have saved:
29+
30+
$>git stash list
31+
stash@{0}: WIP on book: 51bea1d... fixed images
32+
stash@{1}: WIP on master: 9705ae6... changed the browse code to the official repo
33+
34+
Then you can apply them individually with 'git stash apply stash@{1}'. You
35+
can clear out the list with 'git stash clear'.

0 commit comments

Comments
 (0)