简体   繁体   中英

Can I track or add to a Git repo a file that's created on a remote but not in local directory?

If there's a file that's added directly into the remote, one that I'm pushing too from my local directory, is there a way to have that file added into the remote so that I can pull or fetch the remote and get that file or any new files?

This seems like a bad practice no matter what, but let's say a file is added during a WordPress install, like the wp-config that WP will create automatically, now that's on the server but not in the Git repository unless I move it with FTP or scp. What is the best practice when that happens?

I think what you're asking is if there's a way to have Git automatically commit and push new changes? No, but you can do it with whatever automation tool you like. But it's rarely a good idea to automatically commit to version control, what you probably want instead is a backup tool.

Your scenario about a config file that WP might create during install is a good example of misusing version control. That config file is only useful to that particular computer. Other deployments will also generate that file. If they both try to push their versions, it will create legitimate conflicts. Instead, keep the version in the central repository fairly generic and deploy from there.

If you want to keep a record of what changes on each deployment, use an incremental backup tool.


What you might have is each deployment is a clone, but local changes are done on their own local branch. This local branch is never pushed (or if it is, its only for archival purposes). If you make a change that is generically applicable to other deployments, like a bug fix, push just that change back.

You'd probably have one branch for local changes, and use master to push fixes back. You can commit whatever you like in local, including the generated WP config, it's for local changes to this deployment.

              [origin/master]
A - B - C - D [master]
             \
              E - F - G [local]

As there are new changes to master upstream, fetch them and rebase local on top of them.

git checkout master
git pull

                      [origin/master]
A - B - C - D - H - I [master]
             \
              E - F - G [local]

git rebase master


                      [origin/master]
A - B - C - D - H - I [master]
                     \
                      E1 - F1 - G1 [local]

If there's a change in local that could be submitted upstream, like a bug fix or a change to a generic template or image, use git cherry-pick to copy just that single change to master and push it.

git checkout master
git cherry-pick F1
git push

                           [origin/master]
A - B - C - D - H - I - F2 [master]
                     \
                      E1 - F1 - G1 [local]

If you really want to push local to a central server, push it to a branch named for the deployment. Like maybe the hostname or client. This would be used only for archival purposes.

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