Git

How to Push Force Changes in Git

While working on Git, project files and folders can be shared with project members working as a team. When multiple members try to work on the same files and folders, conflict is encountered, and they want to change or update these files with the correct data. For this purpose, members can use the “$ git push origin main –force” command to push changes forcefully and overwrite the changes that already exist on the remote repository.

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:

$ cd "C:\Users\nazma\Git\Force_Push"

Step 2: Clone Remote Repo

Clone the remote repository to the local repository using the “git clone” command:

$ git clone https://github.com/GitUser0422/demo.git

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:

$ cd demo

Step 4: List Content

Execute the “ls” command to list the content of remote repository:

$ ls

Step 5: Update File

Open the required file with the “start” command and add some text to make changes:

$ start file1.txt

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:

$ git add file1.txt

Step 7: Commit Changes

After that, commit the changes to save it on the Git directory:

$ git commit -m "updated"

Step 8: Force Push

Finally execute the “git push” with the remote name “origin”, branch “main” to push forcefully into the remote repository:

$ git push origin main --force

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.