简体   繁体   中英

After a Git push, remote repo shows files as deleted

I cannot find any of the files on my remote repo.

  1. Created a Git repo on shared hosting (site5) as described by their tutorial
  2. init'd it and added a simple text file. OK
  3. Cloned on to my local (WinXP) machine. OK
  4. Deleted the test file on my local copy, added a few real files. OK
  5. Committed the local changes. OK
  6. Pushed to remote server. OK
  7. MsysGit threw a few warnings (below), but told me we had success.
  8. FTP'd into the remote server, but the files weren't there .
  9. SSH'd in (using PUTTY) and ran git status . It gave me the whole list, but with the word "deleted" preceding each file. See below.

Working off of the cue that I am not on any branch, I checked out the specific commit. It found it, but the files and git status still show nothing doing. It told me:

Note: moving to '9c47b5' which isn't a local branch

Tried to checkout master HEAD. No luck, it tells me

-jailshell: /usr/bin/git: No such file or directory


Git status shows:

[frumwebc@your tanee.com]$ git status
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    _notes/dwsync.xml
#       deleted:    contact-us/_notes/dwsync.xml
#       deleted:    contact-us/index.html
#

The warnings were:

warning: updating the current branch
warning: Updating the currently checked out branch may cause confusion,
warning: as the index and work tree do not reflect changes that are in HEAD.
warning: As a result, you may see the changes you just pushed into it
warning: reverted when you run 'git diff' over there, and you may want
warning: to run 'git reset --hard' before starting to work to recover.
warning: 
warning: You can set 'receive.denyCurrentBranch' configuration variable to
warning: 'refuse' in the remote repository to forbid pushing into its
warning: current branch.
warning: To allow pushing into the current branch, you can set it to 'ignore';
warning: but this is not recommended unless you arranged to update its work
warning: tree to match what you pushed in some other way.
warning: 
warning: To squelch this message, you can set it to 'warn'.
warning: 
warning: Note that the default will change in a future version of git
warning: to refuse updating the current branch unless you have the
warning: configuration variable set to either 'ignore' or 'warn'.
Pushing to fru@67......ee.com
To fru@67......ee.com/26d352f..9c47b55  master -> master

When you push to a remote repo in Git, it does not update the working copy of that remote repo. This means that the working copy will be out of sync with the last version that you pushed. To avoid this confusion, it is recommended to only push to bare repos; repos that do not have a working copy. The reason is that if anything in the working copy has changed, you will not have a chance to be warned about this and perform a merge, because you are pushing to a remote repo.

Generally, the only way you should be modifying a working copy is from within it, such as doing a pull or checkout from within that working copy. If you want a copy of your code to be checked out somewhere on your server automatically when you push, you should set up a post-receive hook that will then go a git pull into a repository that has a working copy.

I have a similar use case at work: desiring a 'pristine' home directory on a number of servers where I check processes, monitor logs, etc. - without having to change every single one of them when I add a new command, alias, script, etc. Learning from the existing answers here I solved like this:

  1. Create a 'master' repo on my desktop machine representing my desired $HOME directory on the remote
  2. Get the master repo into a good state with the setup I wanted (in particular with a canonical .ssh/authorized_keys file)
  3. On one remote machine, create a new (non-bare) repo in $HOME
  4. Push the master to the remote repo in $HOME
  5. Run 'git reset --hard' in remote repo

To keep them up to date I added the following git alias to help me out with updating all my remote working copies in a single command:

[alias]
    pushall = "!for remote in myremote{1..10}; do git push ${remote} master; ssh ${remote} git reset --hard; done"

Now when a new remote is provisioned - I rsync my $HOME directory from an existing remote to the new one, then add my new machine to the alias above. Running 'git pushall' after making a change (say after adding a handy new script to look at logs) will then update all the remote working copies with a pristine copy of what I want my (remote) $HOME directory to be. No more updating every single remote by hand!

Note: the double quotes around the entire alias are important here otherwise the semi-colon will bite you

Thanks to others for existing answers that helped me get this working.

You shouldn't have created a 'standard' git repo on the remote host, as Brian Campbell noted. What you should do:

  1. copy all the files you wish to place under version control to a directory on your local machine.

  2. delete them and their containing directory on the remote host.

  3. still on the remote host create a new directory called projectname.git (substitute a meaningful name for projectname ), cd into it, and run git init --bare

  4. on your local machine, in the directory in which you've placed your files:

    • run git init to initialise a git repo
    • run git add . to add all files and sub-directories
    • run git commit -m "Initial commit"
    • You should see something like [master agd27c9] Initial commit
    • run git remote add origin ssh://hostaddress/path/to/dir/projectname.git (you may have to modify this slightly, if you usually access your remote host using ssh://user@hostaddress, for instance)
    • run git push origin master
  5. You should see a message indicating connection to your remote host, and some output from git indicating that the push was successful. It usually looks like:
    Counting objects ...
    Delta compression ...
    Writing objects ...
    Total ...
    To ssh:// ...
  6. If everything happened as described above, you're done. You could add default push and merge if you wish:
    • git config branch.master.remote origin && git config branch.master.merge refs/heads/master

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