0

I am currently using BitBucket as version control for my project. I see different node (In the image with black dots/multiple commits) of Branch graph on multiple commits. How can I make sure only one node is present on branch graph for all commit I make? In other words all commits must be seen as 1 commit and not incremental

Image 1

I have tried using git commit --amend -m "Message" but it always gives me

error: failed to push some refs hint:'git pull' before pushing again

So I git pull and push again which creates a new commit.

Paritosh M
  • 289
  • 2
  • 5
  • 16
  • It _sounds_ like maybe you are just saying you want to squash multiple commits into a single commit, to clean up your history. That's very easy with an interactive rebase. – matt Dec 03 '20 at 20:24
  • @matt I have add the image, please check. The black dots(nodes) are commits. For each commit new commit, new node is generated. I am trying to get just single node for all commit on graph – Paritosh M Dec 03 '20 at 21:00
  • @matt I hope question is now clear – Paritosh M Dec 03 '20 at 21:02
  • Okay so for different commits different nodes are created, in my team they just want 1 node to be created for all commits. That's the reason I am using `append` so that no new commit is created and changes are pushed to previous commit. `Pushing to old commit without creating new commit is what I am trying to achieve` – Paritosh M Dec 04 '20 at 03:00
  • If you are working on a branch and you make many commits, you can squash them down to one commit before you push and ask for a pull request. Maybe that’s what your team is telling you. Why not ask them whether that is what they mean? – matt Dec 04 '20 at 03:05
  • Yes that is somewhat similar to what I am looking for. `Eg` If I have already made 1 commit, then my 2nd commit should not be as new commit but update on commit 1. `This looks like I just make 1 commit in the end` – Paritosh M Dec 04 '20 at 03:09
  • No problem, just interactive rebase and squash them together. It’s easy. – matt Dec 04 '20 at 03:11

1 Answers1

2

What you're asking to do is perfectly reasonable; I like to do it too. I can just keep making commits on my branch, and then when I am ready to push, I squash them all down to one commit, and push. So the people seeing my PR see it as a single finished product; they don't need to see the whole history of how I got there.

One way to squash all commits in chain into a single commit is to do a soft reset back to the first commit and then amend that commit. See my three types of regret; this is regret type 1.

However, on the whole I like to use interactive rebase for this. Here is an example. We are working on a branch mybranch and we have made three commits:

* cd591cc (HEAD -> mybranch) z
* cdbe774 y
* 3ce8eaa x
| * e86283a (master) b
|/  
* 4227e7d a

Now I want to push so we can merge to master. But I do not want three commits x and y and z. I want just one commit.

So I say:

git rebase -i 4227e7d

(because that is where the branch started). The editor opens and I see this:

pick 3ce8eaa x
pick cdbe774 y
pick cd591cc z

I rewrite it like this:

pick 3ce8eaa x
squash cdbe774 y
squash cd591cc z

I save and close the editor. The editor opens again so I can write my commit message. I delete everything in the editor and replace it with:

my very cool work

I save and close the editor. Now things look like this:

* bb8c9fb (HEAD -> mybranch) my very cool work
| * e86283a (master) b
|/  
* 4227e7d a

Just what I wanted! My branch is just one commit, and now I am ready to merge (or push for a pull request).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • You _can_ do this with doing a `commit --amend` every time you commit, but I can never remember to do that, and I don't like it; it is too limiting. With interactive rebase, I can just keep working, go forward, go back, change my mind, try things, and when I'm all done and ready to push, I clean up by squashing it all into one commit as I showed you. – matt Dec 04 '20 at 03:29
  • Thank you for detailed answer, I will try this out – Paritosh M Dec 04 '20 at 20:31
  • Hey @matt What should I do after `save and close the editor`? Do I need to do `git push` after this? On `git status` it says `your branch has diverged and have 1 and 6 different commits each, respectively(use "git pull " to merger the remote in yours)` – Paritosh M Dec 04 '20 at 20:55
  • I still have push changes `to the open pr` to merge into `master branch` – Paritosh M Dec 04 '20 at 21:03
  • Well it’s very hard to rewrite history you’ve already pushed but you might just be able to push with force. But next time don’t do that! However that’s a different matter really. – matt Dec 04 '20 at 21:04
  • Ohh, I am confused, so what should I do next? – Paritosh M Dec 04 '20 at 21:05
  • I think you should ask this as a new question! Personally I would withdraw the PR and delete the remote branch and start over. – matt Dec 04 '20 at 21:06