git and Teamwork

These videos from a prior class cover using git, branches, and merging with teams.

Working in a branch

To create a new branch (branch_name), clone the repo as usual and then do:

git checkout -b branch_name

git status will now show that you are on a new branch. add and commit as usual as you work, and those changes will be appended to your local branch only. Because you are working in your own branch, you can also push whenever you like and it won't break anything anyone else is working on. As usual, do:

git push

The first time you do this, it will error and tell you:

fatal: The current branch branch_name has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin branch_name

This is fine, do the command as suggested in order to create the remote branch on github.

In order to update your local repo with branches and commits from the server, commit all your work first, and then do:

git pull

This is useful if you're working back and forth on multiple machines, or to get all your teammates' branches to look at. To see what is in another branch (or work in another branch), commit your work first, and then do:

git checkout other_branch_name

All the code in your working directory will change to reflect what is in that branch, and you can run the project there to see what it does, look at the files, etc. If you make changes and add/commit/push, those changes will be added to that branch.

To go back to your branch (or any other), do:

git checkout branch_name

To see all the local branches, use:

git branch

You can view all the remote branches and manage them easily on the github website.

Merging with branches

As you work in a branch, you should be pulling new content from the master or main branch and merging it into your work. The master branch is for code that the team has determined should be adopted by all team members.

To do this:

(add/commit any changes on your own branch before switching)
git checkout master
git pull
git checkout your_branch
git merge master

If there are any changes to be merged, your branch will enter the merge state. From this state:

  • Resolve any conflicts
  • add, commit, and push

As long as you keep merging in changes from master, eventually merging your branch into master will be easy. Only merge into master *after* you merge master into your branch and fix any problems.

Resolving Conflicts

If your merge generates conflicts (in red from status), you have to resolve each one. Your options are:

  • Keep your version and discard the one being merged in:
    git checkout --ours -- <filename>
  • Keep the version being merged in and discard yours:
    git checkout --theirs -- <filename>
  • Open the file in an editor (VS Code does this well) and edit it to fix the line-by-line conflicts

After any of these options, mark it resolved with:

git add <filename>

You can use . and * to resolve multiple files at the same time. The --ours, --theirs, and add operations will only apply to files that are listed as in conflict.

If you run into trouble, you can always bail out back to pre-merge and change filenames, delete things, etc before merging again.

git merge --abort

Commit and push

If you conflicts were resolved (by you or automatically), you'll need to commit those changes.

Push the completed merge.