简体   繁体   中英

How to accept pushed changes on git remote repository?

I have git cloned my git remote repository from my web server onto my local machine. After making changes on my local machine I have committed and pushed changes to the web server with the git remote repository, and see that there are changes when I run git status. But the only way I can think of implementing the change is to run git stash. I am sure this is the incorrect way. I have tried git fetch and git merge, but this is the remote repository that I attempting to update with changes from others for production.

git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.txt
#

git stash
Saved working directory and index state WIP on master: eb8230d Changes
HEAD is now at eb8230d Changes

Looks like I needed to run

git checkout -f

to update the web server to the head of the branch or changes.

I'm guessing what you mean is that the remote repository has a working directory that is a live web site. You want to deploy changes to your web site by pushing those changes to the remote repository and have the changes automatically applied to the working directory, ie the live web site.

What you want to do is set up the remote repository with a post-receive hook that will checkout the new files.

Git's defaults are set up assuming that changes in a working directory are important and should not be easily blown away. This is because the defaults are designed for the working directory to be an actual directory where the user is working, making changes, etc. For a repository acting as a live web site this assumption doesn't hold and so the default behavior doesn't work very well.

In particular, pushing to a remote repository will update branches and the git index on that repository but will not make any changes to the working directory. This makes it appear as though there are changes in your working directory which simply undo whatever work was just pushed.

You can take the command you've already found, git checkout --force and put this in a post-receive hook, and any time you push changes the repository will checkout the new files. This blows away any changes you have in the working directory, but presumably those 'changes' will never be anything but the fake changes that just undo the pushed changes. If you ever manually edit anything in the remote repository's working directory you risk losing those edits.


Another option is to have the repository on your web server be a --bare repository, as remote repositories normally should be. Instead of having a working directory that also serves as the live web site, have the post-receive hook script run commands to deploy the web site to another location.

http://krisjordan.com/essays/setting-up-push-to-deploy-with-git

If you want to manually update the live server through SSH and git commands, you would use

git checkout -f

If you want your local push to automatically update the remote repository, run the below command on the live server.

git config receive.denyCurrentBranch updateInstead

You will need to have your remote webserver pull the latest commits from the branch it is tracking. this is manually done with git checkout (branchname) and git pull (once on the correct branch).

You can write a cronjob to periodically attempt a git pull, or set up hooks for your repository. This would send a message to your remote webserver every time a commit was pushed up to the repo.

I solved this problem by using a Jenkins server and the ssh-command plugin to SSH in and do a git pull.

The other solution is to add your webserver as another remote. You could do this by typing:

git remote add webserver ssh://<your user name>@<FQDN>:22/path/to/repo.git
git push webserver <branch>

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