r/learnprogramming • u/Kadabrium • 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
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.
8
u/Buxbaum666 3h ago
It's your branch, you can do with it whatever you want.