4. How do git branches work?#
4.1. Review#
Recall, We can move around and examine the computer’s file structure using shell commands.
cd Documents/inclass/systems/github-inclass-fa23-brownsarahm/
To confirm our current working directory we print it with pwd
pwd
/Users/brownsarahm/Documents/inclass/systems/github-inclass-fa23-brownsarahm
this confirms what we expect
Now let’s see what is in our folder:
ls
README.md about.md
and check in with git
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
nothing to commit, working tree clean
this is as we left off.
4.2. Branches do not sync automatically#
Now we will look in GitHub and see what is there.
Warning
the step below requires that you have the gh
CLI. On windows with GitBash, you have not needed this, and it can be bit tricky to install. If you get it done and working well, and you submit instructions to this repo about how to get it set up, for either a community badge (if it’s simple) or an explore badge if it takes a lot
gh repo view --web
Opening github.com/introcompsys/github-inclass-fa23-brownsarahm in your browser.
On Github, we see that it matches the branch we were on because we had merged in our PR in class on Thursday.
git status
On branch 1-create-an-about-file
Your branch is up to date with 'origin/1-create-an-about-file'.
nothing to commit, working tree clean
Back on our local computer, we will go back to the main branch, using git checkout
git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
now we look at the status of our repo:
ls
README.md
the file is missing. It said it was up to date with origin main, but that is the most recent time we checked github only. It’s up to date with our local record of what is on GitHub, not the current GitHub.
Next, we will update locally, with git fetch
git fetch
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 646 bytes | 215.00 KiB/s, done.
From https://github.com/introcompsys/github-inclass-fa23-brownsarahm
6a12db0..caeacb5 main -> origin/main
Here we see 2 sets of messages. Some lines start with “remote” and other lines do not.
The “remote” lines are what git
on the GitHub server said in response to our request and the
other lines are what git
on your local computer said.
So, here, it counted up the content, and then sent it on GitHub’s side. On the local side, it unpacked (remember git compressed the content before we sent it). It describes the changes that were made on the GitHub side, the main branch was moved from one commit to another. So it then updates the local main branch accordingly (“Updating 6a12db0…caeacb5”).
We can see that this updates th working directory too:
ls
README.md
no changes yet. fetch
updates the .git directory so that git knows more, but does not update our local file system.
We can use git pull to fully update.
git pull
Updating 6a12db0..caeacb5
Fast-forward
about.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 about.md
ls
README.md about.md
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Now we will follow the instruction sin the README issue, commit to a new branch with closes #x
where x is the issue number in the comment and make PR.
Merge the PR and then the issue closes.
4.3. Pull will downlaod and update#
We will git pull
again now all at once, this will do the work that fetch did before and the checkout.
git pull
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.32 KiB | 337.00 KiB/s, done.
From https://github.com/introcompsys/github-inclass-fa23-brownsarahm
caeacb5..5c8aaa9 main -> origin/main
* [new branch] add-name -> origin/add-name
Updating caeacb5..5c8aaa9
Fast-forward
README.md | 4 ++++
1 file changed, 4 insertions(+)
We can see commits with git log
git log
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (HEAD -> main, origin/main, origin/HEAD)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:46:00 2023 -0400
closes #2
commit caeacb503cf4776f075b848f0faff535671f2887
Merge: 6a12db0 693a2b5
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:38:54 2023 -0400
Merge pull request #4 from introcompsys/1-create-an-about-file
create and complete about file closes #1
commit 693a2b5b9ad4c27eb3b50571b3c93dde353320a1 (origin/1-create-an-about-file, 1-create-an-about-file)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:36:03 2023 -0400
create and complete about file closes #1
commit 6a12db0035e7c73772f7b2348b80dd0bfb3a2a2e
Author: github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com>
Date: Thu Sep 14 16:43:07 2023 +0000
Add online IDE url
commit cfe32e5066921ad876d8a2c74b1fcb00c99b1cc7
Author: github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com>
Date: Thu Sep 14 16:43:05 2023 +0000
Initial commit
this is a program, we can use enter/down arrow to move through it and then q
to exit.
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
git pull
Already up to date.
4.4. making a new branch locally#
We’ve used git checkout
to switch branchs before. To also create a branch at the same time, we use the -b
option.
git checkout -b fun_fact
Switched to a new branch 'fun_fact'
git log
Note
notice where each branch pointer is
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (HEAD -> fun_fact, origin/main, origin/HEAD, main)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:46:00 2023 -0400
closes #2
commit caeacb503cf4776f075b848f0faff535671f2887
Merge: 6a12db0 693a2b5
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:38:54 2023 -0400
Merge pull request #4 from introcompsys/1-create-an-about-file
create and complete about file closes #1
commit 693a2b5b9ad4c27eb3b50571b3c93dde353320a1 (origin/1-create-an-about-file, 1-create-an-about-file)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:36:03 2023 -0400
create and complete about file closes #1
commit 6a12db0035e7c73772f7b2348b80dd0bfb3a2a2e
we used the nano text editor. nano
is simpler than other text editors that tend to be more popular among experts, vim
and emacs
. Getting comfortable with nano will get you used to the ideas, without putting as much burden on your memory. This will set you up to learn those later, if you need a more powerful terminal text editor.
nano about.md
this opens the nano program on the terminal. it displays reminders of the commands at the bottom of th screen and allows you to type into the file right away.
Add any fun fact on the line below your content… Then, write out (save), it will prompt the file name. Since we opened nano with a file name (about.md
) specified, you will not need to type a new name, but to confirm it, by pressing enter/return.
My file looks like this.
Sarah Brown
2027
- i skied competitively in high school
git status
On branch fun_fact
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: about.md
no changes added to commit (use "git add" and/or "git commit -a")
This is very similar to when we checked after creating the file before, but, notice a few things are different.
the first line tells us the branch but does not compare to origin. (this branch does not have a linked branch on GitHub)
thefile is listed as “not staged” instead of untracked
it gives us the choice to add it to then commit OR to restore it to undo the changes
We will add it to stage
git add about.md
git status
On branch fun_fact
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: about.md
and commit:
Warning
here i gave an example of what to do if you commit without a message. This is not recommended, but important to know how to fix.
git commit
without a commit message it puts you in vim. Read the content carefully, then press a
to get into ~insert~ mode. Type your message and/or uncomment the template.
When you are done use escape
to go back to command mode, the ~insert~ at the bottom of the screen will go away. Then type :wq
and press enter/return.
What this is doing is adding a temporary file with the commit message that git can use to compelte your commit.
[fun_fact 6d4dbd3] add fun fact
1 file changed, 1 insertion(+)
Now we can look again at where our branch pointers are.
git log
fun_fact moved up to the new commit, but the other branches stayed behind.
commit 6d4dbd33860fceb9c87bd3c4509deff8cecb3f45 (HEAD -> fun_fact)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:06:54 2023 -0400
add fun fact
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (origin/main, origin/HEAD, main)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:46:00 2023 -0400
closes #2
commit caeacb503cf4776f075b848f0faff535671f2887
Merge: 6a12db0 693a2b5
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:38:54 2023 -0400
Merge pull request #4 from introcompsys/1-create-an-about-file
create and complete about file closes #1
commit 693a2b5b9ad4c27eb3b50571b3c93dde353320a1 (origin/1-create-an-about-file, 1-create-an-about-file)
Now we will push to github.
git push
fatal: The current branch fun_fact has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin fun_fact
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
It cannot push, because it does not know where to push, like we noted above that it did not comapre to origin, that was beecause it does not have an “upstream branch” or a corresponding branch on a remote server. It does not work at first because this branch does not have a remote.
git stores its list of remotes in a .git/config
file
cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
[branch "1-create-an-about-file"]
remote = origin
merge = refs/heads/1-create-an-about-file
we can see our other branches have remote = origin
but our new branch is not there.
To fix it, we do as git said.
git push --set-upstream origin fun_fact
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 311 bytes | 311.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'fun_fact' on GitHub by visiting:
remote: https://github.com/introcompsys/github-inclass-fa23-brownsarahm/pull/new/fun_fact
remote:
To https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
* [new branch] fun_fact -> fun_fact
branch 'fun_fact' set up to track 'origin/fun_fact'.
This time the returned message from the remote includes the link to the page to make a PR. We are not goign to make the PR though
4.5. Merge conflicts#
First, in your browser edit the about.md file to have a second fun fact.
Then edit it locally to also have 2 fun facts.
nano about.md
cat about.md
Sarah Brown
2027
- i skied competitively in high school
- I went to Northeastern
now that we have edited in both places let’s pull.
git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 697 bytes | 174.00 KiB/s, done.
From https://github.com/introcompsys/github-inclass-fa23-brownsarahm
6d4dbd3..768dec8 fun_fact -> origin/fun_fact
Updating 6d4dbd3..768dec8
error: Your local changes to the following files would be overwritten by merge:
about.md
Please commit your changes or stash them before you merge.
Aborting
First it does not work because we have not committed.
This is helpful beacause it prevents us from losing any work.
git status
On branch fun_fact
Your branch is behind 'origin/fun_fact' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: about.md
no changes added to commit (use "git add" and/or "git commit -a")
we can add and commit at the same time using the -a
option from the `git commit``
git commit -a -m 'second fun fact'
[fun_fact b378bd1] second fun fact
1 file changed, 1 insertion(+)
Now we try to pull again
git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
Now it cannot work because the branches have diverged. This illustrates the fact that our two versions of the branch fun_fact
and origin/fun_fact
are two separate things.
the branches have diverged means that they do not agree and that they each have at least one commit that is different from the other.
git log
commit b378bd148e53dfa7195c58123362e40ae12ef3e7 (HEAD -> fun_fact)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:26:20 2023 -0400
second fun fact
commit 6d4dbd33860fceb9c87bd3c4509deff8cecb3f45
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 13:06:54 2023 -0400
add fun fact
commit 5c8aaa9f2a129d551b8cb2cb294676f63c4af410 (origin/main, origin/HEAD, main)
Merge: caeacb5 65e9e39
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:47:08 2023 -0400
Merge pull request #5 from introcompsys/add-name
closes #2
commit 65e9e39935be8400ef12cc9003592f12244b50da (origin/add-name)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Tue Sep 19 12:46:00 2023 -0400
closes #2
commit caeacb503cf4776f075b848f0faff535671f2887
Merge: 6a12db0 693a2b5
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Thu Sep 14 13:38:54 2023 -0400
git gave us some options, we will use rebase which will apply our local commits after the remote commits.
git pull --rebase
Auto-merging about.md
CONFLICT (content): Merge conflict in about.md
error: could not apply b378bd1... second fun fact
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply b378bd1... second fun fact
Now it tells us there is a conflict.
nano about.md
We see that git marked up the conflict for us by editing the file:
Sarah Brown
2027
- i skied competitively in high school
<<<<<<<<< HEAD
- i started at URI in 2020
=======
- I went to Northeastern
>>>>>>>>> "add fun fact"
We manually edit it to be what we want it to be. We can take one change the other or both. We will choose both, so my file looks like this in the end.
cat about.md
Sarah Brown
2027
- i skied competitively in high school
- i started at URI in 2020
- I went to Northeastern
Now, we do git add, because above it said that was the next step.
git add about.md
and then this was the next thing after that.
Warning
If you got somethign very different at this point, run git --version
mine is 2.41. If yours is different, let me know as an issue.
git rebase --continue
[detached HEAD 756c487] second fun fact
1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/fun_fact.
Once we rebase and everything is done, we can push.
git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/introcompsys/github-inclass-fa23-brownsarahm.git
768dec8..756c487 fun_fact -> fun_fact
4.6. Prepare for Next Class#
Read the notes from 2023-09-19 carefully
Examine an open source project on GitHub. Answer the reflection questions below in
software.md
in your kwl repo on a branch that is linked to this issue. You do not need to make the PR, we will work with this in class on 2023-09-21.
## Software Reflection
1. Link and title of the project
1. What types of files are there that are not code?
1. What different types of code files are in the project? Do they serve different goals?
1. Is it all in one language or are there multiple languages?
1. Are there files that are configurations or settings?
1. Is there help for developers? users? which support is better?
1. What type of things are in the hidden files? who would need to see those files vs not?
Some open source projects if you do not have one in mind:
4.7. Review today’s class#
Create a merge conflict in your github in class repo and resolve it using your favorite IDE,. Describe how you created it, show the files, and describe how your IDE helps or does not help in ide_merge_conflict.md. Give advice for when you think someone should resolve a merge conflict manually vs using an IDE. (if you do not regulary use an, IDE, try VSCode)
Read more details about git branches(you can also use other resources) add
branches.md
to your KWL repo and describe how branches work, in your own words. Include one question you have about branches or one scenario you think they could help you with.
4.8. More Practice#
Create a merge conflict in your github in class repo and resolve it using your favorite IDE, then create one and resolve it on GitHub in browser(this requires the merge conflict to occur on a PR). Describe how you created it, show the files, and describe how your IDE helps or does not help in ide_merge_conflict.md. Give advice for when you think someone should resolve a merge conflict in GitHub vs using an IDE. (if you do not regulary use an, IDE, try VSCode)
Learn about GitHub forks and more about git branches(you can also use other resources)
add
branches-forks.md
to your KWL repo and describe how branches work, what a fork is, and how they relate to one another. If you use other resources, include them in your file as markdown links.
4.9. Experience Report Evidence#
Note
this is for if you miss class and need to make it up
run git log >> inclass2023-09-19.md
and then copy that new file into your KWL repo on the branch for your experience report
4.10. Questions After Today’s Class#
4.10.1. How would you fix a merge conflict involving other people trying to commit on other systems?#
The same way we did in class.
4.10.2. are there situations where if you accidently make another branch, that you can merge it can into the one you wanted to be working on?#
yes, exactly!
4.10.3. could you fix a merge conflict in github rather than in the local terminal in some cases?#
Yes, when the merge conflict arises as a result of a pull request and it is simple you can resolve it in github.com
4.10.4. What exactly does the rebase command do?#
I described it in more detail above with links to git docs.
4.10.5. What point does using git locally give?#
it’s the only way to keep your work in git and also run code locally
4.10.6. Why were some of my command prompts different from yours?#
Not sure, but it might be the version of git.
4.10.7. What website do you find the most useful to resolve issues relating to git within the terminal?#
I tend to rely on using git status
to see where it is at, thinking about where I want to get and then searching and using eg stackoverflow for things I do not know how to do.
that said git gud can help you visualize and we will have some other visualizations soon.
4.10.8. Would it be reasonable to never use the local Terminal and just always use github?#
No, that means you would not be able to run your code to work. Even if you work on most clous services, you still need to use the “local” terminal, on that cloud instance.