How to really use git branches?

I know this is a noob question but still, how to use git branches?.

To edit a branch in our local repo we have to edit the files in the local folder also. So how to use another branch without editing the files in the project. Instead I want to edit it without changing the project folder. Such as when we use Github we can edit files only in one branch without changing the project folder i.e master branch. But in git we have to edit the files in our directory and then we have to commit to a branch. But the file will become changed in our local directory. I hope you understood what I meant.

Please do explain in simple terms and in detail.

The same as you would do on GitHub. You would need to create a new branch, make sure you’re on that new branch, then make commits on that new branch. GitHub is simply an online service that helps with storing, collaborating, etc. on Git repositories.

Here’s a PDF cheatsheet with all the commands you should need (under the Branch & Merge section).

1 Like

Even of I have to make commits, I should edit the files in my root directory right? I don’t want to edit the files in the root project directory for making a new feature or whatsoever.

Any changes you make on one branch will not be reflected in any other branch until your merge those commits onto the other branches. Therefore, what directory/files you edit does not influence on what branch your changes will be on, rather your current branch will.

2 Likes

That I understood. But the files in the project directory excluding the git files will be changed right??

Can you clarify what you are asking about?

If your entire project directory is a git repository, what I said above applies to all files in the directory.

I meant that for a git repo there is a .git folder which has all the commits and information about the branches. But to commit a change to a branch such as trial branch, I have to edit my file such as app.js in my project folder. But I don’t want to edit that file in the main project file to commit to a branch other that master branch.

So is there any way make separate project folders for each branch. Is it correct to use?

Making changes in your project folder is fine, those changes will only reflect on your current branch. Like you said, the .git folder is there to store information about the repo, it doesn’t limit the repo’s scope to the .git folder itself.

I don’t want to make changes in the project folder because it will be permanent for that file. Even if I checkout to another branch, the files in the project folder won’t change. I think that I am telling in a wrong way that’s why you didn’t understand what I meant…

Maybe there is a miscommunication here. As long as the project folder is a git repository and you are on the branch you want to be on, any changes you make will only be reflected on the branch you are on. None of the changes you made on this branch will affect the other branches. There is no need to make duplicates of your project folder for other branches. Does this clarify things?

I also suggest you go over this section of Codecademy’s Git course if you have PRO, it might clear some things up.

Consider this situation:

I have a node project. The main file is app.js. So I used git init and made it a git repo. Next I commit everything in this project to the master branch. Now I make a new branch named new-feature . Now I want to develop an extra feature such as number to bits converter. So to add this to the code I edit the app.js in VSCode. After I have added the feature I commit this to the new-feature branch. Then I decided I want to remove the feature from my file app.js itself. I can revert the commit for new-feature branch. But that doesn’t revert the changes I made to app.js in VSCode.

So I want to know if there is a way to work with the new-feature branch without changing app.js in the main project folder???

If you reverted the commit in new-feature, then the feature should be removed from app.js. If it’s not, make sure you’re looking at app.js on the new-feature branch. If you want to remove the changes on your master branch too, simply merge new-feature into master.

If I’m understanding your question correctly, yes you can. Once again, branches are separate from one another. Changing things in one branch will not affect other branches.

1 Like

Oh so when we work checkout a branch, the project files will become that of that branch.

So if we delete app.js file in new-feature branch, then when we look at the project folder in file explorer , app.js won’t be there. But when we checkout to master and then we look at the project folder in file explorer, app.js would be present.

Am I right?

Yes.

Thank you very much. Now I understood.

1 Like