These instructions use <gh-username> as a placeholder for your own github username. You should replace it with your username in any command.
Fork this repository to your personal GitHub space. Specifically:
- Make sure you are logged in to github.com
- Go to https://github.com/ubsuny/compphys-git-exercise
- Click the "fork" button
- On the forking page, accept the default option and click "Create fork"
git clone git@github.com:<gh-username>/compphys-git-exercise
You can use the following commands to inspect the branches and remote configuration:
git branch -a # -a lists both local and remote branches
git remote -v # -v is verbose mode
- Copy the file
template.mdto<gh-username>.md(just work in your localmainbranch, don't worry about creating a new branch):
cp template.md <gh-username>.md- Open the new file in a text editor and answer the questions.
nano <gh-username>.md
# (Answer the questions)- Commit the new file to your local git repo. Specifically:
- Stage the file (i.e., tell git you want this file in your next commit) with
git add <gh-username>.md - Do
git statusto see what happened. - Commit the file (wrap up the staged files into a commit) with
git commit -m "type a log message") - Do
git statusagain to see what happened. - Do
git logto see your commit, now permanently recorded in the history of this branch.
- Stage the file (i.e., tell git you want this file in your next commit) with
git add <gh-username>.md
git status
git commit -m "Add <gh-username>.md"
git status
git log
- Push the files to the remote repo on github.
git push
# Or maybe 'git push origin main'
- Check that you can see the updates on github.com (i.e. open a web browser and find the updated files).
If you can see the updates on github, then the instructor can see them, too. When we get to homework assignments, this counts as "turning in" the assignment. You can continue pushing more changes until the due date (or beyond, depending on the instructor).
- Create a pull request to merge your fork back to the original ubsuny repo. Navigate to your forked repo on github: there should be a big green button to create a pull request. Note: pull requests are a GitHub-specific feature (not part of git), but are nonetheless a key part of managing software projects.
- Inside your local
compphys-git-exercisefolder, create a new branch with the following command:
git switch -c feature-test
git switch is the overall command for switching between branches. The -c flag creates a new branch, so this command basically creates a new branch, and then switches to it.
- Make a modification to
<gh-username>.md. For example, add your favorite color at the bottom. - Repeate the git add and commit:
git add <gh-username>.md
git commit -m "Add favorite color"
You now have a new commit on the feature branch, corresponding to the new <gh-username>.md file.
4. Finally, let's merge the new commit back into your main branch. First, switch back to the main branch:
git switch main
If you look at your markdown file now (less <gh-username>.md), the update will not be there, because it only exists in the feature-test branch. Don't worry! Nothing has been deleted. Similarly, if you do git log, you will not see the new commit.
Next, we merge the feature-test branch into the main branch:
git merge feature-test
You should now see the color update in less <gh-username>.md, as well as the new commit in git log.
Once the instructor has merged everyone's pull requests, your local repo will now be out of sync with the ubsuny repo. Let's use git to synchronize your local repo, basically downloading everyone's <gh-username>.md files.
First, your local repo is only connected to your fork on github, not the ubsuny repo. We can use git remote add to add the ubsuny repo as another remote:
git remote add ubsuny git@github.com:ubsuny/compphys-git-exercise
If you do git remote -v, you should see the new remote named ubsuny connected to your local repo.
Next, use git fetch and git merge (git pull is another way, basically combining the two commands):
git fetch ubsuny
git merge ubsuny/main
Hopefully, the merge operation goes smoothly. If not, resolving merge conflicts is worth learning, but is a bit beyond the scope of this exercise (see, for example, github merge conflict tutorial).