How to update my branch with the latest changes from upstream - github

This question may sound trivial but I really don't know how to do it right... I am working on an open source project and the source codes are on Github, so I've forked that repo, added a "remote upstream" pointing back to that original repo, and also created a branch in my forked repo and made some changes. It looks something like this:
$git remote -v
origin https://github.com/my_github_account/project_name (fetch)
origin https://github.com/my_github_account/project_name (push)
upstream https://github.com/project_name/repo_name (fetch)
upstream https://github.com/project_name/repo_name (push)
$git branch
master
* test_branch
After making some changes on my "test_branch", there are also some new changes in the upstream, so I want to get the latest changes from upstream, merge them to my test_branch and see if everything works fine. I tried:
git pull upstream master
which works fine for this purpose, but it also created a "new commit", since the commit messages will also be shown when I create a pull request to the project, if I pull the upstream changes frequently in this way, there will be a lot of
Merge branch 'master' of https://github.com/project_name/repo_name into test_branch
messages showing, which is kind of messy to the others and I don't see others' pull requests contain these kind of messages either... (or maybe there is some way to get rid of these messages?)
I also googled it and tried:
git pull --rebase upstream master
and then do:
git push origin test_branch
but then I got some error saying something like:
! [rejected] test_branch -> test_branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/my_github_account/project_name'
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.
Hmm... I don't have a clear picture of what it means... Maybe I was not doing it the right way... So what is usual the procedure of getting the latest changes from the upstream, integrate/merge them into my branch?
Thanks in advance!

The second approach is the correct one (replaying your test_branch on top of an updated master).
But it will also have the expected side-effect of rewriting the history of your test_branch, with you having to force push it to your fork
git push --force origin test_branch
Since it is your fork and nobody else is working on origin/test_branch, it is ok, and even if you had a PR in progress, that Pull Request would detect the force push and update itself accordingly.

Related

git reset --hard origin/master still giving different code

hello guys i want to ask about my GITHUB problem, why is my local master branch is different from remote?, i was modifying without committing then git branch -b and git add and git commit, and then after i push the new branch, create pull request, and merged to master, its different when i comeback to local master and git pull
i also already do git fetch origin and git reset --hard origin/master
the code is almost the same as remote but its just weird, a few declaration is missing and one entity naming is the same as my other branch
it turns out, that there is open tab in VSCode that i havent saved from another branch, close it out and dont save it solved the problem

Getting error 'git pull ...') before pushing again

I am very new to Github. I cloned a repo and created a test branch. After that I pushed the test branch and created a pull request. After that, I made the changes in test branch and committed the changes.
Before pushing my latest commit in test, I did the following in test branch:
git fetch
git rebase origin/develop
Now, I am trying to run the following command from the test command:
git push origin test
I am getting the following error:
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.
I have already done git fetch and git rebase origin/develop. What should I do now? Why am I getting this error?
When you rebase, you're rewriting your history (and essentially putting commits from both branches in order) - if you have new commits on your branch this will generally put them in a different chain than they were in before.
This means git will think your branches are out of whack, but if you know that you had the latest before you started, you can force push. This is generally OK when working on a branch by yourself.
If you are working off a shared branch where commits may be coming in regularly, I wouldn't do a rebase but would do regular merges from the other branch instead. This puts all the new stuff at the tip of your branch but avoids any history rewriting.
Try This:
You have to take pull first.
git pull origin test
After that u can push your data.
git push origin test
Try This : You can do this git pull shortly after, The screen will appear. There are will be three cards. You can edit card, it according to your own code from.
Before getting the error, you should do definitely git pull. don't forget :)

Can't push in github before pulling. Is it because I changed my code through github website before?

I can't push to github. I think I have made changes in my code through github website before and now it gives me a hint when I do git push origin master:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/hrvoojex/telecom-toplist.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.
Do I just have to do:
git pull origin master
and after that I will be able to do:
git push origin master
I made a lot of changes in my code localy so I don't want to lose al of my work till now from the last commit.
Basically you have got two solutions:
If your local copy is the good one, you may want to overwrite the remote work. You can do that with a simple force push.
git push -f origin master
If you want both changes, you can merge remote work to local one, and then push the result. In that case, the suggested commands are the way to go.

want to squash multiple commits in github

I want to squash two latest commits with commit messages "first" and "second". First I pull master then I use the command
git rebase -i HEAD~2 master
It shows me both commits in an editor like this:
pick first
pick second
Then I change this editor as:
pick first
squash second
After saving the changes I got this message:
Successfully rebased and updated refs/heads/master.
It did change anything in remote master. To apply these changes I use the git push command and got the following error:
To https://github.com/aneelatest/GITtest.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/test/GITtest.git'
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.
Then I again run the git pull command and it merge origin master and make another commit with this commit message:
Merge branch 'master' of https://github.com/aneelatest/GITtest
After this when I run git push, it squash the two commits into one with message "first".
The problem is that in remote master, I have now four commits:
first
second
Merge branch 'master' of https://github.com/test/GITtest
first
Where I want only one commit which is the squashed one with commit message"first".
Any ideas where I am doing mistake??
git rebase rewrites history, because the commits were modified. It's not a problem when the said commits haven't been pushed to a remote repository, but here the remote was previously pushed with the commits you rewrote, so it rejected the push.
When you pulled from github, it merged both histories, and applied your squash commit, hence the mess.
Conclusion: when you want to rewrite commits that has already been pushed, you have two options:
don't do it
do a git push --force, which will rewrite history also on the remote, and tell people that you have rewrote history, so they'll see strange things on their next pull.

eclipse git confusions: How to merge & push?

I can never seem to figure out how to use git. I just do random commands and it works eventually. This is what I usually do when I want to push:
Fetch from upstream
Commit
Push to upstream
However, the above steps don't work sometimes. For example, when I do fetch from upstream, my local branch doesn't get updated.
How do I merge the remote branch into my local branch? Is there a difference between pull and fetch?
"git pull" is equivalent to "git fetch" followed by "git merge"
What you want to do is:
git commit -am "your message here"
This commits your current changes locally, and only locally. Now, assuming branch "foo":
git pull origin foo
This does a "git fetch" followed by a "git merge". Everything in this step is done to your local branch, and still has no effect on the remote branch. Depending on how your git is setup, you might need to specify the branch.
If you have a merge conflict, you will have to fix the files listed, then add them back:
git add path/to/resolved.file
When you are done, push everything you have from your local changes, to the remote server:
git push origin foo
If you pull before committing, that might cause your local branch to not be updated.
tl;dr, always commit before pulling!
Well to understand differences betwene git fetch and git pull look here.
In the simplest terms, "git pull" does a "git fetch" followed by a
"git merge".
You can do a "git fetch" at any time to update your local copy of a
remote branch. This operation never changes any of your own branches
and is safe to do without changing your working copy. I have even
heard of people running "git fetch" periodically in a cron job in the
background (although I wouldn't recommend doing this).
Working with git in eclipse might be a little bit tricky, but if you understand git basics, you should cope with it. I recommend you to read one of git tutorials, so you will be able to understand basic git operations.
The first step to learn git: Don't use egit.
Now, you are fetching and pulling from upstream. That makes me think that several people have write-access to the same repository, so we probably don't want to be doing a whole lot of merges to complicate history. It would be best to do this. I'm assuming you have already committed a changes or a set of changes to your local master branch that you want to place on the upstream repository which has been pushed to by someone else while you were making your commits.
First, we fetch the new changes but don't use them yet. This updates upstream/master to the new head of the upstream master branch.
git fetch upstream master
Now, we need to pull in these changes. This command rewrites history, and we are assuming
that you have not published your changes anywhere yet.
git rebase upstream/master master
If you have published your changes, this messier command is the one you should use (do not use both, just use this one!)
git merge upstream/master master
Now, we can push:
git push upstream master
The first two steps can be shorted to git pull --rebase and git pull for the rebase and merge versions respectively.
If you are already on the master branch, most of those second arguments are superfluous but I wrote them in for clarity. Notably, giving a second argument to git-rebase or git-merge will simply check out that branch before doing the operation. Supplying master to fetch and push is only necessary if you don't have the refs set up to automatically fetch and push master.