One way you can temporarily remove your feature that predates your Git repo history is to make a commit that removes the feature. Then when you want to add the feature back in, simply revert the commit that took it out. This will do a reverse patch, meaning it will apply the changes in reverse, which will effectively re-add the feature:
git revert <sha of commit that removed the feature>
If you would like to make sure that you can easily re-add the feature later by keeping it in sync with changes to the code in the meantime, you can create a separate feature branch immediately after you remove it, and then just treat that branch like any other feature branch, and keep it in sync by frequently rebasing it against master
(or a develop
branch, if that's the way you want to do it), resolving conflicts as you go.
So basically, you would want to do something like this (it won't matter much if you're using a GitHub Flow or a Git Flow branching strategy, both use the concept of feature branches that eventually get merged into a main-line of development. For simplicity, I'll use GitHub Flow in this example):
# On master branch
git commit -m "Remove feature X" # Creates commit 1234567...
# Now make feature branch
git checkout -b saved-feature
# Immediately put the feature back in the feature branch
git revert 1234567
# When you want to sync branch with master, just use rebase.
# Rebase allows you to sync frequently, since it doesn't
# leave behind a bunch of merge commits.
#
# From the feature branch:
git rebase master # Resolve any conflicts as needed.
# N commits later, you decide it's time to merge the feature
# back in. You can use fast-forward or non-fast-forward merge,
# it's up to you.
#
# Using fast-forward merge with master checked out (assuming
# feature branch was just rebased onto master):
git merge saved-feature
# Or forcing a merge commit, if that's what you want:
git merge --no-ff saved-feature
Assuming you've kept saved-feature
in sync frequently with master
(or develop
, if that's what you use), resolving conflicts as you go, you should have no problems merging the feature back in.
Documentation for reference: