0

So there are many times that while working with git I follow this process:

  1. Clone a project and perform it's setup.
  2. The setup/install process creates new files which maybe configurational files specific to my environment (that aren't gitignored) or modifies existing "core" files in the process of the setup of the project. These changes/files are essential to run the project but should not be checked into the repo and were coded specifically that way so that these should NOT be checked in ever coz it maybe sensitive.
  3. I make a branch called feature-newfeaturename and then start my feature development. At this point there are several unstaged changes/files from step 2 and I work on top of that.

Now when I finish developing the feature, I want to checkin merely the changes I made excluding the files. Normally what I do is that at the end of step 2, I do a git diff > setup-changes.txt and then use that file as reference at the end.

When I'm done with my feature development, I literally do git checkout each-file-name-in-the-list, followed by git add . and git commit and then make the PR--this way it merely PRs in my changes only.

This works fine for now but isn't efficient and I feel there might be a better way?

And another issue is, sometimes my changes include changes to the core files which conflict with the changes made in step 2. So when I do a git checkout in the end, my changes would be erased and therefore I have to manually diff out the changes I made, do a checkout of the file with the original core (that excludes the setup changes) and then manually use the diff to find and add my changes alone.

Again this is counterintuitive and wish there is a better process. Is there? If so how?

To simplify this question, let me provide an example:

  1. There's a file main.txt which simply has the word 'apples' and is part of "first commit" in the main branch.
  2. Then I do a git checkout -b setup and add bananas which is the configuration changes and perform a commit.
  3. Then I do a git checkout -b feature from setup branch and add oranges making the file have apples, bananas and oranges. So the new feature is addition of oranges.

Now what I want to do is simply get the diff between first and last i.e. I want to be able to have just apples and oranges in my final commit and then push that as the second commit to my main branch.

Saifur Rahman Mohsin
  • 929
  • 1
  • 11
  • 37
  • 2
    Instead of `git add .` could you stage only the files you want in the commit? Using something like `git add -p` may make it easier to help decide what should be staged. Then, after your commit/PR, you just run something like git clean? – Dillon Hafer Jan 01 '21 at 08:12
  • 6
    Your workflow doesn't play nice with source control tools. The right thing to do would be to refactor out the things that need to change so that they don't need to be checked in (e.g. read user-specific from config file instead of reading from constants). – root Jan 01 '21 at 08:31

1 Answers1

0

I second @root's suggestion (which should probably be written as an answer).

That being said :
another way would be to actually commit the files right after step 2, as the first commit of your feature branch, and then either :

  • use git revert <that commit> at the end of your development branch (that would leave the initial commit and the revert in your branch's history)
  • use git rebase -i to "erase" this initial commit before pushing your branch
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • I found a [better solution](https://stackoverflow.com/a/1195209/997147) eventually that's based on this same concept. The only issue with this method is the conflict resolution issue. – Saifur Rahman Mohsin Jan 03 '21 at 11:10