2

resubmitting the same files to remote branch

enter image description here

I am working on a project of my university.
I have already submitted the corrupted files of project (degrees.py, small.csv ... etc) to remote branch 1, which is protected branch (could not use force push) of **GitHub repository, and now I want to resubmit the updated same files of project (degrees.py, small.csv ... etc) again to branch 1.

I am not able to resubmit the updated same files to my branch 1.

i am using git fetech and git checkout commands, I am trying to do

git init    
git remote add origing <origin> 
git fetch origin    

#create branch branch1 locally switch to branch1 and copy from origin/branch1
git checkout -b branch1  origin/branch1   
git add --all    
git commit -m "comments"  
git push

That is:

dir
__pycache__  degrees.py  large  small  util.py

Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees
$ git init
Initialized empty Git repository in C:/Users/Sbariz/Desktop/CS50ai_search/s_degrees/.git/

Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
git remote add origin https://github.com/me50/shjee1063.git

Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
git fetch origin**
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 29 (delta 1), reused 26 (delta 1), pack-reused 0
Unpacking objects: 100% (29/29), 21.60 MiB | 1.56 MiB/s, done.
From https://github.com/me50/shjee1063
 * [new branch]      ai50/projects/2020/x/degrees -> origin/ai50/projects/2020/x/degrees
 * [new branch]      ai50/projects/2020/x/tictactoe -> origin/ai50/projects/2020/x/tictactoe
 * [new branch]      main       -> origin/main

Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
$ git branch -a
  remotes/origin/ai50/projects/2020/x/degrees
  remotes/origin/ai50/projects/2020/x/tictactoe
  remotes/origin/main

Sbariz@Sbariz-PC MINGW64 ~/desktop/cs50ai_search/s_degrees (master)
$ git checkout -b ai50/projects/2020/x/degrees     origin/ai50/projects/2020/x/degrees**

error: The following untracked working tree files would be overwritten by checkout:       
 __pycache__/degrees.cpython-37.pyc
 __pycache__/util.cpython-37.pyc
        degrees.py
        large/movies.csv
        large/people.csv
        large/stars.csv
        small/movies.csv
        small/people.csv
        small/stars.csv
        util.py

Please move or remove them before you switch branches.
Aborting
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
sh agh
  • 21
  • 1
  • Your main problem shown here is that some commits specifically *do* contain files that other commits specifically (and correctly) *omit*. In order to check out the bad commit(s), you need to get the untracked working tree files out of the way so that the checkout-of / switch-to the bad commit can put the unwanted files in, after which you can remove them again and put the good (wanted) files back. Note how I keep mentioning *commits* here. – torek Jan 02 '22 at 16:29
  • The reason for talking about *commits* first, and *files* only later, is that Git stores, and pushes, *commits*, not files. The commits then contain files: each commit has a *full snapshot of every file*. Some of the commits in your repository contain *too many* files, such as the `*.pyc` files, and others correctly *don't* contain those files. – torek Jan 02 '22 at 16:30
  • It is literally impossible to change any existing commit. So those commits that do have the files (but shouldn't) are broken commits and will be broken commits forever because they cannot be fixed. You must allow them to continue to be broken commits. Use them carefully, knowing that they are broken! – torek Jan 02 '22 at 16:31
  • can i send new commit (containing same file) to same branch mean branch1. – sh agh Jan 02 '22 at 18:58
  • You can always make *new* commits. Each new commit has its own new snapshot of all files (plus its own metadata). Any *new* commit can be on any branch you like. You can also, under certain conditions, kick commits *off* some branch, though this gets a bit tricky. – torek Jan 02 '22 at 19:08

1 Answers1

0

You could simply stash first your modified files (git stash), then checkout your branch and restore your local modifications:

git stash
git checkout -b ai50/projects/2020/x/degrees     origin/ai50/projects/2020/x/degrees
git stash pop

Note: if you are using Git for Windows, you can:

That command has a guess mode:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git switch -c <branch> --track <remote>/<branch>

If this somehow remains an issue, try and have tow different working tree, from the same local cloned repository.

  • Your current cloned repository would remain on branch1
  • you make a second working tree (a second local folder witch a checkout of a different branch), using git worktree.

That way, you don't have to switch branch, just chose the right root folder, depending on the branch you want to work on (add/commit and push).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • git stash is not working as we can use it after committing. i have used it but error is: you have not commit anything yet. i am trying but not success – sh agh Jan 02 '22 at 17:14
  • @shagh Exactly, but if you have an error message when switching branch, git stash will still work in cleaning up your index, and allowing the branch switch to occur. – VonC Jan 02 '22 at 18:14
  • @shagh I have edited the answer with a possible alternative, to sidestep completely your checkout/switch branch issue. – VonC Jan 02 '22 at 18:17
  • @shagh And then yes, you can make a second submission to that branch 1. (And a third, and as many as you need). – VonC Jan 02 '22 at 18:52
  • let i am in master branch (local) for example c://desktop /search/ degrees(master) – sh agh Jan 02 '22 at 19:00
  • @shagh Sure: just modify your file, add, commit and push. If you need to do the same on a different branch, do so in a separate folder, after an initial `git worktree add ../b1 branch1`: that way, you don't have to checkout back and forth between two branches. And you can add/commit/push as many time as you want in each branch. – VonC Jan 02 '22 at 19:21
  • sorry it is not working. can you specify procedure step by step by seeing my above diagram. i have seen tutorial of merging and branching all these tutorial on when different files are commit on same branch. for example here branch1. it is looking more challenging for me. the scenario is that. i have commit some faulty file (degrees.py, util.csv...) on branch1 (protected branch). now i want my second submission of same file (degrees.py, util.csv, ....) on same branch1. how it is possible. – sh agh Jan 03 '22 at 14:59
  • @shagh Sure, but before that, what does "not working" does? What new error message did you get when following my initial answer? – VonC Jan 03 '22 at 17:09
  • i am very thankful for your cooperation – sh agh Jan 04 '22 at 14:11