So apparently when you are working on the same repo with like 30 other people, you will probably end up getting a whole bunch of merge conflicts all the time. The problem with merge conflicts and committing many times over and over is to fix the merge conflicts, you need to go through all of your commits and fix it everywhere all over the place. So you can reduce this workload by squashing all of your commits into a single commit, so you just need to fix the merge conflicts one time. Here’s how to do a git squash.
Checkout a new branch:
$ git checkout -b my_groovy_branch Branch my_groovy_branch set up to track remote branch my_groovy_branch from origin. Switched to a new branch 'my_groovy_branch'
Make some changes and then commit them:
$ echo "hello" > file.a $ git status On branch my_groovy_branch Your branch is up-to-date with 'origin/my_groovy_branch'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: file.a no changes added to commit (use "git add" and/or "git commit -a") $ git add file.a; git commit -m "commit message one"; git push origin my_groovy_branch [my_groovy_branch 20a269f] commit message one 1 file changed, 1 insertion(+) Counting objects: 7, done.
Now make some more changes and commit them
$ echo "hello again" > file.b $ git status On branch my_groovy_branch Your branch is up-to-date with 'origin/my_groovy_branch'. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: file.b no changes added to commit (use "git add" and/or "git commit -a") $ git add .; git commit -m "COMMIT MESSAGE TWO" [my_groovy_branch 546c7d4] COMMIT MESSAGE TWO 1 file changed, 1 insertion(+) Counting objects: 20, done.
We now have two commits. Lets squash the changes of both commits into a single commit:
$ git merge-base my_groovy_branch master c48321ac257c0694c275ab22a41698841d93f3d1 $ git rebase -i c48321ac257c0694c275ab22a41698841d93f3d1
After you run this command you’ll get a text editor which will ask you to choose which commit message you want to represent all of your squashed commits. Change your commit messages by changing the command keyword in front of the commit message to any other options you want.
pick 20a269f commit message one f 546c7d4 COMMIT MESSAGE TWO # Rebase c48321a..546c7d4 onto c48321a (2 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
After you save quit, you’ll see this:
[detached HEAD c4219bf] commit message one Date: Thu Mar 30 15:31:40 2017 +0200 2 files changed, 2 insertions(+) Successfully rebased and updated refs/heads/my_groovy_branch.
Then, to push it remotely, you need to force push it, if not you’ll see this error:
$ git push origin my_groovy_branch To https://firstname.lastname@example.org/scm/admin/hello.git ! [rejected] my_groovy_branch -> my_groovy_branch (non-fast-forward) error: failed to push some refs to 'https://[email protected]/scm/admin/hello.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.
Force push, just like Darth Vader would
$ git push origin my_groovy_branch -f Counting objects: 8, done. Delta compression using up to 4 threads. Compressing objects: 100% (8/8), done. Writing objects: 100% (8/8), 673 bytes | 0 bytes/s, done. Total 8 (delta 6), reused 0 (delta 0) remote: remote: Pull Request erstellen für my_groovy_branch: remote: https://github.com/hello/my_groovy_branch remote: To https://email@example.com/scm/admin/hello.git + 20a269f...c4219bf my_groovy_branch -> my_groovy_branch (forced update)