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.

Leave a Comment