Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 5

Force-pulling files from origin while keeping new local files

Solved! Go to Solution.

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)?

4 Replies
Copilot Lvl 3
Message 2 of 5

Re: Forc-pulling files from origin while keeping new local files

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?

Highlighted
Commander Lvl 2
Message 3 of 5

Re: Forc-pulling files from origin while keeping new local files

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-

 

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!
Copilot Lvl 3
Message 4 of 5

Re: Forc-pulling files from origin while keeping new local files

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 `git pull origin master` command, but it still showed the same message and aborted the pull execution.

 

Is there any way to do this? Would I ideally need to use a hook to do this?

Solution
Commander Lvl 2
Message 5 of 5

Re: Forc-pulling files from origin while keeping new local files

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.

 


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 `git pull origin master` command, but it still showed the same message and aborted the pull execution.

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:

1. Pull the origin/master into local/master:

 

                                      D local/new                   

                                    /

                      A---B---C---E local/master
                                      /
                  A---B---C---E origin/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
                                      /      /
                   A---B---C---E---D origin/master

 

2. Push the local/new to origin/new:

 

                                       D  local/new

                                    /      

                      A---B---C local/master
                                          
                   A---B---C---E origin/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.

 

 


Is there any way to do this? Would I ideally need to use a hook to do this?


You can use the pre-push hook:


The pre-push hook runs during git push, after the remote refs have been updated but before any objects have been transferred. It receives the name and location of the remote as parameters, and a list of to-be-updated refs through stdin. You can use it to validate a set of ref updates before a push occurs (a non-zero exit code will abort the push).

but you dont need that, it's better to use branch to do what you asked.

 

Hope this helps 🙂  

 

-Gabriele-

 

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!