简体   繁体   中英

How do I take a bunch of my git local commits and merge them into one commit?

In my company we have recently moved git. While in the process of working in this environment, I end up making a bunch of local commits (in my local branch). In order to keep my code updated I make pulls. Now the end result is that when I do a git log, I find my commits as well as other developers' commits in that log. Before pushing my commits to remote I would like to gather a all of my local commits and merge them into one commit (inorder to reduce noise in the log) and push it to remote. How do I do that in git?

The simplest thing to do is, once you've done a git pull and you've resolved any conflicts and are all up to date, reset your branch pointer to the latest remote commit (I've assumed origin/master ) and then make a new commit.

git reset --soft origin/master
git commit

The new commit will be all of your changes as a single new commit on top of the remote's master commit. You can then push this commit to the remote. Use git show to verify the commit before pushing.

Note: This assumes that you've only pulled from the one shared branch ( master ) or that any other shared branches that you've pulled have already been merged into the remote's master .

git fetch <remote name>
git rebase -i <remote name>/<branch name>

For more details on how to use rebase -i (interactive rebase), see this guide:

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

Note that rebasing/squashing doesn't always play nicely with merge commits. Rebasing will often move your commits after the things you merged in, so the merge commits may go away and you won't run into problems, but if something you merged in isn't in the remote branch already, you'll need to be careful.

You should change the approach. While developing on your local branch you should not do any pools (or fetches). Rather try to update "main" development branch and then rebase (git rebase) your development branch with "main" branch from time to time.

Right now maybe git cherry-pick could be what you are looking for.

You can also try to manually reset (git reset --soft) to your first commit on given (your development)) branch and then choose only your changes which next commit as one commit.

If you will have only yours commits on your branch issue is much simpler: you can do squash those (git squash). Some method, for not creating too many commits on your development branch is also ammending your commits (git ammend).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM