Home > Software engineering >  Clear tail in git pull
Clear tail in git pull

Time:05-27

I know I can do a shallow clone using git clone --depth 1 <branch-name> <repository>.
I know I can pull new changes using git pull.

But is there a way to git pull while clearing the tail, so in the end I will have only one commit in the filesystem (as if I would do clone --depth 1 on the new version?

CodePudding user response:

git pull and git clone both implicitly do git fetch, which means they both have many of its arguments. The depth is one of them, so git pull --depth=1 should do it...

Except that only works if the histories overlap, otherwise git says this:

fatal: refusing to merge unrelated histories

This appears to work:

git fetch --depth=1 && git reset --hard FETCH_HEAD

CodePudding user response:

It would be better not to use git pull. It does nothing that you can't do far better with git fetch followed by some disposal command if needed.

Additionally in your case I would also question whether you need a local copy of this branch at all; if you just want to look at it and perhaps form a feature branch from it, just say git fetch --depth 1 and stop, and now git switch --detached origin/branchname and proceed from there.

That way, there is no local branchname preserving unwanted commits. (You'll want to say git branch -D branchname once to ensure this.)

  •  Tags:  
  • git
  • Related