git – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 10 Jan 2023 02:32:21 +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
Git Change Remote URL to SSH (from HTTPS) https://tecadmin.net/git-change-remote-url-to-ssh-from-https/ https://tecadmin.net/git-change-remote-url-to-ssh-from-https/#respond Thu, 05 Jan 2023 13:25:46 +0000 https://tecadmin.net/?p=33659 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. One way to access a Git repository is via SSH (Secure Shell). SSH is a [...]

The post Git Change Remote URL to SSH (from HTTPS) 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.

One way to access a Git repository is via SSH (Secure Shell). SSH is a network protocol that provides a secure way to communicate with a remote computer over an unsecured network. Using SSH can be more secure and efficient than other methods, such as HTTP or HTTPS, for accessing Git repositories.

If you want to change the URL of a remote repository in your local Git repository to use SSH, here are the steps to follow:

Prerequisites

Before you can change the remote URL to use SSH, you will need to do the following:

  1. Generate an SSH key pair. This consists of a private key and a public key. The private key is kept on your local machine, and the public key is uploaded to the remote repository.
  2. Add your public key to the remote repository. This allows your local machine to securely connect to the remote repository using the private key.

If you are not familiar with generating SSH keys or adding a public key to a remote repository, you can find more information and instructions in the Git documentation or online tutorials.

Changing the Remote URL

Once you have generated an SSH key pair and added your public key to the remote repository, you can change the remote URL in your local Git repository as follows:

  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 to use SSH, use the following command:

    # Syntax
    git remote set-url <remote> <user>@<host>:<path>

    Replace <remote> with the name of the remote repository (usually origin), <user> with your username on the remote host, <host> with the hostname or IP address of the remote host, and <path> with the path to the Git repository on the remote host.

    For example, to change the URL of the origin repository to “git@github.com:username/repo.git”, you would run the following command:

    git remote set-url origin git@github.com:username/repo.git 
    
  5. Verify that the URL has been changed by running the `git remote -v` command again. You should see the new SSH URL listed for the specified remote repository.

That’s it! The remote URL for your local Git repository should now be changed to use SSH. You should now be able to push and pull changes to and from the remote repository using the git push and git pull commands 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 to SSH (from HTTPS) appeared first on TecAdmin.

]]>
https://tecadmin.net/git-change-remote-url-to-ssh-from-https/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 Create Empty Branch in Git Repository https://tecadmin.net/git-create-empty-branch/ https://tecadmin.net/git-create-empty-branch/#respond Sat, 04 Jun 2022 16:34:39 +0000 https://tecadmin.net/?p=29751 I have an application code maintained in the Github repository. Now our requirement is to create the documentation for that application and save them under a separate branch in the same repository. I simply tried to create a new branch but it linked to the main branch. After googling it, I found an option --orphan [...]

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

]]>
I have an application code maintained in the Github repository. Now our requirement is to create the documentation for that application and save them under a separate branch in the same repository. I simply tried to create a new branch but it linked to the main branch. After googling it, I found an option --orphan to create branch with no parents.

This tutorial will help you to create a new empty branch in the Git repository.

Git Create Empty Branch

We can use --orphan command line option to create a new branch with no parents.

git checkout --orphan new-empty-branch 

The above command will create a new branch with no parents. Now, you can delete files from the working directory, so they don’t commit to a new branch.

git rm -rf . 

Now, you can add new files to this new branch, commit and push them up to the remote git repository.

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

]]>
https://tecadmin.net/git-create-empty-branch/feed/ 0
How to Install Latest Git on Ubuntu 22.04 https://tecadmin.net/how-to-install-latest-git-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-latest-git-on-ubuntu-22-04/#comments Tue, 24 May 2022 10:52:40 +0000 https://tecadmin.net/?p=4114 Git is a free and open-source version control system that is widely used for software development and version control. It allows developers to track changes to their codebase, revert to previous versions, and collaborate with other developers. An older version of the Git client is also available under the default Apt repositories. The latest versions [...]

The post How to Install Latest Git on Ubuntu 22.04 appeared first on TecAdmin.

]]>
Git is a free and open-source version control system that is widely used for software development and version control. It allows developers to track changes to their codebase, revert to previous versions, and collaborate with other developers.

An older version of the Git client is also available under the default Apt repositories. The latest versions come with multiple enhancements and security updates. So, we always recommend using the latest Git client for the security of valuable and hard work.

This article will guide you to install the latest Git client on Ubuntu 22.04 Linux system via PPA.

Installing Latest Git Client on Ubuntu

    First of all, you need to update the package manager’s list and install the `software-properties-common` package on your system. You can do this by running the following commands:

    sudo apt update && sudo apt install software-properties-common 
    
  1. Then, add the Git PPA to your system. Which is regularly updated by the Ubuntu Git maintainers team and provides the latest Git versions for Ubuntu systems. Open a terminal and type:
    sudo add-apt-repository ppa:git-core/ppa 
    

    How to Install latest Git on Ubuntu 22.04
    Configuring the Git PPA
  2. Now, you can install the latest version of the Git client on your Ubuntu system.
    sudo apt install git 
    

    How to Install latest Git on Ubuntu 22.04
    Installing Git on Ubuntu
  3. After installation is finished, verify the installed Git version by running the following command.
    As a result, you should see the latest Git client version installed on your Ubuntu system.

    git --version 
    

    How to Install latest Git on Ubuntu 22.04
    Check git version

Configuring the Git Client

The git client required developer information like Name and Email address. These details are used by the Git client to attach proper use information with git commits. Every git user must set these values once.

Run the following commands from your account to save them globally to share between applications.

git config --global user.name "Your Name" 
git config --global user.email "youremail@example.com" 

Replace “Your Name” with your actual name and “youremail@example.com” with your email address.

The above information is stored under ~/.gitconfig file. To view this information at any time run the following command.

git config --list 
Output:
user.name=Your Name user.email=you@example.com

Conclusion

That’s it! Git should now be installed on your Ubuntu 22.04 system. You can use Git to manage version control for your code projects. If you encounter any issues while installing or using Git, you can refer to the Git documentation or seek help from the Git community.

The post How to Install Latest Git on Ubuntu 22.04 appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-install-latest-git-on-ubuntu-22-04/feed/ 3
How To Install Git on Fedora Linux https://tecadmin.net/install-git-on-fedora/ https://tecadmin.net/install-git-on-fedora/#comments Thu, 10 Feb 2022 10:47:49 +0000 https://tecadmin.net/?p=5609 Git is a free and open-source distributed version control system. It is designed to handle small to very large projects with speed and efficiency. This article will help you to install the latest Git client on Fedora Linux systems. Method 1. Installing Git using DNF The default Fedora repositories also contain the Git packages. But [...]

The post How To Install Git on Fedora Linux appeared first on TecAdmin.

]]>
Git is a free and open-source distributed version control system. It is designed to handle small to very large projects with speed and efficiency.

This article will help you to install the latest Git client on Fedora Linux systems.

Method 1. Installing Git using DNF

The default Fedora repositories also contain the Git packages. But it contains an older version. You can use the following command to install the available git client on the Fedora system.

sudo dnf install git 

Method 2. Installing Latest Git from Source

  1. Firstly we need to make sure that we have installed the required packages on your system. Use the following command to install all the required packages for source code compilation.
    sudo dnf install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker 
    
  2. Now remove any prior installation of Git through RPM file or Yum package manager. If your older version is also compiled through source, then skip this step.
    yum remove git 
    
  3. Download git source code from kernel git or simply use following command to download Git 2.0.5.
    cd /usr/src 
    wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.36.1.tar.gz 
    tar xzf git-2.36.1.tar.gz 
    
  4. After downloading and extracting the Git source code, Use the following command to compile the source code.
    cd git-2.36.1 
    sudo make prefix=/usr/local/git all 
    sudo make prefix=/usr/local/git install 
    
  5. Now, configure the PATH environment variable.
    echo "PATH=$PATH:/usr/local/git/bin" | sudo tee -a /etc/environment 
    source /etc/environment 
    
  6. Once completing the above steps, you have successfully installed Git in your system. Let’s use the following command to check the git version
    git --version 
    
    git version 2.36.1
    

Conclusion

This tutorial helped you to install the latest Git client on the Fedora Linux system.

The post How To Install Git on Fedora Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/install-git-on-fedora/feed/ 15
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