Join our Newsletter to find out first about the Job Openings!

GitHub Basics 18 Common Problems and Possible Fixes - Inside WPRiders Article

GitHub Basics: Discover 18 Common Problems and Possible Fixes

Navigate to

1. I do not have Git installed, what now?

To install Git you can follow the steps based on your operating system:

Windows

  • This is pretty straightforward, you just download the exe file and follow the steps.
  • The exe file can be downloaded from here
  • I do recommend the standalone version ( full installer ).
  • To use as terminal, you need to open “Git Bash”

MacOS

  • To make your life easier, you have to first install Homebrew if you don’t have it already installed. Homebrew can be found at this URL. If by chance you get an error while using the command install, try typing “bash” and then hit enter in the terminal. 
  • For MacOS I highly recommend using “Oh My ZSH” terminal ( found here
  • Using Homebrew in the terminal you can easily install Git using the command found here ( the command with “brew install git”)

Linux

  • This will be different based on your version of Linux, but there’s no issue with that as they have details on how to install it using methods based on your Linux version. Details are found here.  

2. I have installed Git, but how do I create and copy my SSH key(s) to add them to the project(s)?

Windows, MacOS, Linux

  • In all cases it’s easier to create the key using the terminal command.
            
                

ssh-keygen -t rsa

  • The exception is for Windows only, the default terminal will not work and Git Bash terminal must be used. After you type the command and hit enter, you will be asked a few questions like the location, file name, and password. 
  • It is recommended to use a password when creating the key, there are web applications where your key will be rejected because it’s not password protected and the error message received doesn’t inform you specifically about it and you will end up scratching your head to understand what’s wrong.
  • To add the key to a web application you have to copy the content of the .pub ( public ) key file. You can copy the key content by opening the file using an editor or by terminal command, which is easier for you.
  • The keys can also be created by using a Git client APP, but this situation is a bit more confusing to me as sometimes you will not be informed where exactly it was created.

3. Do I create SSH keys for all projects separately or in one place?

Once upon a time, I used a single file to save the keys, then it took a bug to the content and it got corrupted. The result was that I had to go to all the web apps and replace the key. This can be a hassle if you use git with multiple different hosted repositories.

If you are creating different keys for different remote web applications ( GitHub, BitBucket, and so on… ) it’s easier for you to handle and have better control over it. When a key expires, you can delete and easily replace only that key for the specific web application.

4. I have created and uploaded my SSH key, but I can’t push the changes saying that ‘The key is not correct’ or ‘Permissions issues’

This is a fun one and there are 2 possible issues. This can happen when you have multiple keys.

The key has no password

  • Some hosting will require for the SSH key to be password protected and will reject the key even if you have done everything according to specifications ( except the password ).
  • The solution for this is to delete and create the key again and add a password in the system of SSH key creation.

Incorrect key used

  • Sometimes the correct SSH key will not be read/loaded which is why you will have to specify which SSH key to use for your project repository, this can happen when you create multiple keys for different hostings.
  • The solution is to run a terminal command where the “.git” folder is located for your project ( not inside “.git” folder ).
  • Close your git client and open the terminal. The key with the extension “.pub” is the public key, you will need to use the private key for this.
  • Use terminal or git bash ( windows ) and type the command:
            
                

git config core.sshCommand ‘ssh -i ~/.ssh/

Then type: 

            
                

eval “$(ssh-agent -s)

Now, open the git client and try again. Opening and closing of Git client will make it reload the git config file.

5. Should I use git clone or not? What’s the difference?

Git Clone

  • When you are using the terminal command git clone <repo_url> a new folder will be created in the folder where you opened the terminal. The new folder will contain the name of the repository. 
  • With this method you do not need to define the git config first, you can use the command right away.

Git Pull

  • When you are using the terminal command git pull origin <master|main|branch_name> the content of the remote repository will be added to the current folder, it does not create a new folder with the repo name. 
  • With this method, you have to define the git configuration for the project first.

6. Can I have multiple repositories where I can push the same code?

It’s possible to have ties with multiple remote repositories from the same or different platforms. Using the terminal to add a repository URL, you will have to declare an alias for the URL.

Example:git remote add origin git@somedomain.com/something/something.git

The word “origin” is just an alias, you can name it however you want. Once I have added this to the git configuration file with the terminal, I can add more remote aliases with different URLs.

Example:git remote add wpriders git@domain2.com/potato.potato.git

This will allow you to configure a second remote repository URL for the same source code…and so on. The config file data will look something like this:

            
                

[remote “origin”]

url = git@wpriders.git.beanstalkapp.com:/wpriders/wpriders_theme.git

fetch = +refs/heads/*:refs/remotes/origin/*

[remote “wpriders”]

url = git@domain2.com/potato.potato.git

fetch = +refs/heads/*:refs/remotes/wpriders/*

7. How do I set up my First/Last name and email address for Git setup? Globally and just for a project. ( all OS )

To define your full name globally for all git repositories use the following terminal command.

            
                

git config –global user.name “John Doe”

To check that it was saved, use the following command

            
                

git config –global user.name

In a similar manner, you can set the email:

            
                

git config –global user.email “youremail@yourdomain.com

Run “git config –list” to check that it was saved. ( press Q to quit )

 To only add these credentials to a local repository, use the same commands but without the word “global”

8. How do I set a default editor for Git?

Use a text editor to open and edit your files with Git. This command is for all OS to set Visual Studio Code as the default editor:

            
                

git config –global core.editor “code –wait”

9. I’m starting a new project, how do I set up my Git environment? [or] The project (client) already has a Git repository, how do I add that on my local machine?

The repository exists

I for one like to set up the remote Git repository URL by terminal, pull the code, and then open it with a Git client APP. There are a lot of free and premium Apps, SourceTree is a free client Git APP and easy to use.

How to set up a git repository from zero using the terminal:

  • Navigate or the folder where you want to set up git.
  • Type in order:
            
                

git init

git remote add origin git@domain.com:/folder/reponame.git

git pull origin master

  • Where origin is the alias for the link and master is the branch, it can be another branch than the master( main or custom-named branch ).

The URL info is normally offered by the repository. Avoid using anything other than Git links, like HTTP link types.

On GitHub, you can also use their Git-CLI.

Remember that SSH (tab) repository link will only show up if you are authenticated ( especially on GitHub )

The repository does not exist 

In this case, you will have to set it up on the hosting at your disposal, be it GitHub, Bitbucket, BeanstalkAPP, and so on. After you create the repository on the platform of choice or need, the next step is to upload the code from your local repository.

  • First of all, configure the Git repository if not configured already.
  • You will want to create a file called “.gitignore” in the root directory where the “.git” folder is located.
  • Add any folder or file path to the file, each on a new line. The path must be relative to the .gitignore file.
  • For example, we have a folder called “classes” and in that a folder called  ”uploads”. We want Git to ignore the folder uploads. Thus we need to add the path “/classes/uploads/” to .gitignore file.
  • Remember that you have to do this right before your first push on Git. Even if you add a folder/file later to ignore, it will not be ignored since it was already pushed on the repository.
  • You can fetch the repository URL and configure the local repository with the online repository:
            
                

git init

git remote add origin git@domain.com:/folder/reponame.git

  • After this, it’s best if you open the local repository with SourceTree or another Git client APP. 
  • From there, you can check the files and folders list that are up for commit and push.
  • If you see something in the list that you don’t want to be uploaded, return to the file .gitignore and add it there, before making the first commit.

10. I’ve set up my Git and have to start a new task for the project. How do I approach this with Git? [or] I have finished my work on a task, on a new branch. How do I merge my code into the main branch?

Option One

This is the most used option.

Using a visual Git client ( SourceTree ), make sure you have committed everything to the branch you are currently working on.

Let’s use the following example: Working branch name “task-122-issue-with-meta” ( alias branch beta )

Main branch where you want to merge branch beta is named “master” or “main” ( alias alpha branch ).

We have just committed our changes to the beta branch, as for the next step is to switch to the alpha branch ( checkout alpha branch ).

Use the mouse to right-click on the beta branch after switching to the alpha branch and you will see an option “Merge beta branch into alpha branch”.

Option Two

This option is not a merge action but a way to move parts of the code between branches.

This option will only work with a code editor. For the next example, we will be using PHPStorm.

Similar to the above example we check out the alpha branch. Open the Git menu and click on the logs tab if not already selected.

Use the mouse right-click on beta and you should see an option named “Show diff  with working tree”. After selecting the option, a list of files will be displayed, these files are the ones that have differences between branches. If the beta branch has a file that you want in alpha branch, right-click on the file and use the option “Get from branch”. This will copy the file from the beta branch to the alpha branch. 

GitHub Basics 18 Common Problems and Possible Fixes - Inside WPRiders Article

If you open the file with a double left click, you will be able to move parts of the code from the beta to alpha with ease. You can use the arrows to move the code or you can just copy-paste it from beta to alpha with no restrictions, but not from alpha to beta. This can also be used to solve conflicts as it allows you to edit the current branch code.

This method also allows you to get only parts of the code from any branch to the current branch, in case you need something from a different branch but that branch is not done yet so it’s not merged in the main branch.

11. I applied the merge action, but it says something about conflicts. How do I fix this?

Using visual editor

We merged 2 branches and we got the message that there are conflicts. One option is to use PHPStorm or VSCode ( or other advanced code editors that have similar functionalities ). If we open the project in PHPStorm, we will see the files that have conflicts marked with a color, usually red/orange.

On any of these files, right-click to see the options, and navigate to the Git submenu, only the affected files will have a submenu called “Merge conflicts”. A new window or file list will display only the files that have conflicts. Opening those files ( by selecting the file and clicking Merge ) will allow you to visually select which changes to move from branch B to branch A or copy-paste the changes manually.

In some cases, it’s easier to copy-paste just parts of code. If you are working with another developer on the project and you are confused about which version is the latest or the good version of a class/function when solving the merge conflict, just contact them and discuss it. In rare cases maybe both of you made changes to the same function and the final version will require both changes, then just manually copy parts of the code required from branch B to branch A ( you will not be able to copy-paste from branch A to B ) Another option is to use the option “Show diff with working tree” which was described previously.

12. I have started working on a solution for the client-reported issue. But I need to try multiple solutions to find out the best one. Can I save the progress of each solution without going into the code and commenting on each line of the code? [or] Something came up mid-development and for X reasons, I don’t want to push my unfinished code, how can I save it without a commit so that I can switch branches to fix the emergency issue that came up?

Maybe you have more time or maybe you need to research for the best solution regarding an issue. You create a branch from the main ( latest ) branch which has the latest code. 

After reaching a certain point and after modifying multiple files you want to start from scratch, but don’t want to create multiple branches just for the sake of it and also you do not want to lose the changes you have made in the current branch,

The solution is to create a Git Stash. The main point is to not commit the changes to git. Select the uncommitted selection and look for the option “Stash”, for example, SourceTree has a button at the top menu for it.

The APP will request a name for the stash, and input relevant keywords, you want to figure out what the stash contains at first glance.

All the uncommitted code since you started working will be saved into this stash and it will be removed from the branch itself. 

Your branch will look like no changes have been made to it since its last commit

You can start working on a different solution and create another stash later to save the code you wrote. Stashes can be applied to any branch and can be accessed at any time, you can even see which files were affected and which code was changed. You can copy-paste code from a stash if you do not need the whole stash.

If you apply the entire stash, a new prompt will appear on the screen asking you if you want to delete the stash after it is applied. If you want to delete it or keep it, it is up to you. I usually keep them until it’s very clear to me that I do not need them.

Important notice

A stash is being saved only locally and not online. There are some unconventional ways to save them online as temporary branches or you create multiple branches and apply the stash, each stash with its own branch, but this might take a while at some point.

Just remember that these are not permanent, if something happens to your local Git, they are gone. Well, this has a low chance of happening, but it’s better to know in advance.

13. I need to report which files had changes or what new files were added in the last X number of commits. Can I get this info easily?

Using SourceTree you can right-click on a commit and select copy SHA/hash.

You will need two hash items to get the full list of files changed/added/deleted in between.

We will use the example:

Latest commit = C1

5 commits back ( including C1 ) =  C5

6 commits back ( including C1 ) = C6 or C5+1

GitHub Basics 18 Common Problems and Possible Fixes - Inside WPRiders Article

You need the list of files between C1 and C5.

You will need the hash of C1 and C5+1

Open the terminal in the project Git root, where the folder .git is also located. 

Run the following terminal command:

            
                

git diff –name-only SHA1 SHA2 or git diff –stat SHA1 SHA2

14. I have a file for which I want to save the changes locally but not push them to the working branch

This can only be used before making the commit.

Some files in your working directory may be tracked by git, meaning that Git knows about them and records their history. Other files may be untracked, meaning that Git does not know about them and ignores their changes.

To check if a file is tracked or untracked, you can use the `git status` command, which will show you the state of your working directory and the staging area.

You can also use the `git ls-files -v` command to list all the tracked files in your working directory.

            
                

git update-index –assume-unchanged

To add it back to the index/tracking, for git to see file changes again, run the command.

            
                

git update-index –no-assume-unchanged

This command is handy for local configuration files that you don’t want to accidentally commit. However, be cautious, as this could lead to conflicts if updates to these files are made in the remote repository.

For folders we have different commands:

            
                

git ls-files -z / | xargs -0 git update-index –assume-unchanged

And

            
                

git ls-files -z / | xargs -0 git update-index –no-assume-unchanged

15. Some Git commands are long and have way too many parameters which is why it’s hard to remember them. Is there a way to save them in the terminal?

Some of the git commands have multiple parameters that can be fixed for what you need and it’s hard to remember all of the parameters. If you are using “Oh My ZSH” https://ohmyz.sh/ then it’s very easy to create an alias for any command, complex or not. 

After installing ZSH ( MacOS/Linux ), in your home directory, you will find a file named “.zshrc” and you can open it with any editor as it is considered a text file.

In the file you will see some examples of aliases:

GitHub Basics 18 Common Problems and Possible Fixes - Inside WPRiders Article

# Example aliases

# alias zshconfig=”mate ~/.zshrc”

# alias ohmyzsh=”mate ~/.oh-my-zsh”

I will create a new alias 

alias gstat=’git status’

After creating an alias, restart terminal. Now if I run the command in the terminal “gstat”,
it will run the command “git status”.

16. Where is git root recommended to be set?

This only applies if you have to create the git repository for a project.

If the project already exists, it’s already decided where the git root is located.

It’s better to use the WordPress root folder, where wp-config.php is located, as git root. Even if you only work on a plugin for this project or a theme.

After creating the git root in the WP root directory, you need to add to the list of git ignore every folder/file that is not related to your work.

If later you need to create a new plugin for this project or a child theme, you can have them on the same repository just for the simple reason that your git root is in the WP root.

If you created the git root in the plugin/theme directory root then you will not be able to add any other plugin/theme to the repository as these will not be visible.

If your project requirement is clear that it’s only a plugin/theme that you maintain frequently, or add features to, and it does not belong to any WordPress project, it’s better to have the git root in the plugin/theme root folder.

17. I have 2 remote repositories in which I have to push changes and I have 2 SSH keys that need to be used, each with its remote repository.

You can specify after adding both or X remote repositories links to the git configuration. You need to open the config file with a text editor from .git folder in the project.

You can specify which SSH key to use for each remote repository by adding this line for each remote repository link instead of [core].

            
                

sshCommand = ssh -i ~/.ssh/

In the end, it would look like this

            
                

[remote “origin”]

url = git@github.com:personal/project.git

fetch = +refs/heads/*:refs/remotes/origin/*

 # Use the personal SSH key for this remote

sshCommand = ssh -i ~/.ssh/personal_rsa

[remote “work”]

url = git@github.com:work/project.git

fetch = +refs/heads/*:refs/remotes/work/*

# Use the work SSH key for this remote

sshCommand = ssh -i ~/.ssh/work_rsa

This will specify which SSH key to be used for each remote URL. Restart your Git client APP so it reloads the git configuration for the project.

18. I need code from a commit from another branch, how can I move that exact code to my current branch?

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD. 

Cherry picking is the act of picking a commit from a branch and applying it to another.

This is a useful tool but not always a best practice. Cherry picking can cause duplicate commits and in many scenarios where cherry picking would work, traditional merges are preferred instead.

In PHPStorm use the option from the menu Git > “Show diff with working tree” where you compare 2 branches and can move code to your current branch from the branch you compare with.

Examples of how to use cherry pick.

You need to make sure you are on the correct branch and then use cherry pick option from the APP or by the terminal. The commit you cherry picked will be applied to your current branch.

By terminal:

            
                

git cherry-pick

You can easily find the SHA for a commit using a visual APP instead of the terminal. 

It can be displayed by selecting a commit or right-click on a commit and find the option “Copy branch SHA”

You can add to cherry pick the parameter -e or –edit to edit the commit message before actual commit.

Recommended Git Client Apps

Free: SourceTree (MacOS, Windows)

Free: GitFiend ( MacOS, WIndows, Linux ) – has less options than SourceTree

Premium: GitKraken ( MacOS, Windows, Linux )

Or you can use your code editor. 

PHPStorm – has built in Git client system.
Visual Studio Code – Install plugin GitLens

Do you like this article? Share it and send us your feedback! Here are other articles you might find interesting. Also, if you want to learn more about business, check out the WPRiders Blog!
Recruiter job - project based and remote - Inside WPRiders
Don't forget to subscribe Receive WPRiders' newsletter for the freshest job openings, sent directly to your inbox. Stay informed and never miss a chance to join our team!