Question

I am new to git.

At work we have a remote repository which I pull to make changes in my local before pushing back up.

Just want to confirm that my work process is correct.

  1. git fetch to get all the changes to origin/master
  2. git checkout -b newbranch origin/master
  3. git merge origin/master (when I am on the current branch I am working on)

This is the process I have been doing to do my work but I keep getting this annoying error:

error: failed to push some refs to 'ssh://xxxxx'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I determined that it has to be due to the way I set up my work.

Please help!

Was it helpful?

Solution

The git fetch command imports commits from a remote repository into your local repo.

The resulting commits are stored as remote branches instead of the normal local branches that you’ve been working with.

This gives you a chance to review changes before integrating them into your copy of the project.

$ git pull <remote>

Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy. This is the same as git fetch <remote> followed by git merge origin/<current-branch>.

$ git pull --rebase <remote>

Same as the above command, but instead of using git merge to integrate the remote branch with the local one, use git rebase.

OTHER TIPS

You should check the state of both remote and local branches. For that do

  1. git fetch origin
  2. git log newbranch --not origin/master. This command should display your local commits not pushed to the central repo yet.
  3. git log origin/master --not newbranch. This displays commits which are in origin/master but not have been merged into newbranch
  4. merge origin/master into newbranch if step 3 shows smth
  5. Repeat steps 1-4 until steps 3 give you empty output (no commits in origin/master that weren't merged into newbranch)
  6. do the push again: git push origin newbranch:master

git pull first fetches the code from the remote repo and updates the local repo by merging it.

git pull origin master

After committing your changes you must take a pull and then push your changes. Any conflicts should be resolved manually. While working on new branch, first merge it with master while you stay on that branch.

$git branch 
newbranch
$git pull origin master 
This will update your new branch with master changes and both the branches are at same state

Now, $git checkout master and then git merge newbranch This will merge the newbranch with master.

I would suggest you to refer Git-Branching-Basic-Branching-and-Merging to have a good understanding over git.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top