Git Good

A guide to git for Carter to refer people to.

Adding code to git

Once code has been written, it must be staged, committed, and pushed.

First, run git status. Verify that nothing is listed in Changes to be committed and the files you have changed are listed in Changes not staged for commit or Untracked files. If something is listed in Changes to be committed, run git reset.

Next, stage changes.

Then, commit changes. Run git commit for a long commit message, or git commit -m "[message]" for a short message.

Finally, run git pull (in case there are remote changes to your branch that need to be merged), followed by git push. If this is your first time pushing this branch, run git push --set-upstream origin [branch name].

Branch Managing

In a shared repository, or a repository with a stable working version, all development should be done in branches, then merged.

Branch Creation

The following steps will create a branch from the main branch. If you wish to use another branch, substitute [main branch] for whatever branch you wish to base the new branch on.

  1. git checkout [main branch]
  2. git pull
  3. git checkout -b [new branch]

Branch Merging for the Individual

If you are not using a pull request system, use the following steps to merge a branch ([new branch]) back into the main branch. If you wish to use another branch, substitute [main branch] for whatever branch you wish to base the new branch on.

  1. git checkout [main branch]
  2. git pull
  3. git checkout [new branch]
  4. git merge [main branch]
  5. Resolve (edit, add, commit, but no need to push) merge conflicts with your favorite text editor.
  6. git checkout [main branch]
  7. git merge [new branch] (No merge conflicts because they are already resolved.)
  8. git push
  9. git branch -d [new branch]

Branch Merging for the Team

If you are using a pull request system, use the following steps to prepare a branch for a pull request. If you wish to use another branch, substitute [main branch] for whatever branch you wish to base the new branch on.

  1. git checkout [main branch]
  2. git pull
  3. git checkout [new branch]
  4. git merge [main branch]
  5. Resolve (edit, add, commit, and push) merge conflicts with your favorite text editor.
  6. Open pull request via shared git repository host.
Depending on the team, steps 1 through 5 may need to be repeated as changes are requested. The shared git repository host should automatically update the already open pull request.

Deleting local changes

Warning: this could lead to loss of code.

To reset a local repository to the latest commit, run git reset --hard.

Creating a git repository

To create a local repository, run git init. Consult the documentation of a git repository host to synchronize with them (this usually involves git remote add origin [url] followed by git push --set-upstream origin [main branch]).