How can I fix things in git?
Contents
8. How can I fix things in git?#
8.1. Moving a Commit#
Start on your main branch of your github in class repo with a clean working directory
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Now let’s create a new file
touch new_feature.md
and add commit and push it to the main branch
git add .
git comit -m "new feature"
git commit -m "new feature"
[main 0830c39] new feature
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 new_feature.md
git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 266 bytes | 266.00 KiB/s, done.
Total 2 (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
3c9980a..0830c39 main -> main
We can look in GitHub:
gh repo view
introcompsys/github-inclass-brownsarahm
github-inclass-brownsarahm created by GitHub Classroom
github-inclass-brownsarahm
github-inclass-brownsarahm created by GitHub Classroom
View this repository on GitHub: https://github.com/introcompsys/github-inclass-brownsarahm
This command prints the README with a link to the repo online.
We had wanted to make a new feature but we realize that our work is on the main branch, not a separate feature branch.
Fortunately, we can fix it!
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
We can reset the the staging area and undo the last commit (remove the commit itself, but not the changes) to the last state that the staging area had with
git reset HEAD^
we can see what this did with
git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_feature.md
nothing added to commit but untracked files present (use "git add" to track)
Our file is still there, it is now not in the git commit history and the changes are back in the staging area.
Now we can switch branches
git checkout -b new_feature
Switched to a new branch 'new_feature'
and confirm
git status
On branch new_feature
Untracked files:
(use "git add <file>..." to include in what will be committed)
new_feature.md
nothing added to commit but untracked files present (use "git add" to track)
and then commit to the new branch.
git add .
git commit -m ' new feature ready for PR'
[new_feature a399978] new feature ready for PR
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 new_feature.md
Remember branches are pointers, so what we did was remove the commit (but not the snapshot) from a commit to the main branch (with info about main in it) to a new branch. This leave the main branch pointer pointed at the previous commit and moves only the new branch to the new commit. We get a new hash because the time and commit message are different, but the content is the same.
git push
fatal: The current branch new_feature has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin new_feature
git push --set-upstream origin new_feature
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 278 bytes | 278.00 KiB/s, done.
Total 2 (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 'new_feature' on GitHub by visiting:
remote: https://github.com/introcompsys/github-inclass-brownsarahm/pull/new/new_feature
remote:
To https://github.com/introcompsys/github-inclass-brownsarahm.git
* [new branch] new_feature -> new_feature
branch 'new_feature' set up to track 'origin/new_feature'.
If we go back to main
git checkout main
Switched to branch 'main'
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Our changes are not here locally, but they still are on GitHub
ls
README.md about.md
We can try to push our local to GitHub
git push
To https://github.com/introcompsys/github-inclass-brownsarahm.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/introcompsys/github-inclass-brownsarahm.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
but it does not work because this is will result in losing information, git (and GitHub) will not let you lose information by accident.
the -f
option stands for force and makes it do things that we otherwise might not want to/
git push -f
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/introcompsys/github-inclass-brownsarahm.git
+ 0830c39...3c9980a main -> main (forced update)
git log
commit 3c9980a4883782386ea3112d521b1b95997b0be6 (HEAD -> main, origin/main, origin/HEAD)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 17:40:01 2022 -0400
create about
commit ec3dd020d2767e45b6cd61f7c4ea5f6f2be9a3d8
Merge: 1613072 db2e41d
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 16:56:34 2022 -0400
Merge pull request #4 from introcompsys/create_readme
Create README.md
commit db2e41d48129b2c3ad09b78f1c14c2cd295e3eb2 (origin/create_readme)
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 16:55:23 2022 -0400
Create README.md
commit 1613072525c141b13d3ac7db68e8c1dbee70496b
Author: github-classroom[bot] <66690702+github-classroom[bot]@users.noreply.github.com>
Date: Wed Sep 14 20:51:29 2022 +0000
Initial commit
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
8.2. merging#
We can merge offline too
git merge new_feature main
Updating 3c9980a..a399978
Fast-forward
new_feature.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 new_feature.md
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
8.3. Reverting changes#
We can undo changes by picking out a specific commit by its name. First elts make two changes and commit eahc.
echo "change 1" >> new_feature.md
git add .
git commit -m 'change 1'
[main 83690bc] change 1
1 file changed, 1 insertion(+)
echo "change 2" >> new_feature.md
git add .
git commit -m 'change 2'
[main 948eda1] change 2
1 file changed, 1 insertion(+)
SO we hav e a file with two chagnes
cat new_feature.md
change 1
change 2
We need to know the hash of the commit that we want to undo.
git log
commit 948eda10a2b03a168e9d0291a294a4922afecda5 (HEAD -> main)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 17:08:49 2022 -0400
change 2
commit 83690bccd9be5da7c3a816dea4f14d6836674623
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 17:08:32 2022 -0400
change 1
commit a399978f0f20e1b560fbaf59ec831ab42dd6a82a (origin/new_feature, new_feature)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 16:55:41 2022 -0400
new feature ready for PR
commit 3c9980a4883782386ea3112d521b1b95997b0be6 (origin/main, origin/HEAD)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 17:40:01 2022 -0400
create about
commit ec3dd020d2767e45b6cd61f7c4ea5f6f2be9a3d8
Merge: 1613072 db2e41d
Author: Sarah Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 16:56:34 2022 -0400
We use git revert
git revert 83690bc
Auto-merging new_feature.md
CONFLICT (content): Merge conflict in new_feature.md
error: could not revert 83690bc... change 1
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
In this case, we get a merge conflict becaues the file was so simple before, git cannot be sure how to undo that small change.
cat new_feature.md
<<<<<<< HEAD
change 1
change 2
=======
>>>>>>> parent of 83690bc (change 1)
nano new_feature.md
we can use nano to resolve the merge conflict. and then check
git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
You are currently reverting commit 83690bc.
(fix conflicts and run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: new_feature.md
no changes added to commit (use "git add" and/or "git commit -a")
we have to add and commit to finish
git add .
git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
You are currently reverting commit 83690bc.
(all conflicts fixed: run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: new_feature.md
git commit -m "revert chang 1"
[main c1807f4] revert chang 1
1 file changed, 1 insertion(+), 1 deletion(-)
git status
and then we are all set.
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
We can see git revert undoes the changes but *adds an additional comit, it is keeping track of every change and by default the commit history only moves forward.
git log
commit c1807f4c764bfc47ad6cdea81453ae4c75b4df20 (HEAD -> main)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 17:16:59 2022 -0400
revert chang 1
commit 948eda10a2b03a168e9d0291a294a4922afecda5
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 17:08:49 2022 -0400
change 2
commit 83690bccd9be5da7c3a816dea4f14d6836674623
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 17:08:32 2022 -0400
change 1
commit a399978f0f20e1b560fbaf59ec831ab42dd6a82a (origin/new_feature, new_feature)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Mon Oct 3 16:55:41 2022 -0400
new feature ready for PR
commit 3c9980a4883782386ea3112d521b1b95997b0be6 (origin/main, origin/HEAD)
Author: Sarah M Brown <brownsarahm@uri.edu>
Date: Wed Sep 14 17:40:01 2022 -0400
create about
commit ec3dd020d2767e45b6cd61f7c4ea5f6f2be9a3d8
Merge: 1613072 db2e41d
8.4. Installing from source#
Store this outside of your inclass repo
cd ..
ls
2022-09-19 github-inclass-brownsarahm
git clone https://github.com/introcompsys/courseutils.git
Cloning into 'courseutils'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 57 (delta 26), reused 44 (delta 18), pack-reused 0
Receiving objects: 100% (57/57), 9.99 KiB | 3.33 MiB/s, done.
Resolving deltas: 100% (26/26), done.
cd courseutils/
then you can install
Important
on Windows this will probably need to run in either command prompt or Anaconda Prompt if you have that.
pip install .
Processing /Users/brownsarahm/Documents/inclass/systems/courseutils
Preparing metadata (setup.py) ... done
Requirement already satisfied: Click in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from syscourseutils==0.1.0) (7.1.2)
Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from syscourseutils==0.1.0) (1.4.4)
Requirement already satisfied: lxml in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from syscourseutils==0.1.0) (4.6.3)
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from syscourseutils==0.1.0) (1.23.2)
Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pandas->syscourseutils==0.1.0) (2020.1)
Requirement already satisfied: python-dateutil>=2.8.1 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pandas->syscourseutils==0.1.0) (2.8.2)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from python-dateutil>=2.8.1->pandas->syscourseutils==0.1.0) (1.15.0)
Building wheels for collected packages: syscourseutils
Building wheel for syscourseutils (setup.py) ... done
Created wheel for syscourseutils: filename=syscourseutils-0.1.0-py3-none-any.whl size=3890 sha256=32b131a267661070bdd7cd1d858eb4921283c53f5861cf98ff1464ef878086c6
Stored in directory: /private/var/folders/8g/px8bm7bj0_j31j71yh6mfd_r0000gn/T/pip-ephem-wheel-cache-d08bf6rq/wheels/b2/4a/cc/b21f40ef23301a77d7427d6c6766b0ed01411098736a71e5c5
Successfully built syscourseutils
Installing collected packages: syscourseutils
Attempting uninstall: syscourseutils
Found existing installation: syscourseutils 0.1.0
Uninstalling syscourseutils-0.1.0:
Successfully uninstalled syscourseutils-0.1.0
Successfully installed syscourseutils-0.1.0
Important
The package is updated so for the following to work use
cd courseutils
git pull
pip install .
sysgetassignment --date 2022-09-28
- [ ] Try exploring your a repo manually and bring more questions
- [ ] Make sure that you have a grading contract that has been reviewed at least once
Then we can pipe the output into another command to create an issue.
sysgetassignment | gh issue create --title "more practice 1" --body-file -
Creating issue in introcompsys/github-inclass-brownsarahm
https://github.com/introcompsys/github-inclass-brownsarahm/issues/8
8.4.1. Trouble shooting#
If anaconda or miniconda is installed: conda activate
. Otherwise activate a python environment of your choosing
Try pulling updates and reinstalling.
Make sure you reactive bash.
try both pip
and pip3
if you want to make yourself a linkux computer for the cost of a flash drive
8.5. Review today’s class#
Fix any open PRs you have that need to have a commit moved to a different branch, etc.
In your github in class repo, create a series of commits that tell as story of how you might have made a mistake and fixed it. Use git log and redirects to write that log to a file in your KWL repo and then annotate your story in
gitstory.md
.Create tracking issues for last week’s activities and link them to PRs for any activities you have already completed.
8.6. Prepare for Next Class#
In a
gitunderstanding.md
list 3-5 items from the following categories (1) things you have had trouble with in git in the past and how they relate to your new understanding (b) things that your understanding has changed based on today’s class © things about git you still have questions aboutFollow up on your grading contract as needed
8.7. More Practice#
Create an issue on your group repo for a new vocab term or cheatsheet item from your terminal, check that there is not an exisiting issue first. Write the history from this activity to
offlineissue.md
in your kwl repo.If you plan to do projects. Create milestones for the intermediate deadlines (proposal 1, proposal 2, project 1, project 2 with deadlines)
8.8. Questions After Class#
8.8.1. Will the use of the courseutils and commands be necessary for the class? or would it just make the class easier?#
They’re for tracking your progress. We will help you get it working or work around it.
8.8.2. My kwlfilecheck command is still not working how do I fix that?#
There’s a missing section on the course website for that command, focus on getting sysgetassignment
for now. Try the things above and if it still does not create a detailed issue with what error you get and what OS you are using on the repo
8.8.3. Can you post directions for how to get the kwl tracking to work on m1 macs#
8.8.4. Does a flash drive that I have made to have ubuntu in 2017 works?#
yes