Skip to main content

Using Git

caution

If you are not familiar with Git command line, do not pretend you know it.

Instead, install GitHub Desktop. It will make your life easier.

Setup your project#

Install GitHub Desktop.

Click Clone or download in the repository page and choose Open in Desktop.

Clone from desktop

GitHub Desktop will auto open and guide you to clone the repository.

Pull latest changes from remote#

Try keep your branch up-to-date with remote. Pull often.

Click Pull origin in the header.

Pull origin Pulling origin

Create a new branch#

Command/Ctrl + Shift + N or click Branch -> New Branch in Menu.

New Branch

Input the name of the new branch and GitHub Desktop will automatically go to the new branch.

Delete a branch#

Command/Ctrl + Shift + D or click Branch -> Delete in Menu.

Delete Branch

Click the Delete button.

Review your changes locally#

Just see what's changed directly within GitHub Desktop.

Partial Commit

Select changes to commit#

You can unselect the files you don't want to commit. Inside a file, you can deselect lines that you don't want to commit. (Only lines highlighted with blue on the left will be committed.)

Partial Commit

The fill in the commit message, and click Commit to [your branch name].

Commit

Push to remote#

After you commit, you should push your changes to remote, so that

  • If you computer dies, changes will persist;
  • Other team members will see your progress.

Depending on whether you have published your branch before, you will either see

Publish Branch

or

Push

After you clicked the button, you can see the progress.

Push Progress

Merge in master branch changes#

  1. Select History Tab Merge: Select History Tab
  2. Select master branch Publish Branch
  3. Click the merge button Publish Branch
  4. If there are merge conflicts, GitHub Desktop will prompt you to fix them.

Amend and force-push#

danger

Only do this if you are sure that you are the only one who will work on this branch.

Sometimes you want to maintain a clean commit history. For example, you don't want to have a series of commits like

-o- Implement feature foo
-o- Fix syntax error
-o- Fix linter error
-o- Fix linter error again
-o- Fix syntax error again
-o- Fix linter error again again

You can avoid commits like Fix linter error again again by amend your previous commit.

Run

git add .
git commit --amend --no-edit

to add all your unstaged changes to your commit.

If the commit is already pushed to remote, you need to force push.

git push -f

Rebase and force-push#

danger

Only do this if you are sure that you are the only one who will work on this branch.

Merge commits sometimes turn a nice linear commit history into a messy tree. If you want a nice linear history, you will need to rebase when you want to "merge" in the latest master change.

  1. Command/Ctrl + Shift + E or click Branch -> Rebase Current Branch in menu Merge: Select History Tab
  2. Select master branch and click rebasePublish Branch
  3. If there are merge conflicts, GitHub Desktop will prompt you to fix them.