Skip to content

Instantly share code, notes, and snippets.

@SViccari
Last active April 7, 2019 19:47
Show Gist options
  • Save SViccari/8e2ac529ab95993d52f39a77f750f9eb to your computer and use it in GitHub Desktop.
Save SViccari/8e2ac529ab95993d52f39a77f750f9eb to your computer and use it in GitHub Desktop.
Git Tips

In the example below, we will:

  • update our local master branch to include the latest changes in GitHub's master 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)

Example Workflow

Update Master to pull in any recent changes

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

Create a new branch to make changes

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

Ready to commit code changes

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 that git 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"

Push code changes to GitHub

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

Squash commits

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))

Push branch with squashed commits to GitHub

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

Helpful Tips:

To change the branch's latest commit message

git commit --amend (opens the latest commit)

Entered a rebase by mistake? How to abort:

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:

  1. Exit the vim editor with an error code. To exit vim with an error code, enter :cq
  2. 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.

Understanding the difference between origin, master, and origin/master:

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.

Helpful Commands:

git status (shows the files that have been altered)
git diff master (will compare the current branch against master)

The tool that Steph used instead of using git diff master or git log: Tig

brew install tig
tig (to launch)
q (to quit)
j (move down)
k (move up)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment