How should I use git to stay organized this class?
Contents
5. How should I use git to stay organized this class?#
Warning
this is currently only the raw output from the shell session. Annotation will be added tomorrow
5.1. How do I work with branches?#
Let’s go back to the github iclass repo.
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
modified: about.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
CONTRIBUTING.md
LICENSE.md
docs/
mymodule/
overview.md
setup.py
tests/
no changes added to commit (use "git add" and/or "git commit -a")
I had not pushed my content after Monday, so I have alot of files that are both not staged and fully untracked.
We do not want to commit direcly to main in general. Working with a branch is better, becuase it gives us more option.
We can use the -b
option for git checkout to both create a new branch and switch to it.
git checkout -b organtzation
and git replies that it did what we asked.
Switched to a new branch 'organtzation'
git status
Note
Notice that this time in git status it does not compare to origin. this is because the new branch does not have a remote
On branch organtzation
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
modified: about.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
CONTRIBUTING.md
LICENSE.md
docs/
mymodule/
overview.md
setup.py
tests/
no changes added to commit (use "git add" and/or "git commit -a")
Now
git add .
we can commit right after staging, since we konw that is what we want.
git commit -m 'from inclass org acitivyt'
and it will go a bunch of things, beacuse we made a lot of changes.
[organtzation 8b62af8] from inclass org acitivyt
23 files changed, 69 insertions(+)
create mode 100644 CONTRIBUTING.md
create mode 100644 LICENSE.md
create mode 100644 docs/API.md
create mode 100644 docs/_config.yml
create mode 100644 docs/_toc.yml
create mode 100644 docs/about.md
create mode 100644 docs/example.md
create mode 100644 docs/overview.md
create mode 100644 docs/philosophy.md
create mode 100644 mymodule/__init__.py
create mode 100644 mymodule/abstract_base_class.py
create mode 100644 mymodule/alternative_classes.py
create mode 100644 mymodule/helper_functions.py
create mode 100644 mymodule/important_classes.py
create mode 100644 overview.md
create mode 100644 setup.py
create mode 100644 tests/test_alt.py
create mode 100644 tests/test_help.py
create mode 100644 tests/test_imp.py
create mode 100644 tests/tests_abc.py
create mode 100644 typescript
Once we commit it all, we want to get it to GitHub.
git push
fatal: The current branch organtzation has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin organtzation
and then we can follow what git said to do
git push --set-upstream origin organtzation
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% (8/8), 1.68 KiB | 1.68 MiB/s, done.
Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'organtzation' on GitHub by visiting:
remote: https://github.com/introcompsys/github-inclass-brownsarahm/pull/new/organtzation
remote:
To https://github.com/introcompsys/github-inclass-brownsarahm.git
* [new branch] organtzation -> organtzation
branch 'organtzation' set up to track 'origin/organtzation'.
5.2. What happens when we start a new branch on GitHub.#
Create a branch from an issue:
git fetch
this step dowloads the content that is on the new branch
From https://github.com/introcompsys/github-inclass-brownsarahm
* [new branch] 2-create-an-about-file -> origin/2-create-an-about-file
we use git satus to see what else it did
git status
On branch organtzation
Your branch is up to date with 'origin/organtzation'.
nothing to commit, working tree clean
and see that it did not change our local status, or location.
git branch
main
* organtzation
but we can see the new branch isn not even in our local repo, though it has been fetched. SO we should checkout
git checkout 2-create-an-about-file
branch '2-create-an-about-file' set up to track 'origin/2-create-an-about-file'.
Switched to a new branch '2-create-an-about-file'
this did a few things. It switched our local repference point, made our local directory match the remote branch, and it set the new branch to track the origin branch.
5.3. What if we edit the file in two places?#
First, we edit the file locally
echo "test" >> about.md
git status
On branch 2-create-an-about-file
Your branch is up to date with 'origin/2-create-an-about-file'.
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")
cat about.md
test
and add and commit the changes
git add .
git commit -m 'start about'
[2-create-an-about-file 30c4748] start about
1 file changed, 1 insertion(+)
Then edit the file in your browser.
Now we can try to push it from the local copy
git push
To https://github.com/introcompsys/github-inclass-brownsarahm.git
! [rejected] 2-create-an-about-file -> 2-create-an-about-file (fetch first)
error: failed to push some refs to 'https://github.com/introcompsys/github-inclass-brownsarahm.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
We can’t push because we ahve a merge conflict.
So first, we have to pull
git pull
Now it tells us more about the merge conflict.
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.
We can try the rebase option. We’ll talk more about how this works in detail later, but it basically tries to undo one set of commits, apply the others then apply the first set on top. If it cannot, then there’s still a conflict.
git pull --rebase
Auto-merging about.md
CONFLICT (content): Merge conflict in about.md
error: could not apply 30c4748... start about
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 30c4748... start about
And we cannot, so we have more to do.
git status
interactive rebase in progress; onto 8ce03aa
Last command done (1 command done):
pick 30c4748 start about
No commands remaining.
You are currently rebasing branch '2-create-an-about-file' on '8ce03aa'.
(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")
We will use nano to edit.
nano about.md
5.3.1. Resolving a Merge conflict#
>>>>>>> HEAD
test
========
I am a Professor
<<<<<<<< 30c4748
Notes:
HEAD is the current directory’s content
the other branch is indicated by its last commit number
To fix it, we manually edit the file for it to be what we want
test
I am a Professor
Then we can finish resoling
git status
interactive rebase in progress; onto 8ce03aa
Last command done (1 command done):
pick 30c4748 start about
No commands remaining.
You are currently rebasing branch '2-create-an-about-file' on '8ce03aa'.
(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")
we add and commit:
git add .
git status
interactive rebase in progress; onto 8ce03aa
Last command done (1 command done):
pick 30c4748 start about
No commands remaining.
You are currently rebasing branch '2-create-an-about-file' on '8ce03aa'.
(all conflicts fixed: run "git rebase --continue")
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: about.md
git commit -m 'keep both changes'
[detached HEAD d2f136b] keep both changes
1 file changed, 2 insertions(+)
git status
interactive rebase in progress; onto 8ce03aa
Last command done (1 command done):
pick 30c4748 start about
No commands remaining.
You are currently editing a commit while rebasing branch '2-create-an-about-file' on '8ce03aa'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
but after that, it still says that it is in the interactive rebase mode, because there is one final step to do.
git rebase --continue
Successfully rebased and updated refs/heads/2-create-an-about-file.
and finally
git status
On branch 2-create-an-about-file
Your branch is ahead of 'origin/2-create-an-about-file' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
we see it is all set and ready to go.
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), 285 bytes | 285.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.
To https://github.com/introcompsys/github-inclass-brownsarahm.git
8ce03aa..d2f136b 2-create-an-about-file -> 2-create-an-about-file
and indeed the push works!
5.4. A note on echo#
echo "hello world"
hello world
echo "hello world" >> hello.md
5.5. Review today’s class#
Read today’s notes
Update your KWL chart with the new items and any learned items.
Update the title to any discussion threads you have created to be more descriptive
add
branches.md
to your KWL repo and describe how branches work and what things to watch out for in your own words.
5.6. Prepare for Next Class#
Note
This is what is required, before the next class and will be checked or if you don’t do it you will have trouble participating in class
Read through the Grading Contract README and sample contracts. Start drafting your contract. Bring questions to class on Monday.
Bring git questions or scenarios you want to be able to solve to class on Wednesday
Warning
the template contracts in the repo have an error in them; use the ones on the course website.
5.7. More Practice#
Try creating a merge conflict and resolving it using your favorite IDE.