nus-cs2113-AY2223S2 / forum

10 stars 0 forks source link

Trouble pulling and pushing commit for level-0 #6

Closed choongzhanhong closed 1 year ago

choongzhanhong commented 1 year ago

Hi, I am trying to commit my level-0 change for iP, however I am receiving the following problems when pushing and pulling. Does anyone know what I can do in this situation? I Googled but don't quite know how to apply the solutions online.

Pull:

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks fetch --no-tags origin

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks pull origin master
From https://github.com/choongzhanhong/ip
 * branch            master     -> FETCH_HEAD

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.

and Push error message:

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v --tags origin master:master
Pushing to https://github.com/choongzhanhong/ip.git
To https://github.com/choongzhanhong/ip.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/choongzhanhong/ip.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.

Completed with errors, see above.
joulev commented 1 year ago

When running git log locally it will show the history of all commits, could you paste the recent commits here so we know how your local history is like? Also could you paste the output of git status as well?

choongzhanhong commented 1 year ago

running git log I have a log of a commit I made:

$ git log
commit 1ee7646a757d98ec4302d3d39b000e64d849d87b (HEAD -> master)
Author: choongzhanhong <choongorilla@gmail.com>
Date:   Sun Jan 15 18:37:35 2023 +0800

    .gitignore: add *.class
    Duke.java: add level-0 greet and exit message

before that, it is

commit 3b83789e9ed75b9498c05ee150c073f6492ec6ff
Author: Sean Leong <70213029+seanleong339@users.noreply.github.com>
Date:   Thu Dec 29 13:56:26 2022 +0800

    .gitignore: Fix ACTUAL.txt -> ACTUAL.TXT  (#73)

and so on.

git status shows:

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean
joulev commented 1 year ago

Alright, from the output of git status, your local branch has diverged from the remote (GitHub) branch. Your tree currently looks like this

old commits ─── previous commit ─── current commit (remote)
                      │
                      └──────────── current commit (local)

I also went to see your repository; your remote repository latest commit hash is 1f347c886abfbee97a1037d425de8f57b583054e while your local repository latest commit hash is 1ee7646a757d98ec4302d3d39b000e64d849d87b, confirming the above.

Now what to do? Normally people would probably merge the upstream to their local repo. But in this case, I don't think a merge is necessary (we haven't even studied about it yet). Instead, you probably want to force push/pull to discard one version and keep the other.

To discard the remote version (your remote commit will be gone), use git push --force.

To discard the local version (your local commit will be gone), use git reset --hard origin/master.

After these commands, the local repo will be synced to the remote repo, and you can then git push or git pull normally.

Please note that --force and --hard are dangerous and generally discouraged. Especially git push --force, don’t use it for team (>1 person) projects. However, they can be used for cases like the one described here. Please remember to read their documentation as well.

choongzhanhong commented 1 year ago

Thank you! I have force pushed the commit from my local end. I'm not sure what caused the discrepancy, but for those who are using Sourcetree like me, there is a button on top-right to input terminal commands simply labeled terminal. I just used git push --force and it was successful.