In the example below, we will:
- update our local
master
branch to include the latest changes in GitHub'smaster
branch - create a new branch to make code changes. We'll call this branch
my-new-branch
. - save code changes as commits on
my-new-branch
- share
my-new-branch
with GitHub (with the intent of opening a PR) - use git's
rebase
feature to reduce the number of commits to one commit (this is often referred to as squashing commits)
The following will update your local master
branch to match the repository's master
branch. When other developers merge code into the master branch on GitHub, this step will update your local master
branch to include those changes.
Note about origin
: origin
references the GitHub Repo's URL, you can confirm this by running git remote -v
.
git checkout master
.
git pull origin master
The following will create a new branch called "my-new-branch" and move to that newly created branch.
git branch my-new-branch
git checkout my-new-branch
The git commands below will:
- show you a high level view of files that have been changed (
git status
). - tell git to prepare the changes to be committed/saved (
git add
.). Note thatgit add .
will add ALL files. To add just one file:git add path/to/file/
- save the changes with a message
git commit -m "some message about the code changes"
git status
git add .
git commit -m "some message about the code changes"
To share your code with your team, you'll send your local branch to GitHub (referred to as pushing
your changes)
git push origin my-new-branch
After making code changes and receiving feedback from your team, you may have several commits on my-new-branch
.
Before merging, you'll likely want to collapse these individuals commits into one commit. To collapse several
commits into one commit, use git's rebase
command.
The following steps will ensure sure your local master
branch is up-to-date with GitHub, navigate to my-new-branch
, and then run the rebase
command.
git checkout master
git pull origin master
git checkout my-new-branch
git rebase -i origin/master
(rebase current branch (my-new-branch
) against the Github's master branch. This opens a rebase session in Vim.)
i
(enter insert mode. Change commit labels to use fixup
or squash
)
ctrl + c
(will exit insert mode)
:wq
(will continue the rebase process and apply the changes (stands for write
and quit
))
In this workflow, you've already pushed my-new-branch
to Github. After rebasing, you will have to force push the branch, which is what -f
flag represents. For future instances where you haven't pushed your branch to GitHub (yet), the -f
isn't necessary.
git push origin my-new-branch -f
git commit --amend
(opens the latest commit)
If you've run the command rebase -i
and your in vim session that shows the commits on your branch,
here are two ways you can abort the rebase:
- Exit the vim editor with an error code. To exit vim with an error code, enter
:cq
- Delete ALL the commits shown in the vim session (the vim command
dd
will delete a line). Once the commits are deleted, use the:wq
command to continue the rebase process. Git will see all the commits were removed and will essentially abort the rebase process by doing nothing.
origin
- points to the remote repo on GitHub
master
- name of a branch
origin/master
- points to the branch named master
on the Github Repo, can also referenced as remotes/origin/master
.
git status
(shows the files that have been altered)
git diff master
(will compare the current branch against master)
brew install tig
tig
(to launch)
q
(to quit)
j
(move down)
k
(move up)