kalai selvan
kalai selvan

Reputation: 15

How to work in same git directory in local without cloning branch for each task?

We are following agile where we have to create a new branch for each task. The challenge here is the project I'm working on is huge, and cloning a new branch each time takes a lot of time. How can I switch to a new branch each time without cloning the branch manually and push the changes to that branch and so on.

As of now I'm cloning a new branch ( 20 + GB ) each time and pushing changes to that branch and the cycle continues for each sprint.

I'm expecting a way to create a new branch locally and do changes, then push those changes to the repository where we raise a pull request and merge it to the "develop" branch.

Upvotes: 1

Views: 83

Answers (2)

IMSoP
IMSoP

Reputation: 97818

I suspect you have come from a background using a different version control tool, or no tool at all, and associate "new branch" with "new local copy of the code". That's not what git means by "branch".

As you say, you start working in git by "cloning" some other copy of that repository (usually, but not necessarily, hosted somewhere like GitHub or GitLab), like this:

git clone git@somehost:SomeThing/AwesomeApp /home/me/my-projects/AwesomeApp

Inside /home/me/my-projects/AwesomeApp there are now two things:

  • A full clone of the entire history of all branches of the remote repository you cloned
  • A working copy based on the most recent code in some branch of that repository. The history may contain many existing branches, and you can point your working copy at any one of them.

For most workflows, this is everything you need, forever; you don't need to run git clone again unless you have a new computer you want to work on.

When people say "create a branch for each task", they mean that inside /home/me/my-projects/AwesomeApp you run git commands to change the "current branch" of the working copy. When you commit, you store changes from your working copy to a new commit on whatever current branch you've chosen or created.

Git is especially good at keeping these branches "light" - in fact, all a branch stores in git is a pointer to its latest revision, from which the rest of the history can be traced through the main "revision graph".

When you're done, all git needs to push back to the shared repository is information about the new commits you've created (dates, messages, your name, etc), and the files you've changed.

I won't bother listing all the commands you might need here - there are hundreds of git tutorials online already that will explain it better than I could.

Upvotes: 2

Mureinik
Mureinik

Reputation: 311563

You certainly shouldn't clone the repository for each branch.

You can create a new branch using git branch and then git checkout to it, or even easier, use the git checkout -b option:

git checkout -b my_new_feature

# perform the changes needed for the feature

git add file_you_changed.txt other_file_you_added.txt # etc...

git commit -m 'Added a new feature' # or just git commit and use the editor for the commit msg

git push origin my_new_feature

Upvotes: 1

Related Questions