Things to remember while doing git push --force
What not to do.
One should never use powerful git command like
git push --force
This could make permanent damage to your remote branches, which were present in your local repository.
What to do.
always use a branch name with it.
git push -f origin branch_name
If there is no choice, then it should be used with caution.
Sample correct way to do.
if you have a branch corrupt_branch
,
which you need to fix with local code, do as below:
git push -f origin corrupt_branch
Things to learn :
Never use
git push -f
or
git push -f origin
or
git push --force
How to fix it.
If you have make done it by mistake, no worries you can roll it back,
use this command to list the ref logs of a perticular branch.
git reflog remotes/origin/branch_name
now you need to roll it back with -f
to push it on remote
run git reflog remotes/origin/branch_name
it will give the list of the reflogs
050a14f remotes/origin/[email protected]{0}: fetch: forced-update
de59133 remotes/origin/[email protected]{1}: pull origin branch_name: fast-forward
take last version number to restore it back,
git reset --hard de59133
git push -f origin branch_name
This should help you restore your last version of the branch,
Reflog is a mechanism to record when the tip of branches are updated.
This command is to manage the information recorded in it.
Basically every action you perform inside of Git where data is stored,
you can find it inside of the reflog
Also do git fetch
and get pull
regularly to keep local updated with remote,
if you are working with a lot of branches and developers.
#HappySafeCoding