2

I use(just use not develop) some other people's git repos, but I don't want the whole history.

It seemed that 'git clone --depth 1' meets my requirements.

But

1) how can I trim the existing repos like I do a 'git clone --depth 1' ?

2) how can I update the repo without history, so the repo only remain the last commit?

Assume the reop's history is A-B-C, I execute 'git clone --depth 1', so I get a repo of C. Then the upstream update the repo, the history become A-B-C-D-E. Can I do some simple operation, so I get a repo of E? Delete the repo and execute 'git clone --depth 1' again?

I want this, because some repo's history is too big, the .git maybe 100M+, and my internet access is not so stable, so I want the last update only.

Solution

1) make a existing repo shallow:

git rev-parse HEAD > .git/shallow
git reflog expire --all --expire=now
git gc --aggressive --prune=now

2) update a shallow repo:

git fetch origin master --depth=1
git reset --hard origin/master

after upate several times, you can:

git reflog expire --all --expire=now
git gc --aggressive --prune=now

to remove old commits and files again.

Referece

How to update a git shallow clone?

Reduce git repository size

1
  • why would you need it? can you give me some context? Commented Feb 7, 2020 at 20:41

1 Answer 1

1

So you have a git repository locally that you have cloned. You want to get just the most recent version of the master branch.

git pull origin master --depth=1

This retrieves the latest 1 commit in the master branch and merges it into your local repository. If you have made any changes to your local version this may introduce merge conflicts that you have to deal with, but OP indicates that's not the case for them.

Sign up to request clarification or add additional context in comments.

1 Comment

On a shallow repo, "git pull origin master --depth=1" will failed(refused to merge), but "git fetch origin master --depth=1" can work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.