Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git merge files into working copy

Tags:

git

git-merge

I have a git repository where I store template files that I often use, I did a checkout of the latest version a couple of days ago and made local changes to these template files.

I just made change to the template files master repository and now I want to checkout the most recent version of my master branch but I don't want to have my local changes deleted.

How can I merge the changes from the master branch into my working copy?

like image 963
kristian nissen Avatar asked Dec 16 '22 23:12

kristian nissen


2 Answers

To take full advantage of the merge (or rebase) tactics of git, you could commit the changes to your local repository and then pull. Pull results in a merge between the remote and local repositories, which may be a fast-forward or a recursive merge depending on the upstream changes. You may even want to create a new branch for this (which you can then push to the origin if you want without affecting the master branch).

The stash could help you here, but you run the risk of the stash not being able to apply after the pull.

Simple example:

git add .
git commit -m 'My local template changes'
git pull origin master # fix any conflicts

New branch example:

git checkout -b newbranch
git add .
git commit -m 'My local template changes'
git pull origin master # fix any conflicts

The latter merges the remote changes in master into your local newbranch, but bear in mind that in this situation your local copy of master would still be behind (i.e. if you then did a git checkout master you'd still need a git pull to get the changes).

like image 140
cmbuckley Avatar answered Dec 19 '22 08:12

cmbuckley


you can always save you local changes in the stash, pull the latest changes and then apply your saved changes again.

git stash     -- save your local changes
git pull      -- get latest changes from remote
git stash pop -- apply your saved changes
like image 45
abhay440 Avatar answered Dec 19 '22 07:12

abhay440