Part 3 - Summary
This completes Part 3.
To summarize, I'm going to demonstrate a common workflow using the commands we learned in Part 3.
Under this workflow, I keep the master branch clean. This means I try not to make commits directly to master.
Instead I create a branch off master to do any new work.
This branch is often called a feature or topic branch.
Once work is complete I merge the branch back in master. I repeat the process for any new work.
I'll demonstrate this on the repository we've been using during Part 3.
Just a reminder, I'll go a little fast. Don't feel like you have to follow along with the commands. Instead focus on the process.
First I'll make sure I'm on the master branch.
Looks good.
Since the master branch only has completed work, I know it's in a clean state.
So I can start new work from master.
I want to update the timestamps for the files in the repository.
So I'll create and checkout a branch called updating-timestamps.
I'll update the timestamp for File 1 and commit that change.
I'll do same for File 2.
And I'm done.
Before merging, I'll review my work with git log --oneline
.
Now that I'm done, there's really no need for these to be two separate commits. Both relate to updating timestamps.
So I'll squash them down into one commit with:
git rebase -i
Remember git rebase -i
takes the SHA of the last commit you want to keep. So I can pass it the SHA of my first commit followed by a caret.
Or, under this workflow, I know if I want to rebase all the commits on my feature branch, that's the same commit as master references.
So I can just pass it master.
I'll edit the commit history by changing pick
to squash
or s
for short.
I'll save this and update the message.
I'll run git log
again and see that my commits were squashed and the message has been updated.
Now I'm ready to merge so I'll check out the master branch and run git merge update-timestamps
.
Now I need to add a few new files in my project.
So I'll create another branch called new-files.
I'm going to add File 3 and then I'll add File 4.
Again I'll run git log
to review my recent changes.
Now I realized I no longer need File 3. So I don't want to merge the whole branch back in the master.
While I could remove the file in a commit or rebase and drop the original commit, using git cherry-pick
is faster.
So I'll switch back to the master branch and run git cherry-pick
and pass at the SHA of the commit with File 4.
If I run git log
again I see that I have the commit with File 4.
The last thing I want to do before finishing up is remove the branches for the work I've merged.
This helps keep the repository clean and avoids any confusion with future work.
So I'll run git branch -d updating-timestamps
and git branch –d new-files
.
Git warns me that the new-files branch was not merged.
This is true because it contains additional commits that were not merged into master.
Since I cherry-picked the commits I wanted I know it's ok.
So I'll follow the suggestion and use git branch –D new-files
to force the remove.
Adopting such a workflow as doing new work on feature branches gives me confidence that at any point the master branch is clean and stable.
Using feature branches also gives me the freedom to try new things and make big changes without worry about breaking anything since Git tracks the branch separately.
While we'll discuss popular workflows in one of the Everyday Git videos, all of them use branches.
So if you master the commands covered in Part 3, you'll be able to manage any workflow.