GOAL: In this project, you and your partner will be creating a list of apples in a text document, all version-controlled with Git! While this project has an apples-listing theme, the apples (and the project) is not the point: The real goal is to give us an introduction to how we’ll be collaborating via Git in the coming lessons.
Collaborators: Evie Cutrell
-
Open the project in VS Code. From within your project directory, type code ., and that should open your project in VS Code.
-
Open the README and edit the line that says Collaborators: _________________ to include your names.
-
Create a new file,
apples.txt
. Add one item granny smith in it. -
In your terminal type
git status
:- If it is not already,
git status
should become one of your favorite and most used git commands. It gives you a lot of information, including which branch you are on (more about branches soon), and information about the state of files that have been modified since the last commit.
- If it is not already,
-
You should see both your
README.md
andapples.txt
files listed in red. This means they have not yet been staged to be included in the next commit. -
You should notice that they are listed in different sections. README.md is in a category called "Changes not staged for commit" while apples.txt is under "Untracked files". What’s the difference between “unstaged” and “untracked” files?
- Add these files to the staging area! We do this using the
git add
command. Go ahead and typegit add
in the terminal. - Oops! Looks like you got an error message. We have to specify which files you want to add to the staging area. There are a couple of ways to do so:
- If you want to add only some of the modified files to the staging area, you can use the pathname of the file. This might look like
git add README.md apples.txt
- If you want to stage every file with changes, including untracked files, you can use
git add -A
(or the longer equivalentgit add --all
) - A popular shortcut to stage all of your changes is
git add .
- If you want to add only some of the modified files to the staging area, you can use the pathname of the file. This might look like
- Using any of the above methods, add
README.md
andapples.txt
to the staging area. - Type
git status
you should see both files listed in green under "Changes to be committed."
- Work together as partners to add a few apples to
apples.txt
. It doesn’t matter how many or which ones. - Since Partner A has been driving and Partner B has been navigating for a while, it is time to switch roles. Before Partner A commits the changes you have made, do one more
git status
. You will see that because you made changes toapples.txt
after it was staged, you now have to add those changes to the staging area. - Partner A should execute the following commands to commit all of the changes you have made so far.
git add .
git commit -m "A message describing changes since the last commit"
- If you type
git status
, you should see a message saying there are no changes to commit and that your branch is 1 commit ahead of 'origin/main'.
- Both Partners: Go to your personal GitHub fork of the repo. You will notice that even if you refresh the page, you will not see Partner A's commit or any of the changes you have made so far. Currently Partner A has only been making changes to their local repository.
- Partner A: To see the commit in their GitHub remote, Partner A must push those changes. To do so, type the
git push
command, followed by the name of the remote you want to push changes to and then the branch to push to (in junior phase, you will almost always push to the main branch, but you can push to other branches as well).
git push origin main
- Partner A: Type git status, you should see that your branch is up to date with 'origin/main'.
- Partner A: Refresh their GitHub page again. You should now see your commit!
- Partner B: Refresh your GitHub page. Do you see Partner A’s commits? What!?! No changes. No new commits.
- Partner B: To access Partner A’s work, Partner B should pull from Partner A's remote repository. Use the name you assigned to Partner A's repo when you added it as a remote. Again, we specify that we want to pull from the main branch.
git pull partner main
// Replace "partner" with the name of your partner's branch
- Type
git pull
command fetches any changes from the remote and merges them into the local branch. - Partner B should now see the changes Partner A made in their local repository!
- Partner B: Type
git log
. You should see Partner A's commit at the top of the list. This is because when you merge a branch into one you are currently in, all of the commits in the second branch since their histories diverged are incorporated into the current branch. - Partner B: Go refresh your GitHub page one more time. What happened? Why?
- Partner B: What do you think you will see when Partner B types
git status
? Type git status to see if you were correct! - You now have two options for how to proceed. Partner B can either:
- Push Partner A's commit to their GitHub repo before making new changes.
- Continue working on their local repository and push both Partner A's commit and any commits they make at the same time.
- If you choose to push the changes now, you should type the same command Partner A did a few steps ago:
git push origin main
- Partner A: Change the granny smith apple in your
apples.txt
to any other apple. Save it, but don’t yet commit.
- Discuss what you should expect to see when Partner B types
git status
. Partner B: Type thegit status
command. Did you see what you expected? - **Partner B: Commit your changes and push them up to GitHub.
- Partner A: Try to pull the changes from Partner B's repo.
git pull partner main
- Oh no! You should be seeing an error message that local changes would be overwritten and therefore the merge is aborted. Remember when Partner A (purposefully) made changes to the repo despite being the navigator? What do you see when you do git status? Why is this problematic?
There are a number of ways you can resolve the problem you are currently experiencing. The solution you will use now is particularly helpful if you don't need to keep any of the changes you have made since your last commit (like if you accidentally edited a file).
- Type
git stash
. Your repository should revert to your last commit. Now when you do git status you should see that your working tree is clean. You should now be able to successfully pull from Partner B's repo. Go ahead and do so now.