6

I have a git repository that contains folders and files. And have locally a same folder as in git but a little bit changed files but not connected to git. How can i connect my local folder to the same git and commit all changes that i have done?

Sascha Frinken
  • 3,134
  • 1
  • 24
  • 27
Grish Barseghyan
  • 79
  • 1
  • 1
  • 4
  • 1
    I don't think this is a duplicate. The question can be answered with `git -C ./my-project init` and then setting the existing Git repo as a remote. – shadowtalker Jun 03 '21 at 18:22

1 Answers1

19

If your local folder is not a git repository, you should make it a repository first and commit your changes in a new branch then add a remote linking to your existing repository where you want to push your branch containing the changes.

$ cd path/to/your/local/directory
$ git init # make it a git repository
$ git checkout -b my_awesome_branch # checkout to a new branch containing your local changes
$ git add --all
$ git commit -m 'This should be a Message describing your local changes'

You will have all your local changes in git branch my_awesome_branch. Now, You need to setup an upstream to remote repository where you will want to push this my_awesome_branch.

$ git remote add my_awesome_upstream <your-upstream-git-repository-url>
$ git remote -v # To list remotes, and see if your remote is added correctly
$ git push my_awesome_upstream my_awesome_branch

Now, you can go to your remote repository and checkout to my_awesome_branch from the UI in order to see your changes.

To merge the changes into the main branch(e.g. usually master), you can open a merge/pull request against the main branch and review your changes before merging them to your main branch.

Hope it helps.

Let me know if any of the above is not clear. I will be in touch.

Resources:

  1. Adding an existing project to GitHub using the command line.
  2. Adding a remote
mrehan
  • 1,122
  • 9
  • 18
  • This is great, I'm just not sure what to use as `my_awesome_upstream` - is that `origin` or something else? It seems it's gonna be used as some internal name for the upstream repo, but do I use it later for anything else? What people normally use here? I'm new to git... – jena Aug 14 '23 at 14:05
  • Never mind, I checked the second link in resources and the answer was right there at the top (I should use `origin`). Thanks for this nice answer! – jena Aug 14 '23 at 14:13