How to delete a file at remote branch after I delete the local file

sijieh July 23, 2019

Hi,

I am a newbie to Git system. Before I found out using `git rm` to delete files, I directly delete files without using the command. For example, I have file `a.bin` in both my local and remote branch, they are synced. After I delete the `a.bin` locally without using `git rm` and then pushed to the remote branch, the `a.bin` will still be in the remote branch. So I am wondering how can I delete the `a.bin` in the remote branch without sabotaging what I have in the local one? Namely, I want to sync the remote branch to my local one.

 

Thx!

2 answers

1 accepted

1 vote
Answer accepted
Joao Sgreccia
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 24, 2019

Hi @sijieh

Typing git status on the command line will provide you with the next steps:

$ rm myfile
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: myfile

1) use "git add/rm <file>..." to update what will be committed

Using git rm <deleted-filename> and git add <deleted-filename>.

$ git add myfile
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted: myfile

It will stage the deleted file, and followed by git commit and git push will remove the file from the repository.

2) (use "git checkout -- <file>..." to discard changes in working directory)

Using git checkout -- <filename> in your scenario, it will fetch the file from the repository, giving you the alternative to use git rm followed by git commit and git push

$ git checkout git checkout -- myfile
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

nothing added to commit but untracked files present (use "git add" to track)

$ git rm myfile
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

deleted: myfile
sijieh July 24, 2019

Thx! It works for me!

Like Joao Sgreccia likes this
0 votes
Ana Retamal
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 24, 2019

Hi @sijieh

When your code is versioned with Git, all the operations should be done through Git commands and not by directly interacting with the repository, as this can cause trouble as already happened in your case.Since you said you're a newbie, I recommend you to go through some tutorials, we offer some to help you get started and you can find it at Become a Git guru.

Following up on this, this is a Git question and not really an Atlassian related question, if you need more help there's plenty of documentation and specific forums available that you can access online to learn and get more help about this topic.

Hope you find that useful!

Ana

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events