6. What if I edit the file in two places?#

One of the biggest advantages of git is that it is distributed. This gives us great flexibility, but also allows us to make things complicated.

Today we will see how to fix this.

6.1. Get set up#

We left off in the middle of some work on tuesday

git status
On branch organization
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md
	deleted:    _config.yml
	deleted:    overview.md
	deleted:    test_alt.py
	deleted:    test_help.py
	deleted:    test_imp.py
	deleted:    tests_abc.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	docs/
	tests/

no changes added to commit (use "git add" and/or "git commit -a")

git thinks we deleted files because we moved them into an untracked folder.

I’ll add just the one folder

git add docs/
git status
On branch organization
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   docs/_config.yml
	new file:   docs/_toc.yml
	new file:   docs/overview.md

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md
	deleted:    _config.yml
	deleted:    overview.md
	deleted:    test_alt.py
	deleted:    test_help.py
	deleted:    test_imp.py
	deleted:    tests_abc.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	tests/

Now it thinks I deleted and created new files Now if we dd all

git add .
git status
On branch organization
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md
	renamed:    _config.yml -> docs/_config.yml
	renamed:    overview.md -> docs/_toc.yml
	renamed:    test_alt.py -> docs/overview.md
	renamed:    test_help.py -> tests/test_abc.py
	renamed:    test_imp.py -> tests/test_alt.py
	renamed:    tests_abc.py -> tests/test_help.py
	new file:   tests/test_imp.py

It sees that we moved files. Sine these were empty, it cannot match them all up very well.

Now we can commit

git commit -m 'start organizing'
[organization 812245d] start organizing
 8 files changed, 19 insertions(+)
 rename _config.yml => docs/_config.yml (100%)
 rename overview.md => docs/_toc.yml (100%)
 rename test_alt.py => docs/overview.md (100%)
 rename test_help.py => tests/test_abc.py (100%)
 rename test_imp.py => tests/test_alt.py (100%)
 rename tests_abc.py => tests/test_help.py (100%)
 create mode 100644 tests/test_imp.py

and confirm we’re all set

git status
On branch organization
nothing to commit, working tree clean

then push

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

    git push --set-upstream origin organization

we have to create the upstream.

git push --set-upstream origin organization
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 1.42 KiB | 1.42 MiB/s, done.
Total 9 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
remote: 
remote: Create a pull request for 'organization' on GitHub by visiting:
remote:      https://github.com/introcompsys/github-in-class-brownsarahm-1/pull/new/organization
remote: 
To https://github.com/introcompsys/github-in-class-brownsarahm-1.git
 * [new branch]      organization -> organization
branch 'organization' set up to track 'origin/organization'.

Then we’ll make a PR for that branch, but leave it open.

6.2. Editing main when a PR is open.#

First we switch to main

git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

and make sure it is up to date

git pull
Already up to date.

Next we will edit the README to add somethign to the first line

nano README.md 

on the main branch it starts like :

# GitHub Practice 

Name: Sarah Brown

and we edit to:

# GitHub Practice in class 

Name: Sarah Brown

Then we checkin

git status
On branch main
Your branch is up to date with 'origin/main'.

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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

add and commit

git add .
git commit -m 'update title'
[main f2844b2] update title
 1 file changed, 1 insertion(+), 1 deletion(-)

and push to GitHub

git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 356 bytes | 356.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/introcompsys/github-in-class-brownsarahm-1.git
   4b89dff..f2844b2  main -> main

When we check in our PR, we are still all set. git has checked the changes mad and they are consistent or do not conflict. This means that git knows how to merge this branch without any help from a person to tell it what is important.

6.3. Making a conflict#

nano README.md 

again we will edit the same file, but this time we will edit in a way that conflicts with how we changed it on the organization branch.

We will add text on line 4.

# GitHub Practice in class 

Name: Sarah Brown
major: EE

and check with git

git status
On branch main
Your branch is up to date with 'origin/main'.

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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

and add commit, and push

git add .
git commit -m 'add major'
[main cffcf05] add major
 1 file changed, 1 insertion(+)
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), 366 bytes | 366.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/introcompsys/github-in-class-brownsarahm-1.git
   f2844b2..cffcf05  main -> main

If we look on the organization branch

git checkout organization
Switched to branch 'organization'
Your branch is up to date with 'origin/organization'.
cat README.md 
# GitHub Practice

Name: Sarah Brown
age=35
|file | contents |
> | ------| ------- |
> | abstract_base_class.py | core abstract classes for the project |
> | helper_functions.py | utitly funtions that are called by many classes |
> | important_classes.py | classes that inherit from the abc |
> | alternative_classes.py | classes that inherit from the abc |
> | LICENSE.md | the info on how the code can be reused|
> | CONTRIBUTING.md | instructions for how people can contribute to the project|
> | setup.py | file with function with instructions for pip |
> | tests_abc.py | tests for constructors and methods in abstract_base_class.py|
> | tests_helpers.py | tests for constructors and methods in helper_functions.py|
> | tests_imp.py | tests for constructors and methods in important_classes.py|
> | tests_alt.py | tests for constructors and methods in alternative_classes.py|
> | API.md | jupyterbook file to generate api documentation |
> | _config.yml | jupyterbook config for documentation |
> | _toc.yml | jupyter book toc file for documentation |
> | philosophy.md | overview of how the code is organized for docs |
> | example.md | myst notebook example of using the code |
> | scratch.ipynb | jupyter notebook from dev |

we see the file is different.

6.4. Resolving Merge Conflicts on GitHub#

We can see it on the PR now and GitHub allows us to resolve it in the browser.

GitHub docs are the best reference on what we did here.

we resolved the issue in the browser then merged the conflict.

6.5. Making and resolving a conflict locally#

We will go back to main and get up to date

git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
git pull
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 751 bytes | 187.00 KiB/s, done.
From https://github.com/introcompsys/github-in-class-brownsarahm-1
   812245d..56f29ca  organization -> origin/organization
Already up to date.

Now we will edit the about file one way on our local copy and a different way on GitHub without pulling or pushing

nano about.md 

Once the two versions are different, we pull the contents.

git  pull
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.23 KiB | 314.00 KiB/s, done.
From https://github.com/introcompsys/github-in-class-brownsarahm-1
   cffcf05..8e2fe11  main       -> origin/main
Updating cffcf05..8e2fe11
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

… and it fails. We need to commit our local changes first or git cannot even do its work to compare.

It will not overwrite our working directory with un committed changes.

git add .
git commit -m 'add jacket'
[main 9cb0323] add jacket
 1 file changed, 2 insertions(+)
git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 5 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

Now it tells us w have a conflict, or the two branches that are suppose to be the same have diverged.

When we do what it says now,

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.

It still fails, but we have to tell it how to try to merge them.

git pull --rebase
Auto-merging about.md
CONFLICT (content): Merge conflict in about.md
error: could not apply 9cb0323... add jacket
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 9cb0323... add jacket

this time we edit the same way, but locally.

nano about.md 
git status
interactive rebase in progress; onto 8e2fe11
Last command done (1 command done):
   pick 9cb0323 add jacket
No commands remaining.
You are currently rebasing branch 'main' on '8e2fe11'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
	both modified:   about.md

no changes added to commit (use "git add" and/or "git commit -a")

then do as git says

git rebase --continue
about.md: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add

we have to add first

git add about.md 
git rebase --continue
[detached HEAD 6a2e1cc] add jacket
 1 file changed, 3 insertions(+)
Successfully rebased and updated refs/heads/main.
git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

6.6. Review today’s class#

  1. create gitadvice.md and write tips for how often to commit and how to avoid merge conflicts. Include at least 3 tips.

  2. create an issue on your group repo for a tip or cheatsheet item you want to contribute. Make sure that your contribution does not overlap with one that amemb

  3. clone your group repo.

  4. work offline and add your contribution and then open a PR

  5. review a class mate’s PR.

6.7. Prepare for Next Class#

  1. Bring questions about git to class on Wednesday.

  2. Make sure that the gh CLI tool works by using it to create an issue called test on your kwl repo with gh issue create

  3. Read sections 1.1,1.2, and 1.3 of the pro git book this is mostly review at this point, but we are going to go into more of how git works next, so you need to make sure these concepts are all sorted out. Comment on your prepare issue if reading helped clarify confusion, made you more confused, or gave you new understanding. Either explain what you learned or ask a question you have. Tag @brownsarahm on the issue.

6.8. More Practice#

  1. 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. 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)

  2. create an issue on your group repo for a resource you want to review. Make sure that your contribution does not overlap with one that another member is going to post.

  3. clone your group repo.

  4. work offline and add your contribution and then open a PR. Your reivew should help a classmate decide if that reference material will help them or not.

  5. review a class mate’s PR.

6.9. Experience Report Evidence#

link to your github inclass repo’s commit history.

6.10. Questions After Today’s Class#

6.10.1. Is there a way to configure git to update automatically or a command we can run to update it?#

Manual, unfortunately.

6.10.2. Whenever a merge conflict occurs will there always be some indicator that it occured and instructions on how to fix it on git or github?#

Yes! no guessing.

6.10.3. #

We will see this soon.

6.10.4. If there are multiple merge conflicts, does the terminal iterate through each conflict once the prior one is fixed?#

It tells you the first one’s file and you have to find them all within the file.

6.10.5. how to get out of VIM when you accidently enter git commit without -m#

escape, :wq, enter/return

if the escape button does not work, sometimes you have pressed another key that puts it in a different mode, then you can use command/control + c and then :wq+enter.

As a last resort, you can close the terminal window and open a new one. Everything important that git does edits a file on your computer, git does not use a lot of memory that you can lose by quitting. If you quit, your staging area will stay exactly how you left it. You can then navigate back to that directory and start your commit again with -m.

6.10.6. what does git rebase actually do?#

the git docs describe this well

6.10.7. What’s the importance of merging?#

It’s the way to combine two branches’ of work

6.10.8. Questions we will answer later#

  • Is there any way to reverse what you did just like how you reverse your commits in before your staging area?

  • How does git track all of the information between commits and merges and branches?

6.10.9. What if reolving a merge conflicts causes other issues?#

By definition, it cannot.

6.10.10. Best way to prevent different/colliding branches / issues with local git and github not being the same?#

This is an exercise for the review badge, so I am declining to answer here, but we will engage with you on your badges.

6.10.11. when you rename a file, how does git know that you renamed it (how does it know it’s the same file even though it has a new name)?#

It matches the content of the files.

6.10.12. How do we fix accidental merges?#

The goal is to not have to, that’s why GitHub makes you click twice to merge a pull request. It is possible though. It’s on the FAQ: https://introcompsys.github.io/spring2023/faq/github.html#help-i-accidentally-merged-the-feedback-pull-request-before-my-assignment-was-graded. This example is taken from my Data Science class, where they commit to main and I grade a PR from main into Feedback. If they accidentally merge, they need to leave their main branch alone and hard reset their Feedback branch. In other cases you may want to reset the main branch or a develop branch. The procedure is the same, only the branch names change.

6.10.13. the prepare work before class and the work we do in class are for the same grade?#

Yes, the prepare for class is a part of the experience badge for the next class after it is posted along with in class. This is because the prepare for class work is due before the next class. Increasingly, class sessions will rely on you having completed those items.