简体   繁体   中英

Git how to update local repository and keep my changes

OS: ubuntu 16

I use git clone for copy remote repository to my local machine. I made some changes (via nano) in some code lets say in 5-10 files (repository have more then 2000 files i think).

But now this repository get some nice updates. And i want get all this updates, but i want keep my custom changes. I cant commit to this repository because i believe my code and fixes are "bad" and not perfect.

Which command i should use to get all updates and keep my changes in files?

One option would be to stash your working directory (and possibly stage), pull the changes in, and then apply the stash afterward:

git stash
git pull origin master   # or whatever the branch name is
git stash apply

This should leave you in roughly the state you were in before pulling in the new changes. I said "roughly" because applying the stash may cause merge conflicts. Such conflicts may be unavoidable if the changes you made conflict with changes made to the repo since you last synched.

If you are worried about losing your work, then I would recommend also creating a side branch with your changes. For this you can try:

git stash
git checkout -b safe_branch
git stash apply
git commit -m 'my work'

Then, you can try merging or rebasing this branch on the original remote branch. Assuming you wanted to merge, you could try:

git fetch origin
git checkout safe_branch
git merge master     # or whatever the remote branch is called

Now you have a branch with a bona fide commit containing your work. It would be really hard to lose your work in this safer setup.

Simplest way (if you are working on the branch on want to update):

git stash
git pull
git stash pop

Another option is to create another branch to keep your changes:

git checkout -b my-new-branch
git add .
git commit -m "my changes"
git pull --rebase origin master # this will get the updates and place your changes on top of it

On booth methods you might get conflicts, to solve them you can use:

git mergetool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM