GIT – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Mon, 09 Jan 2023 02:13:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Git Change Remote URL in Local Repository https://tecadmin.net/git-change-remote-url-in-local-repository/ https://tecadmin.net/git-change-remote-url-in-local-repository/#respond Sun, 08 Jan 2023 17:59:55 +0000 https://tecadmin.net/?p=33646 Git is a distributed version control system that is widely used for tracking changes in source code during software development. It allows developers to collaborate on projects and keep track of their changes without the need for a central repository. Sometimes, it may be necessary to change the URL of a remote repository in a [...]

The post Git Change Remote URL in Local Repository appeared first on TecAdmin.

]]>
Git is a distributed version control system that is widely used for tracking changes in source code during software development. It allows developers to collaborate on projects and keep track of their changes without the need for a central repository.

Sometimes, it may be necessary to change the URL of a remote repository in a local Git repository. This can happen if the remote repository has been moved to a new location, if you want to use a different remote repository for your local project or if you want to change authentication methods like HTTPS to Git or vice versa.

There are a few different ways to change the remote URL for a Git repository, depending on your needs. Here are the steps for each method:

Method 1: Using Git Bash or Command Prompt

The first method used the command line interface to manage the git repository. A majority of users manage git repositories using command line clients. Follow the below steps to change the remote git URL:

  1. Open a terminal window (Git Bash on Windows, or any terminal emulator on macOS or Linux).
  2. Change to the directory that contains the local Git repository.
  3. Run the following command to view the current remote repository URL:
    git remote -v 
    

    This will display a list of all the remote repositories that are linked to your local repository, along with their URL.

  4. To change the URL of a specific remote repository, use the following command:

    # Syntax
    git remote set-url <remote> <new_url>

    Replace <remote> with the name of the remote repository (usually origin), and <new_url> with the new URL that you want to use.

    For example, to change the URL of the “origin” repository to “https://new.url/repo.git”, you would run the following command:

    git remote set-url origin https://new.url/repo.git 
    

    You can also prefer to use SSH URL for your git repository.

  5. Verify that the URL has been changed by running the `git remote -v` command again. You should see the new URL listed for the specified remote repository.

Method 2: Using the Git Configuration File

If you are not confident with the command line interface, this is another quick and easier method to change the remote git URL by editing the configuration file:

  1. Open the “.git/config” file in a text editor. This file is located in the root directory of your local Git repository.
  2. Find the section that corresponds to the remote repository that you want to change the URL for. It will look something like this:
    [remote "origin"]
        url = https://old.url/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

  3. Replace the url value with the new URL that you want to use. For example:

    [remote "origin"]
        url = https://new.url/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

  4. Save the “.git/config” file and close it.
  5. Run the `git remote -v` command to verify that the URL has been changed.

Method 3: Using the Git GUI

If you prefer a graphical interface, you can also change the remote URL using a Git GUI (graphical user interface) tool. These instructions may differ depending on the GUI client.

  1. Open the Git GUI tool and select the “Repository” menu.
  2. From the “Repository” menu, select “Repository Settings…”.
  3. In the “Repository Settings” window, select the “Remote” tab.
  4. Select the remote repository that you want to change the URL for, and click the “Edit” button.
  5. In the “Edit Remote” window, enter the new URL for the remote repository in the “URL” field.
  6. Click the “Save” button to apply the changes.
  7. Close the “Edit Remote” window and the “Repository Settings” window.

That’s it! The remote URL for your local Git repository should now be changed to the new URL that you specified.

Wrap Up

It’s important to note that changing the remote URL will not affect any of the local code in your repository. It only changes the location of the remote repository that your local repository is linked to. If you want to push your local changes to the new remote repository, you will need to use the git push command as usual.

I hope this helps! Let me know if you have any questions or need further clarification on any of the steps.

The post Git Change Remote URL in Local Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/git-change-remote-url-in-local-repository/feed/ 0
How to Create Branch in Git Repository https://tecadmin.net/create-branch-in-git/ https://tecadmin.net/create-branch-in-git/#respond Sun, 24 Jul 2022 06:50:38 +0000 https://tecadmin.net/?p=8199 Git is an amazing tool, and it has multiple use cases. You can use Git to manage source code, user stories, bug reports, and so much more. As a result of its versatility, there are many different ways to use Git in different contexts. This article explains the usage of Git in the context of [...]

The post How to Create Branch in Git Repository appeared first on TecAdmin.

]]>
Git is an amazing tool, and it has multiple use cases. You can use Git to manage source code, user stories, bug reports, and so much more. As a result of its versatility, there are many different ways to use Git in different contexts.

This article explains the usage of Git in the context of a software project with multiple developers working on different features simultaneously. It will help you learn how to create a branch in a Git repository for better version control and collaboration among team members. Let’s get started!

What is a Branch in Git Repository?

A branch in Git is an independent line of development. They allow developers to work on multiple features or bug fixes in parallel without the risk of conflicting code when those features are eventually merged back into the main codebase. Branching in Git is used to see the progress of code with multiple features without interrupting the workflow.

Alternatively, a branch can be used to fix bugs on a specific feature without interrupting or changing the progress of other features. Branching in Git allows multiple developers to collaborate with each other with ease because they can work on different features or bug fixes and then merge them when they’re ready. This allows for seamless collaboration among team members and reduces the risk of conflicting branches and code.

Create a Branch in Git Repository

A branch can be created when you have a specific feature or fix in mind. This allows you and your team members to focus on a single change at a time to reduce the risk of conflicting and broken code. The steps below will explain how to create a branch in Git Repository. To create a branch, follow these steps.

  • Open the Terminal or Command Prompt and navigate to your git repository folder:
    cd /path/to/git_repository/ 
    
  • Create a new branch by entering the following command.
    git checkout -b stage1 
    
    Output
    Switched to a new branch 'stage1'
  • The above command will switch immediately to the new branch. Later you can switch branches by entering the following command
    git checkout stage1 
    

That’s it. You have successfully created a new branch in your local Git repository.

Push a Local Branch to Remote

Once you have created a new branch in your local repository, You may need to push it to remote also. Let’s push your newly created branch ‘stage1‘ to the remote git repository. To push make sure you are on the correct branch.

git checkout stage1 

Now use the following command to push your branch ‘stage1’ to the remote git repository.

git push origin stage1 
Output
Username for 'https://github.com': your_git_user_name Password for 'https://your_git_user_name@github.com': Total 0 (delta 0), reused 0 (delta 0) To https://github.com/tecrahul/firstrepo.git * [new branch] stage1 -> stage1

Merge Changes from One Branch to Another

Merge changes from one branch to another when you’re ready to combine the code from one branch with the code from another branch. This is done to combine the development of two or more independent branches into a single feature or fix. Merging branches together can be easy or difficult, depending on the situation. When branches have diverged significantly, it can be difficult to merge them without introducing bugs. When branches have diverged only a small amount, merging is easy. It’s important to be careful when merging branches together because changes can be overwritten.

When it comes to merging branches in Git, you can create pull_request (Github) or merge_request (Gitlab) or similar tools in other Git providers.

Creating a new branch in Git repository

Conclusion

In this article, you learned how to create a branch in Git Repository. You also learned that a branch is an independent line of development, and they allow developers to work on multiple features or bug fixes in parallel without the risk of conflicting code when those features are eventually merged back into the main codebase.

You also learned the steps to create a branch in Git Repository, commit changes on the branch, and merge changes from one branch to another. You can use branching to see the progress of code with multiple features without interrupting the workflow. Alternatively, a branch can be used to fix bugs on a specific feature without interrupting or changing the progress on other features.

Now that you know what a branch is and how to create one, you can better manage your code and collaborate with other developers. With branching, you can break down your code into smaller chunks, and team members can focus on one task at a time without breaking the entire project.

The post How to Create Branch in Git Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/create-branch-in-git/feed/ 0
How To Add a Git Remote Repository https://tecadmin.net/git-add-remote-repository/ https://tecadmin.net/git-add-remote-repository/#respond Thu, 04 Nov 2021 05:47:08 +0000 https://tecadmin.net/?p=28310 Every time you clone a Git repository, you are actually downloading your project locally. This means that you will have a local copy of the Git repository tied to your project. Local copy that was created this way will be automatically connected to the remote repo. But what if you created a local copy first? [...]

The post How To Add a Git Remote Repository appeared first on TecAdmin.

]]>
Every time you clone a Git repository, you are actually downloading your project locally. This means that you will have a local copy of the Git repository tied to your project. Local copy that was created this way will be automatically connected to the remote repo. But what if you created a local copy first? In that case, you will have to add a Git remote repository. Read on and learn how to do that.

How to Add Git Remote

You will notice word origin In an example of the syntax that you see below. Origin is not a flag nor does it have to be called “origin” in the first place. That is just the term to name your remote connection. You can name your remote connection however you want, but “origin” is a commonly agreed term when it comes to repository naming.

Let’s see how we can add a Git remote repository. You’ll need two things. Name for your remote and URL of the remote repository. URL will be generated after you create a remote repository on GitHub.

In order to add the remote repo, use the syntax below:

git remote add origin https://github.com/tecrahul/helloworld.git 

Please take note that “origin” can be named to whatever you prefer and the URL that we used is just an example. You will have to add the URL of your own repository.

Everyone who works in the IT industry wants to check if something that was done actually works. You’ll probably want to do the same after you add a Git remote repository. Luckily, this can be done quickly and efficiently. Just run the following:

git remote -v 

The output of that should look like the one below.

Output
origin https://github.com/tecrahul/helloworld.git (fetch) origin https://github.com/tecrahul/helloworld.git (push)
Example: Git Add Remote Origin
Example: Git Add Remote Origin

This means that everything is good. Always run this just to be sure.

How to Add Remote with Tower Git Client

If your machine is running on Mac or Windows operating system, there is a high chance that you are using Tower Git. Since Tower Git has a GUI through which repositories are being manipulated there is a simple box in which you need to enter necessary info. An example of it can be found below.

Tower: Git Add Remote Origin
Tower: Git Add Remote Origin

And basically, that’s all there is to it. Hopefully, you’ll find this article useful in your future interactions with Git.

The post How To Add a Git Remote Repository appeared first on TecAdmin.

]]>
https://tecadmin.net/git-add-remote-repository/feed/ 0
How to Git Reset to Head https://tecadmin.net/git-reset-head/ https://tecadmin.net/git-reset-head/#respond Wed, 03 Nov 2021 02:57:07 +0000 https://tecadmin.net/?p=28303 Git reset is a process that is pretty similar to undoing the recent Git commit that we covered in one of the previous tutorials. However, in this one, we will cover Git reset to Head in more depth. We will check what the revert command does and what is mixed reset. Read on and find [...]

The post How to Git Reset to Head appeared first on TecAdmin.

]]>
Git reset is a process that is pretty similar to undoing the recent Git commit that we covered in one of the previous tutorials. However, in this one, we will cover Git reset to Head in more depth. We will check what the revert command does and what is mixed reset. Read on and find some tips and tricks about Git reset.

Reset Last Git Commit to HEAD

In our previous article, we used git reset –soft HEAD~1 to undo the last commit without losing changes that were uncommitted. Additionally, we used git reset –hard HEAD~1 to undo everything, even changes that we made locally. But what to do when you want to reset the last Git commit to HEAD, keep the changes that you did in your repo directory, but you don’t want to keep them in the index? Here’s your answer.

If you stumble upon situations like the one that we described above, you have to use –mixed flag. Here’s an example.

Let’s say that we added some sort of file with our last commit.

git log --oneline --graph 
Output:
d445900 (HEAD -> master) Added a new file named "test_file" 61t6ll5 Second commit 4096r12 Initial repository commit

Now let’s run Git reset command with --mixed flag.

git reset --mixed HEAD~1 

What the command above did is the following. It removed the last commit, which in this case was file addition and it removed it from the Git Index, but the file remained in the directory where you are currently located ( which is your local repository directory ). So flag --mixed is actually a combination of --soft and --hard Git reset options. That is why it’s called mixed in the end.

How to Use Git Revert Option to Reset

Revert is a bit different than reset. The main difference is that reset sets a new position for HEAD while revert actually reverts the whole commit which is specified. Let us show you an example of how this actually works.

git log --oneline --graph 
Output:
d445900 (HEAD -> master) Added a new file named "test_file" 61t6ll5 Second commit 4096r12 Initial repository commit

So again, the last thing that we committed was file addition. Let’s run the revert command now.

git revert HEAD 

Your default text editor will open now and the output will look like this.

Revert “Added a new file named test_file”

This reverts commit 5e998t74du5h4z4f.

# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is ahead of ‘origin/master’ by 6 commits.
#    (use “git push” to publish your loacl commits)
#
# Changes to be committed:
#                deleted:         test_file
#

Once you are done, exit the text editor, and a new message will pop up.

Output:
[master d445900] Revert "Added a new file named test_file" 1 file changed, 1 deletion(-) delete mode 100644 test_file

That’s it! You successfully competed Git reset to HEAD action with the revert option.

The post How to Git Reset to Head appeared first on TecAdmin.

]]>
https://tecadmin.net/git-reset-head/feed/ 0
How to Delete a File on Git https://tecadmin.net/git-delete-files/ https://tecadmin.net/git-delete-files/#respond Fri, 29 Oct 2021 08:41:08 +0000 https://tecadmin.net/?p=28250 During the development process bunch of files will be added to your repository. On the other hand, a bunch of them will be removed. Either because they are no longer needed or because they became surplus to the requirements. Deletion of something is easy in the IT industry, it tends to happen accidentally and when [...]

The post How to Delete a File on Git appeared first on TecAdmin.

]]>
During the development process bunch of files will be added to your repository. On the other hand, a bunch of them will be removed. Either because they are no longer needed or because they became surplus to the requirements. Deletion of something is easy in the IT industry, it tends to happen accidentally and when we least want it to happen, right? The same is with file deletion on Git. But to avoid all the confusion between deletion of the file from the repository or from the filesystem, in this tutorial we will learn how to delete files on Git. That way we can be sure that accidental removal of the files will become thing of the past.

How to Delete a File on Git – Repository and Filesystem

Git command for file deletion is pretty much similar to the command that is being used on all Unix distributions. Each time you delete a file on git, you have to commit and push your changes. We’ll show you how to do that in the example below.

First and foremost, let’s remove the file.

git rm test_file 

The output will look like the one below.

Output
rm ‘test_file’

What this did is that it removed the file from the repository and the filesystem. The next step is to commit this change. Here’s how you can do that.

git commit -m “Removed test_file from Git repository and filesystem” 

And the last step is to push the change to your remote repository. Simple Git push will take care of that.

git push 

Congratulations! The file called “test_file” has successfully been removed. I just want to point out that the file that we used was just an example. The same goes for the commit line in quotation marks. Both filename and that line will have to be the actual file that you want to remove and commit line which fits the action that you just performed.

How to Delete File on Git – Recursively

This one is identical to the one that would be used on Unix-based distributions as well. Same command, same philosophy, with the addition of the “-r” flag.

Action like this one is extremely useful when you need to remove the whole directory, or even several directories at once. The example below shows how to recursively remove the directory.

git rm -r dir_for_testing  

Again, we have to commit the change.

git commit -m “Removed dir_for_testing from Git repository” 

And finally, push it.

git push 

What we did with these three commands is that we actually removed the directory called “dir_for_testing” along with all the content of it, and that includes subdirectories.

NOTE: Be extremely careful when you are removing something recursively! This can cause damage beyond repair in your repository. Always double-check your work and syntax which is being used.

How to Delete File on Git – Just Repository

If you are facing a scenario in which you want to delete from your Git repository, but you want to keep it in the filesystem, you’ll have to use the “–cached” flag. So same syntax, same process, just add the flag.

git -rm --cached test_file 
git commit -m “Removed test_file from Repository” 
git push 

Conclusion

We hope that you learned something new today and that you will put it to good use. I’ll point out one more that it is crucial to always check your syntax. You don’t want to remove something by accident.

The post How to Delete a File on Git appeared first on TecAdmin.

]]>
https://tecadmin.net/git-delete-files/feed/ 0
How to Git Stash Changes https://tecadmin.net/git-stash-changes/ https://tecadmin.net/git-stash-changes/#respond Wed, 27 Oct 2021 15:08:00 +0000 https://tecadmin.net/?p=28241 Ever had a need to temporarily store changes that you made to your code? Without committing it? For example, you were in the middle of branch editing and someone asked you to collaborate on a different one? If you didn’t, you most likely will somewhere down the road in your developer career. At that point, [...]

The post How to Git Stash Changes appeared first on TecAdmin.

]]>
Ever had a need to temporarily store changes that you made to your code? Without committing it? For example, you were in the middle of branch editing and someone asked you to collaborate on a different one? If you didn’t, you most likely will somewhere down the road in your developer career. At that point, it will be extremely useful to know how to use git stash. With the help of that command, you can temporarily store your current work to jump onto something else. Read on and find out how to Git stash changes.

How to Use Git Stash Command to Temporarily Save Your Changes

Okay, so picture this scenario. You are currently working on a piece of code that should bring a new feature to the application. All of a sudden, you urgently have to assist one of your co-workers with a bug that they found in a completely different branch. The first thing that you would want to do is to check which files you recently modified in the branch on which you are currently working. You can do that with a simple command.

git status 

This command will show you modified files. Below, you can find an example of the output.

Output:
modified: app_layer.php modified: readme.txt modified: functions.php

Of course that you don’t want to lose your work on these files, but on the other hand you can’t just commit them. And that is where git stash shines!

The command itself is extremely easy and straightforward. An example is below.

git stash 

The output will look similar to the one below.

Output:
Saved working directory and index state WIP on master: 3tjaq12w Implement the new login box HEAD is now at 3tjaq12w Implement the new login box

And that’s it! All your work on the current branch is saved in some sort of clipboard. Feel free to start working on whatever popped up in the meantime.

Speaking of pop, here’s how you can return to the place where you left off once you are ready to continue. Simply type this into your terminal.

git stash pop 

After the command above, you will be returned to your last saved state.

Conclusion

You’ll agree with us when we say that this is a very simple Git command to learn and it is of real value to having knowledge of it. We certainly hope that you’ll find a good use for what you learned today. Remember, developing is a never-ending course when it comes to learning.

The post How to Git Stash Changes appeared first on TecAdmin.

]]>
https://tecadmin.net/git-stash-changes/feed/ 0
How to delete Git tags https://tecadmin.net/how-to-delete-git-tags/ https://tecadmin.net/how-to-delete-git-tags/#respond Mon, 25 Oct 2021 08:41:03 +0000 https://tecadmin.net/?p=28220 Tags work as an additional identifier for a particular incident. And in the case of Git, Tags are used as the reference points in your development workflow and it denotes special events like a new version release or new commit. You can create a new tag to give a reference for your newly launched version. [...]

The post How to delete Git tags appeared first on TecAdmin.

]]>
Tags work as an additional identifier for a particular incident. And in the case of Git, Tags are used as the reference points in your development workflow and it denotes special events like a new version release or new commit. You can create a new tag to give a reference for your newly launched version.

We use tags for future reference of our previous releases and commits. And we can create and delete as per our convenience.

  1. To delete a tag from a local repository, We can use the “git tag -d” command followed by the tag name you want to delete. For example:
    git tag -d [tagName]
    

    So if you want to delete a tag v1.4 then you can execute:

    git tag -d v1.4 
    
  2. To delete tags from the remote repository, Execute the following command:
    git push origin --delete origin [tagName] 
    
  3. You can also use the git push command to delete a tag by specifying the tag name by refs syntax.
    git push origin :refs/tags/[tagName] 
    
  4. If you want to check if your tags have been deleted or not then you can list them all using “-l”
    git tag -l 
    

The post How to delete Git tags appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-delete-git-tags/feed/ 0
How to Clone a Git Repository Into a Specific Folder https://tecadmin.net/git-clone-into-a-specific-folder/ https://tecadmin.net/git-clone-into-a-specific-folder/#respond Thu, 14 Oct 2021 04:22:52 +0000 https://tecadmin.net/?p=28114 The process of git repository cloning is initiated on a daily basis in the DevOps world. But, if you simply issue a git clone command, the cloning process will create a new directory for the repository. In this tutorial, we will help you learn how to clone a git repository into a specific folder. Clone [...]

The post How to Clone a Git Repository Into a Specific Folder appeared first on TecAdmin.

]]>
The process of git repository cloning is initiated on a daily basis in the DevOps world. But, if you simply issue a git clone command, the cloning process will create a new directory for the repository. In this tutorial, we will help you learn how to clone a git repository into a specific folder.

Clone a Git Repository Into a Specific Folder

General-purpose of git cloning is to create a local copy of the remote repository. However, cloning without specifying the exact directory will create a new one. Quite often, this won’t be very convenient and it could potentially lead to a lot of clutter residing on your server.

That is why specifying the directory to which you want to clone is a useful thing to learn.

Syntax – The command for this operation should look like this:

git clone [repo-url] [path/to/the/directory]

Here’s what we did. First, we initiated the git clone command. After that, we defined the URL of the repository that we want to clone. And lastly, we defined the full path to the directory to which we want to clone.

Let’s see how would that look in practical use.

git clone https://github.com/tecrahul/helloworld.git  /home/devops/dev-project 

The command that is shown in the example above will clone helloworld.git repository into a dev-project directory that resides within the DevOps user.

Please take note that you have to replace the git repository URL and destination directory with the actual names of your repo and directory. The ones used above are just an example.

And that’s all there is to it! Hopefully, you learned how to clone a git repository into a new directory and you will put this command to good use in your future DevOps endeavors.

The post How to Clone a Git Repository Into a Specific Folder appeared first on TecAdmin.

]]>
https://tecadmin.net/git-clone-into-a-specific-folder/feed/ 0
How to Git Remove File but Keep Local Version https://tecadmin.net/git-remove-file-but-keep-local/ https://tecadmin.net/git-remove-file-but-keep-local/#respond Mon, 09 Aug 2021 07:10:02 +0000 https://tecadmin.net/?p=28074 In a scenario where you are working with an application which files contain some sensitive data, you most likely don’t want to push code to a remote repository. The best practice, in that case, is to remove the file from the git. You are in the right place In case that you want to keep [...]

The post How to Git Remove File but Keep Local Version appeared first on TecAdmin.

]]>
In a scenario where you are working with an application which files contain some sensitive data, you most likely don’t want to push code to a remote repository. The best practice, in that case, is to remove the file from the git. You are in the right place In case that you want to keep the file locally. In this tutorial, you will learn how to git remove files but keep the local versions.

Git Remove File – Keep Local Version

  • In a situation where the file hasn’t been committed or pushed to a remote repository, use the command below.
    git reset {fileName} 
    
  • If the file has already been committed or pushed to a remote repository, tracking for it can be removed. In that case, your command will look like this.
    git rm --cached {fileName} 
    

Git Remove Directory – Keep Local Version

What about the directory removal? You’ll use pretty much the same syntax. The only difference is that you will append it with the -r option. Here’s an example

git rm --cached -r {directoryName} 

Keep in mind that in both cases terms within brackets have to be changed with the actual file or directory name that you want to remove.

Now here’s one useful trick. Even though you removed the file or directory with the commands above, Git will still try to track it. Additionally, if you accidentally commit or push that certain file or directory in the future, it will end up in a remote repository again. To avoid that, add the full path to the file/dir in question to the .gitignore file. That way you’ll make sure that it won’t end up in the remote repo again one way or another.

And we’ll wrap up this tutorial with that information. Put what you learned about git remove file but keep local to good use when you are dealing with sensitive information in your repositories. Security and data sensitivity are both very important!

The post How to Git Remove File but Keep Local Version appeared first on TecAdmin.

]]>
https://tecadmin.net/git-remove-file-but-keep-local/feed/ 0
Git Worktrees: Parallel Development Guide https://tecadmin.net/git-worktrees-parallel-development-guide/ https://tecadmin.net/git-worktrees-parallel-development-guide/#respond Mon, 28 Sep 2020 14:37:00 +0000 https://tecadmin.net/?p=22916 Under specific scenarios, you may require different identical copies of your repository. At this point, you are probably thinking about cloning your storage – but there is a better solution. Git offers a better and more reliable workflow model – Git worktree. It gives a perfect copy of your entire repository. In this article, I [...]

The post Git Worktrees: Parallel Development Guide appeared first on TecAdmin.

]]>
Under specific scenarios, you may require different identical copies of your repository. At this point, you are probably thinking about cloning your storage – but there is a better solution.

Git offers a better and more reliable workflow model – Git worktree. It gives a perfect copy of your entire repository.

In this article, I will explain all you need to know about worktree and how you can use it step-by-step with the Git best practices. And once you know all these basics, Git Worktree will be relatively easy to implement as compared to other practices like cloning with Git Clone your repository.

What is Git Worktree

In simple words – Git worktree allows developers to have multiple working directories at the same time and those working directories associated with a single Git repository.

If you are working on a large project and have to switch branches to work on different issues, Git worktree can be helpful.

Manage Git Worktrees

Before you start adding a new worktree, let us see how many worktree you have right now.

List Worktrees

You can check with the following command.

git worktree list  

Git list all worktrees

At this time, you should see only one worktree. It’s your default worktree.

Add New Worktree

Let us add a new worktree, and you can use the following commands –

git worktree new_working_tree  

Once you add a new tree, you can view all the working trees by the worktree list command to confirm that you now have two available trees.

Git create new worktree

And this newly created working tree known as a linked tree.

Remove Worktree

You might have to delete your worktree for many good reasons like When your work is complete, and it’s safe to delete the linked worktree.

You can use the following command to delete the worktree

git worktree remove your_worktree_name 

Git remote worktree

Why Multiple Worktree

There should be a question in your mind that you would trouble introducing a linked worktree if you already have your main one.

Why do you need multiple worktrees? – For parallel development.

With multiple worktree you can do parallel development, and also you need these for the following reasons

Parallel Development & Testing

If you are working on a legacy enterprise project, and at some time, you have to fix bugs, and then other hours need to add a feature. In that case, Git worktree will be useful and support parallel development.

In the same way, Running multiple test suites in parallel reduces the run time. It is convenient to use various test suites at the same time to test your coverage.

It is easy to change your worktree from primary to linked with a simple git command. Also, you can make changes in git worktree by using Git cherry-pick and git reset.

Easy Management

It’s easy to manage your codebase with multiple worktrees as compared to cloning your repository. Git worktree is a lightweight entity, and it is easy to use and manage.

When you use the git clone command to your existing repo, you make a full copy of your repository. And It might be possible you create the same branch name in your existing and newly created repo. It might be confusing and hard to manage.

Here, the good part about git worktree will allow you to create the same branch name in your primary and linked worktree — so overall, it’s easy management.

Save Time & You Stay Focused

Switching is an expensive operation because when you switch, you completely restructure the repository.

And If you change your current working branch from IDE, in that case, your IDE might run mad and try to adapt to the project settings.

With the help of a worktree, you can avoid these frequent switching. You can checkout required branches in separate folders using the worktree.

So there you will get an independent IDE project for each branch.

Conclusion

Git clone is an older way to duplicate from the principal repository – whereas, Git worktree is one of the best practices.

And in the Git worktree, there is no need to worry about the branches. For example, you can not check in the create branch name in linked worktrees. And because they are linked to your repository, so it will be recorded in history.

The post Git Worktrees: Parallel Development Guide appeared first on TecAdmin.

]]>
https://tecadmin.net/git-worktrees-parallel-development-guide/feed/ 0