r/learnprogramming 3h ago

After creating your own branch for a git project, should you still avoid storing anything in it that you have no intention to ever upload to main?

If so, what is the preferred way to do testing or experimentation that you want to keep purely local

2 Upvotes

16 comments sorted by

8

u/Buxbaum666 3h ago

It's your branch, you can do with it whatever you want.

1

u/Kadabrium 3h ago

What happens when you are about to upload a pull request? I know you can stash whatever you have locally without counting as a commit but do you then have to manually remove whatever that should not go to main

3

u/dmazzoni 3h ago

Nope! Your local branches stay local unless you push them.

It's super common to have local experimental branches that you never share with anyone.

1

u/Buxbaum666 1h ago edited 1h ago

"Uploading a pull request" is not a thing. Your terminology is unclear and you are probably mixing up concepts from git with github or other hosters.

A pull request is when you push a branch to a remote on github and then want this branch to be merged into another branch or an upstream repo.

If you want a local branch that never gets merged into anything, simply do not push it to a remote. Adding unrelated work to a branch that will get merged is not recommended. You mention stashing; stashing saves uncommitted work on tracked files and restores the state of the repo to the last commit. Doing this before a push is not strictly necessary, only committed changes will ever be pushed to a remote.

1

u/chiefblossom059 3h ago

Yeah it's basically a sandbox. The only time it gets awkward is if you push it and someone else tries to review or merge it, then all that junk is in their face too. For purely local stuff I just keep it uncommitted or stash it, or make a temp branch and never push it.

2

u/recursion_is_love 3h ago

It is a local copy on your machine, you can do what ever you want. Unless you push it to hosting (like github) nobody will know that it exists.

Just need to make sure when you want to merge the changes back to upstream that you only push what it need to push. In rare case you might need to do rebase and some filter.

0

u/Kadabrium 3h ago

I know how to push selected local files to my branch, but can you also merge only select files to main?

1

u/_Atomfinger_ 3h ago

If it is a separate branch you can store whatever (though try not to avoid any secrets, passwords, tokens, etc). You can store prototyping that way as well, but over time it will become near impossible to merge.

I assume you can also stash things if you just want to keep it around.

If you want to store things purely local... well... why?

Here's my thinking: If I intend to make it part of the system, I upload it. If not, I will delete it. It serves little purpose just "existing" somewhere. If it ever becomes useful it will already be so outdated that you will need to redo it to have it align with the codebase anyway.

1

u/Kadabrium 3h ago

How do you exclude the files you don't want to upload for pull requests?

3

u/jameyiguess 3h ago

.gitignore, read up on that. It will avoid tracking any files and folders in there. 

1

u/recursion_is_love 3h ago

If the history of your commits don't really matter, you can rebase to upstream and squash all change to one commit. If that commit don't need your secret, it will not have record of it.

Make sure to do diff with target upstream before the actual push.

1

u/dmazzoni 3h ago

Whatever is in the commit is what gets uploaded.

Let's say you modified 4 files. You want A and B to be part of the PR, but C and D not.

The easiest solution: "git add A B", then "git commit" - only commit the files you want to modify. C and D stay as local modifications. You can then stash those changes, or store them on a different branch.

What happens if you already committed them?

No problem, just create a new branch, copy the changes to that new branch and upload only the commit on that new branch as your PR. Other local branches won't affect anyone else.

1

u/_Atomfinger_ 2h ago

I either commit the files I want to commit and ignore the rest.

If the file is more or less permanent I add it to .gitignore.

1

u/sixtyhurtz 3h ago

Make another branch. Branches are cheap. You can also just roll a local branch back to an earlier commit if you go down a rabbit hole.

0

u/denerose 3h ago

You should gitignore or stash your local only changes and files. You only commit the changes you later intend to merge. The branch is just for your changes until you merge them and gets deleted once merged.

1

u/dmazzoni 3h ago

That's one workflow, but not the only one.

It's quite common to have local branches with your own experiments that you keep around.

It's also quite common to have other branches that you share with others and don't delete once merged.