In this manual, we will learn the procedure to push force changes in Git.
How to Push Force Changes in Git?
Suppose that two team members are working on a similar project file. If one member updates the file after the changes made by another member, the changes done by the first member will get lost because of the last update. To solve this issue manually, it is required to force pull for overwriting the updates using Git “$ git push origin main –force” command.
Now, follow the below steps to resolve the above-discussed issue!
Step 1: Move to Git Directory
First, execute the “cd” command with the path of the Git directory to move to it:
Step 2: Clone Remote Repo
Clone the remote repository to the local repository using the “git clone” command:
As you can see, our remote repository is cloned successfully:
Step 3: Navigate to Cloned Repo
Next, navigate to the clone remote repository using provided command:
Step 4: List Content
Execute the “ls” command to list the content of remote repository:
Step 5: Update File
Open the required file with the “start” command and add some text to make changes:
As you can see when above command is executed, the specified file is opened with default editor:
Step 6: Add File
Add the changes and track the file to the Git directory using provided command:
Step 7: Commit Changes
After that, commit the changes to save it on the Git directory:
Step 8: Force Push
Finally execute the “git push” with the remote name “origin”, branch “main” to push forcefully into the remote repository:
Here, we have used “–force” option to forcefully push and overwrite the changes that already exist on the remote repository:
That’s all! We have provided the easiest method to push force changes in Git.
Conclusion
To push force changes in Git, first, navigate to the Git local repository and clone the remote repository using the “$ git clone <url>” command. Next, move inside the cloned repository and list the content. Then, open the file, update it and commit changes to save it in the Git repository by executing the “$ git commit -m <message>” command. Lastly, run the “$ git push origin main –force” command to push
changes forcefully. This manual elaborated the method to push force changes in Git.