Recent

Author Topic: General thread about GIT  (Read 2767 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11801
  • Debugger - SynEdit - and more
    • wiki
Re: General thread about GIT
« Reply #15 on: September 11, 2025, 09:25:45 am »
I need to get the latest version of a source from a git repository, abandoning all my changes.
Which command shall I use?

Assuming the latest code is in branch "main", and your remote is called "origin" (as is by default)
Code: Bash  [Select][+][-]
  1. git reset --hard main origin/main
  2. git pull

or
Code: Bash  [Select][+][-]
  1. git remote update
  2. git reset --hard main origin/main
  3.  

The first can still fail, if an untracked file exists, and the update wants to create it.

To get rid of untracked files

git clean -fdx

(the x forces to ignore .gitignore)

CM630

  • Hero Member
  • *****
  • Posts: 1523
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: General thread about GIT
« Reply #16 on: September 11, 2025, 12:37:18 pm »
Code: Pascal  [Select][+][-]
  1. >git reset --hard main origin/main
  2. fatal: Cannot do hard reset with paths.

Then I tried
Code: Pascal  [Select][+][-]
  1. git reset --hard
  2. git pull
and it seems to work.
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11801
  • Debugger - SynEdit - and more
    • wiki
Re: General thread about GIT
« Reply #17 on: September 11, 2025, 01:40:40 pm »
Sorry, been a while since I done that.

You have to be checked out to branch main.

Code: Bash  [Select][+][-]
  1. git reset --hard
works, if your local branch has not diverged.
I.e. reverts changes in the worktree. Undoes any "git add".

IIRC Keeps you at the current commit, so if that diverged (if you have local commits that you need to drop) then "git pull" still will not be able to fast forward (but you could rebase)

Code: Bash  [Select][+][-]
  1. git reset --hard origin/main
Will also reset your local branch main to the commit "origin/main" (from which you will be able to fast forward to any newly pulled commits on main).

This drops the local commits, if you have any on main.



If you are not on main (works too, if you are on main), you can use "git switch"
Code: Bash  [Select][+][-]
  1. git switch -f -C main origin/main
(and then git clean -fdx if you want to clean up)

The uppercase C is a forced action, and sort of includes a reset.
The -f will make sure it will ignore any other showstoppers

- Create a branch main at origin/main
- overwrite the old branch (reset the old branch to the new location if there was an old)
- checkout (switch) that new branch (update the files in the worktree)

The -f is only needed in rare cases, where git switch would complain. I can't recall what it forces on top of the -C.
(Maybe something like if a file exists, that needs to be created by the checkout)

 

TinyPortal © 2005-2018