Troubleshooting common Git issues

Troubleshooting common Git issues
Photo by Yancy Min / Unsplash

In this article we will go through commonly asked git questions regards to branches and other git related questions.

Explaining git and how it works to a child

Imagine you have a toy box where you keep all your toys. One day, you decide to share some of your toys with your friend.

Git is like a toy box for computer code. Instead of toys, it stores different versions of computer code that people write. Just like you can share toys with your friend, people can share their code with each other using Git.

When you share toys with your friend, you might want to make sure they don't accidentally break your favorite toy or forget to return it. In the same way, Git helps people keep track of changes made to the code and makes sure everyone is working on the same version of the code.

For example, if you and your friend want to add a new feature to a game you're making, you can each work on your own version of the code. Git will keep track of the changes you make and help you merge your work together so you end up with one improved game that has all the new features you both worked on.

In summary, Git is like a toy box for computer code that helps people share and keep track of changes made to the code. It helps make working on projects with others more organized and efficient.

How to check parent branch in git?

To check the parent branch in Git, you can use the command git show-branch. This command shows the branch and commit history of your repository, and it also highlights the branch that your current branch was created from.

Here are the steps to check the parent branch:

  1. Open the terminal or Git Bash in the directory of the repository that you want to check.
  2. Type the following command: git show-branch
  3. Press Enter. This will show you the branch and commit history of your repository.
  4. Look for the line that corresponds to your current branch. It will be highlighted with a star (*).
  5. Identify the branch that is highlighted with a caret (^) symbol. This branch is the parent branch of your current branch.

For example, if you are currently on a branch called "feature-branch" and it was created from the branch "develop", the output of the git show-branch command will show the following:

* [feature-branch] Add new feature
^ [develop] Merge feature-branch into develop

In this example, you can see that the parent branch of "feature-branch" is "develop" because it is highlighted with the caret (^) symbol.

How do you switch branches in git?

To switch branches in Git, you can use the git checkout command followed by the name of the branch you want to switch to. Here are the steps:

  1. Open the terminal or Git Bash in the directory of the repository that you want to switch branches in.
  2. Check the list of branches in your repository by typing the command git branch. This will show a list of all the branches in the repository, with an asterisk next to the currently active branch.
  3. Decide which branch you want to switch to, and make sure that you have committed any changes you want to keep in your current branch.
  4. Type the command git checkout followed by the name of the branch you want to switch to. For example, to switch to a branch named "my-new-branch", you would type git checkout my-new-branch.
  5. Press Enter. This will switch you to the branch you specified, and the terminal will display a message confirming the switch. If you have any changes in your working directory that conflict with the new branch, Git will prompt you to either commit or discard those changes before switching branches.

How do i delete a git branch locally and remotely?

To delete a Git branch both locally and remotely at the same time, you can use the git push command with the --delete flag.

Here are the steps:

  1. Open the terminal or Git Bash in the directory of the repository that you want to delete a branch from.
  2. Switch to a different branch that you are not trying to delete by using the command git checkout followed by the name of the branch you want to switch to.
  3. To delete the branch locally, use the command git branch -d followed by the name of the branch you want to delete. For example, to delete a branch named "my-branch", you would type git branch -d my-branch.
  4. To delete the branch remotely, use the command git push followed by the name of the remote repository, the : character, and the name of the branch you want to delete. For example, to delete a branch named "my-branch" from the remote repository named "origin", you would type git push origin --delete my-branch.
  5. Press Enter. This will delete the branch both locally and remotely. If the branch was successfully deleted, Git will display a message confirming the deletion.

Read more