简体   繁体   中英

How to sync my git repository on a new computer?

I'm newbie to git. I have a repository on bitbucket. Now I want to download it on my computer at work and make some changes on the project, then push it again.

First of all I initialized the git on a local directory by this line:

$ git init

Then I set something configuration:

$ git config --global user.name "my name"
$ git config --global user.email my.email@gmail.com

Then I've downloaded it like this:

$ git clone https://ShafizadehSajad@bitbucket.org/ ...

Now I can see the project's files on my local folder. Ok, Good!

Also I made a connection between my repository and my local directory by this command:

$ git remote add origin https://ShafizadehSajad@bitbucket.org/ ...

Now when I enter $ git remove -v , the result will be:

origin  https://ShafizadehSajad@bitbucket.org/ ... (fetch)
origin  https://ShafizadehSajad@bitbucket.org/ ... (push)

seems good!


When I enter this command: git status , I'll see all folders/files with red color. Because of that I entered this command:

$ git add .

Now they are green! Also I wrote a commit for it by this line:

$ git commit -m "there isn't any change"

And then to sync my local directory with the one on the bitbucket, I entered this line:

$ git push origin master

But I get this error:

$ git push origin master
To https://bitbucket.org/lamtakam/lamtakam.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://ShafizadehSajad@bitbucket.org/lamtakam/lamtakam.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

What's wrong and how can I fix it?

Your very first step is wrong:

git init

This initializes a completely new git repository in your current directory. You don't want that, you want to just clone the existing repository.

What actually happened is that you then cloned the existing repository inside the new repository that you initialized in step 1. Nested git repositories are a bit complicated, but the bottom line is that the files appeared red because all the files were new to the newly initialized git repo (which is in no way related to the repo you have in bitbucket).

Your current directory tree looks something like this:

/htdocs
  /New Folder <--- you initialized the new git repo here
    /lamkatam <--- this is the git repo you cloned

Start over by skipping the first step, doing just git clone (you probably want to do this in htdocs folder). You can also skip setting the remote - when you do a clone, it will automatically set the remote called origin and configure it properly.

Everything is ok, in the meantime somebody updated the repository, prior to push you need to integrate your changes:

git pull

It seems like the changes on remote repository could require some "merging" on your side. If that's true you should change your copy and commit those changes and then execute:

git push

If in the meantime nobody didn't change the origin repository you should be able to push your changes.

hint: Updates were rejected because the tip of your current branch is behind

You need to update local repository first (sync with remote) then push to remote ( origin ).

$ git pull origin master
$ git push -u origin master
git push (name of the dir)

例如:git push myfolder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM