How to push changes to github without pull

You can do a forced push.

git push -f origin branch_name

The forced push will erase all commit history of the remote repository's branch, and replace it to your branch.

Check the answers for doing forced pushes in "How do I properly force a Git push?". Forced push can have unintended consequences as mentioned in "Git: How to ignore fast forward and revert origin [branch] to earlier commit?", so check for the same.

The forced push will be an incorrect way to push stuff in your case, since you already have previous commits on GitHub, and this will erase the commit history for previous commits.

Hence, to preserve your commit history, you can do the following things:

Remove all the files from the git repository, and then add the new files here, and then commit the updated files:

git rm -rf .
cp -r path/to/updated/code/* .
git add .

Doing a git status now will tell you which files the other developers modified, and a git diff will show what modifications are there.

If a file has remain unchanged, then git rm and git add will nullify the effect of each other.

The files which those developers deleted remain deleted since you ran the git rm for them but no git add.

Once you are satisfied that these indeed are the changes, you can commit using

git commit -m "Merged new code"

Potential gotchas:

  1. Only the file mode has changed (755 <=> 644) - depending on what code the other developers sent you.
  2. You will lose your .gitignore file with git rm -rf . (and similarly .gitattributes and other such files). Reset HEAD for each such files using git reset HEAD .gitignore before the commit.
  3. Different line terminating characters (in case different development environments are being used), So check for them appropriately.

It seems like the other people worked on your project outside version control i.e upon bare files and folder.

I would suggest that you clone the repo, copy (and hence replace) the cloned content with the new content, commit and push these changes.

The current commands of git init etc you are using create completely new repos, which is not what you want.

Tags:

Git

Github