简体   繁体   中英

Git keeping remote branches (origin) clean

I'm working on a solo project, following advice from http://nvie.com/posts/a-successful-git-branching-model/

I released version 1 of my code by using the following:

git checkout master
git merge --no-ff release-1.0

into my master branch, I then issued:

git push

Afterwards, I noticed that the remote branch (origin) has all the local commits and branches, even ones that I that I considered private.

How do I keep my private local history, and still keep origin "clean" of them after a push? It seems that there is a lot of value in keeping all the history of the code in my local repo, but correct me if I'm wrong!

If it's not possible to keep separate histories after a push, how can I clean up my local history without losing information that's worth keeping?

Whenever you push something up on git, all your history till that point and all the commits are pushed with it.

If however, you want to say clean up the commits a bit, the easiest method is using git rebase -i and then squashing your commits.

So let's say you've just made a few small commits, and you want to make one larger commit out of them.

Your commit list :

在此处输入图片说明

The last 4 commits would be much happier if they were wrapped up together, so let's do just that through interactive rebasing:

$ git rebase -i HEAD~4

pick 01d1124 Adding license
pick 6340aaa Moving license into its own file
pick ebfd367 Jekyll has become self-aware.
pick 30e0ccb Changed the tagline in the binary, too.


# Rebase 60709da..30e0ccb onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

You have plenty of options available to you from this screen, but right now we're just going to squash everything into one commit. So, changing the first four lines of the file to this will do the trick:

pick 01d1124 Adding license
squash 6340aaa Moving license into its own file
squash ebfd367 Jekyll has become self-aware.
squash 30e0ccb Changed the tagline in the binary, too.

Basically this tells Git to combine all four commits into the the first commit in the list. Once this is done and saved, another editor pops up with the following:

# This is a combination of 4 commits.
# The first commit's message is:
Adding license

# This is the 2nd commit message:

Moving license into its own file

# This is the 3rd commit message:

Jekyll has become self-aware.

# This is the 4th commit message:

Changed the tagline in the binary, too.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   LICENSE
#   modified:   README.textile
#   modified:   Rakefile
#   modified:   bin/jekyll
#

Git allows you to modify the new commit's message based on the rest of the commits involved in the process. Edit the message as you see fit, then save and quit.

Created commit 0fc4eea: Creating license file, and making jekyll self-aware.
 4 files changed, 27 insertions(+), 30 deletions(-)
  create mode 100644 LICENSE
    Successfully rebased and updated refs/heads/master.

And if we look at the history again…

在此处输入图片说明

This is one of the easiest method of cleaning up a bit of history.

The complete history of a commit is part of the commit, so if you push a commit you will see the exact same thing on the remote.

To clean up and tidy your history before pushing, you can have a look at http://sethrobertson.github.io/GitPostProduction/gpp.html .

As Sankalp Singha mentioned already, you need to squash your commits. There is an easy way – merge your branches with private history using:

git merge --squash <dev-branch>

This will create a new commit containing the same data as the dev-branch but not the list of the individual commits of that branch.

The rebase answer is a good way to make your working commits not visible when you push.

However you also want to keep the record of your working commits in your local repo.

The easy way to do this is to keep a development branch on your local repo, and create a separate branch for the push. Rebase the separate branch before you push it.

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