want to squash multiple commits in github - 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.

Related

Undoing Git Branch Commit and Merge

I'm currently working on an assignment where we work on a local repository and push to a remote repository when we are done. We are expected to make use of branches, wherein we make all our commits before merging it into our master branch, so no commits directly to the master branch.
Everything was going fine, but I've come across a problem when I made some commits on a branch, then merged that branch into my master, before going back to the before-mentioned branch to make some more changes, committing them and merging it once again back into my master branch.
My network tree is currently looking like this:
network branch
The problem being the green branch that is branching off at the end with hash 2bbbd0c.
I'm essentially looking to undo that commit completely and simply have my branch merge into my master, so my network branch shows that nothing is branching off.
One idea I have is to use git reset –hard 2b32611 (which is the hash for my latest commit on the branch before merging it into my master):
enter image description here
And then to use git push -f origin 2b32611:cookies-user-tracking to push the commit and the branch, but once again I’m not sure if that’ll work, and I don’t want to mess anything up.
The git reset --hard will indeed reset cookies-user-tracking to the right commit (before merge)
All you need to do then is to force push the branch:
git push -f origin cookies-user-tracking

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 :)

How to update my branch with the latest changes from upstream

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.

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.

Why does git-rebase encounter conflicts when upstream is already reachable?

I have a git branch "dev". Branch "master" is reachable from dev. While on branch "dev", if I type "git log master..dev --pretty=oneline" it clearly shows that master is reachable (104 commits earlier). But if I type "git rebase master", it will stop with conflicts. Why is that? Shouldn't rebase have nothing to do in this case, since dev is already based on master?
The reason I am asking this is because I really want to do an interactive rebase with squashes and rewords to clean up a lengthy history. But I am put off by having to resolve all the conflicts that should have already been resolved once I start the rebase.
The following are some related questions that I've already looked at:
Why does git-rebase give me merge conflicts when all I'm doing is squashing commits?
Conflicts with `git rebase`
git rebase master rebases your branch to be based off the latest commit in master. If you want to base it off something earlier, you need to specify the exact commit, i.e.
git rebase `git merge-base master HEAD`
rebase != merge
If you just want to fast forward, use
git pull --ff-only ...
git merge --ff-only ...
If you want to 'automatically rebase/fastforward' depending on the current context and relationship of your branches, I suppose this would work:
git pull --rebase ...
You may want to read the man page on what it does, precisely
http://gitready.com/advanced/2009/02/11/pull-with-rebase.html
http://longair.net/blog/2009/04/16/git-fetch-and-merge/