Force-pulling files from origin while keeping new local files #22318
-
I would like to know if there is a cmd git command or any way to have file changes of the master repository to replace files that are different on there than local, but still keeping any new and unchanged files locally. To demonstrate: [BEFORE FORCE PULL] Local directory: Master repository: File_1 File_1 (changed) File_2 File_2 (changed) File_3 File_3 File_4 File_5 [AFTER FORCE PULL] Local directory: Master repository: File_1 (changed) File_1 (changed) File_2 (changed) File_2 (changed) File_3 File_3 File_4 File_5 I want to do this before any pushing. Github desktop does not seem to like to do this, however. Is there a way I can do with a git command via command prompt (cmd)? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Anyone? If it’s not possible, it would be great if I could be at least informed of that, and in which case, what should I do? I need to get files, but at the same time I have files to upload. On SVN, I can easily pull changes to local first, why is it such a bother when using github desktop? |
Beta Was this translation helpful? Give feedback.
-
Hi @lo177, Welcome to the GitHub Community forum! I dont understand what do you want to do. Do you want to execute the pull before all the push? Maybe do you need an hook, you can find all the docs here -> Customizing-Git-Git-Hooks Hope this helps 🙂 -Gabriele-
|
Beta Was this translation helpful? Give feedback.
-
Yes, basically a pull before a push, but also without deleting files in my local directory that are on the local directory, but not on the cloud repository. Github Desktop does not let me pull without a push at all to begin with. I tried to do it via cmd instead by using Is there any way to do this? Would I ideally need to use a hook to do this? |
Beta Was this translation helpful? Give feedback.
-
Did you try using branches? This is probably the best solution for you. Create a branch called local in your local repository and keep master on remote repository. With this solution you don’t need to pull before a push every time you changes something, yuo can push every time you want to remote and from there (on github) create a pull request to see if the changes made on the local branch is able to merge in master. When you need to work in the local master you can checkout and pull.
That’s because it’s not a problem of github desktop but it’s git that tell to you that it cannot merge the same branch (in this case master) that are divergent in history. Assume the following history: A—B---C local/master A—B---C origin/master Now you edit some files in local master and create a new commit, at the same time someone else create a new commit to the origin master. The new history will be: A—B---C—D local/master A—B---C—E origin/master You can see local/master and origin/master diverged because there are the commit D and E before the relative commit C. Now you want to try to pull the origin/master on local, but git cannot know what of the commits are made first so it cannot merge the origin/master into local/master. To prevent this you can create a new branch from the local/master before the new commits: local/new / A—B---C local/master A—B---C origin/master Create the same commits as before: D local/new / A—B---C local/master A—B---C—E origin/master Now you have 2 choices:
D local/new / A—B---C—E local/master Merge the local/new into local/master and push the local/master into origin : D local/new / \ A—B---C—E---D local/master
D local/new / A—B---C local/master \ D origin/new And from there (github) create a pull request to see if everything is ok and in the end merge (if possible, if not you can see this post):
A—B---C—E---D origin/master / D origin/new I know that it seems difficult but every time you do that become even more simply.
You can use the pre-push hook:
but you dont need that, it’s better to use branch to do what you asked. Hope this helps 🙂 -Gabriele-
|
Beta Was this translation helpful? Give feedback.
Did you try using branches? This is probably the best solution for you. Create a branch called local in your local repository and keep master on remote repository. With this solution you don’t need to pull before a push every time you changes something, yuo can push every time you want to remote and from there (on github) create a pull request to see if the changes made on the local branch is able to merge in master. When you need to work in the local master you can checkout and pull.