Git clone and overwrite local repository

steakoverflow

I would like to have an up to date copy (periodically) of the bitbucket repo on my server, but not sure which git commands I have to issue to avoid potential merge conflicts that can happen with pulls.
The only clean way I can think of is to clone to a tmp dir then copy/overwrite local repo.
But there must be a better way to achieve this?

torek

Merge conflicts occur when—and only when—you do merge-like operations, such as:

  • rebasing your commits
  • merging your commits with someone else's, or vice versa
  • using git revert to revert commits, so as to make your own branches that differ from theirs

Note the common theme here: you must have your own commits, of your own work (or of reversions of specific parts of theirs, but again these are your commits that you are adding).

Thus, if you never make your own commits, you will never have any conflicts. Of course, this means you can never make any changes—or if you do make changes, you must keep them on your branches, always separate from their branches.

But that's exactly what Git does in the first place, as long as you avoid running git merge (and git rebase and so on).

The git pull command consists of a git fetch followed by a git merge. (Well, you can configure it, or tell it, to use git rebase instead of git merge. But you don't want either one.) So ... don't use git pull.

This is good advice for anyone new to Git: avoid git pull, because it does two things, and you want to do one thing, then ponder a bit about whether you want any second thing, much less git merge or git rebase as the second thing.

As in janos' answer, if you have made no commits of your own, but want your current branch to match origin/master exactly, you can simply git reset --hard. Note that when you run git fetch—I recommend git fetch origin, not git fetch origin master1—your Git will pick up all of their branches, but rename them all: their master becomes your origin/master, their develop becomes your origin/develop, their feature/thing becomes your origin/feature/thing, and so on.

If you do want, and therefore make, commits of your own, on your own branches, just make sure you don't name them origin/whatever (which will be confusing2) and don't try to merge or rebase them in any way until you are sure that's a good idea.


1In current/modern Git, adding master restricts your Git to picking up just their master, and your Git updates origin/master as usual. In older (pre-1.8.4) versions of Git, adding master restricts your Git the same way, but also makes it not update origin/master. So you get their work, which you immediately forget all about, which does you no good. Getting only master saves you a little network time, and then you waste hours trying to figure out why you didn't get anything, only to discover that your chosen Linux distribution ships with a 400-year-old3 version of Git, for some unfathomable reason. :-)

2Git won't get confused—it knows local and remote-tracking branches just fine—but it sure will be confusing for humans to have origin/bruce as a local branch and a different origin/bruce as a remote-tracking branch.

3Slight exaggeration for humor.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to overwrite tags in local git repository?

From Dev

How to overwrite tags in local git repository?

From Dev

How to git clone from *local bare* repository

From Dev

git operations on remote repository without local clone

From Dev

Clone/Push to Git Repository on Local Server

From Dev

Clone/Push to Git Repository on Local Server

From Dev

How to clone from a local git repository to a vm using ansible

From Dev

Clone remote git repository for local use without interfering with the remote branch

From Dev

git repository in window server 2008 cannot clone to local with ssh

From Dev

How to clone local repository

From Dev

How to clone a local repository?

From Java

Git Clone - Repository not found

From Dev

Git cloning a repository that is already a clone

From Dev

Clone git repository without history?

From Dev

Clone a git repository into an InMemoryRepository with JGit

From Dev

Unable to clone git repository on ubuntu

From Dev

Can't Clone Git Repository

From Dev

Can I clone from remote repository, but treat my local version as if it was created using 'git init'?

From Dev

Local Git repository does not exist after successful clone from cygwin bash

From Dev

Where does git clone stores temporary files ,when cloning remote repository to local?

From Dev

How do I run a typescript vscode extension from a local clone of the git repository?

From Dev

Git Local Repository Mapping

From Dev

Git: how to clone another repository automatically after git clone

From Dev

Clone and set local repository config in one command

From Dev

Clone and set local repository config in one command

From Dev

How to get git to overwrite unfound local changes?

From Java

How to clone a Git repository from a Docker container

From Dev

clone from a git repo and commit to a new repository

From Dev

Clone the latest version of a git repository through https

Related Related

  1. 1

    How to overwrite tags in local git repository?

  2. 2

    How to overwrite tags in local git repository?

  3. 3

    How to git clone from *local bare* repository

  4. 4

    git operations on remote repository without local clone

  5. 5

    Clone/Push to Git Repository on Local Server

  6. 6

    Clone/Push to Git Repository on Local Server

  7. 7

    How to clone from a local git repository to a vm using ansible

  8. 8

    Clone remote git repository for local use without interfering with the remote branch

  9. 9

    git repository in window server 2008 cannot clone to local with ssh

  10. 10

    How to clone local repository

  11. 11

    How to clone a local repository?

  12. 12

    Git Clone - Repository not found

  13. 13

    Git cloning a repository that is already a clone

  14. 14

    Clone git repository without history?

  15. 15

    Clone a git repository into an InMemoryRepository with JGit

  16. 16

    Unable to clone git repository on ubuntu

  17. 17

    Can't Clone Git Repository

  18. 18

    Can I clone from remote repository, but treat my local version as if it was created using 'git init'?

  19. 19

    Local Git repository does not exist after successful clone from cygwin bash

  20. 20

    Where does git clone stores temporary files ,when cloning remote repository to local?

  21. 21

    How do I run a typescript vscode extension from a local clone of the git repository?

  22. 22

    Git Local Repository Mapping

  23. 23

    Git: how to clone another repository automatically after git clone

  24. 24

    Clone and set local repository config in one command

  25. 25

    Clone and set local repository config in one command

  26. 26

    How to get git to overwrite unfound local changes?

  27. 27

    How to clone a Git repository from a Docker container

  28. 28

    clone from a git repo and commit to a new repository

  29. 29

    Clone the latest version of a git repository through https

HotTag

Archive