> git init > -m “learnings”

Everytime I create a project and push it first time to a github repository that is just created, I am reminded that it is not that easy. I face a couple of issues like “authentication failed”, “cannot merge unrelated histories”, etc. Let’s see how we can workaround these issues.

The Right Way

Lets start with the right way to create a new project. Well, it is obvious.

  1. Create a github repository
  2. git clone the repository in a local folder
  3. Create the project using your favorite framework
  4. Commit and push the changes

What if you have done steps #1 and #3, and skipped #2? It is fine. You can still do git clone as your step 3 but you will need to copy the project files to the cloned folder.

I have already created my project, a github repo and did a git init

So, here is another situation. You followed some online article on how to setup github repository for a new project. You created a repository, did a git init on your local folder and manually setup the remote urls.

There are two side situations here. One is that your remote github repository has already got some files like README, license, etc. which were created by github for you. Second is that your remote repository is completely empty. If your situation is same as the latter, then you are fine. You should be able to commit and push your project to github. But if you are in the first situation, then you are going to get a message that "cannot merge unrelated histories" when you try to push or pull commits. You can fix this issue by issuing the command

git pull --allow-unrelated-histories

On a side note, if you want to un-commit your local changes, then do git rm -r --cached

Authentication failed

Most of the times you get authentication issue when you have 2 Factor Authentication enabled in github and when you are using HTTPS to connect to the repository. 2FA is a good thing. It is just that you have to do an extra step to interact with your repository. The extra step is to create a Personal Access Token in github and store that in your local “credential manager” so that you do not have to type the credentials every time you issue a git command.

To configure git to use the windows credential manager, run following command. When you do push/pull, enter username and pat when prompted. The credentials will be saved in the credential manager and you will not have to enter credentials again.

git config --global credential.helper manager

Note 1: If git still complains about authentication, please check and upgrade git. Some older versions do not use windows credential manger.

Note 2: Remember that copying PAT and pasting in to the password prompt using keyboard shortcuts in a shell window might not work on all shell interfaces. For example, in Windows Terminal “Ctrl + v” does not work but you have to do a “Right click” to paste the PAT in to password prompt. This was one of the dumb things that took me a while to figure out my first time!

Do not ignore .gitignore

Chances are that your project has already got a default .gitignore file that is best for your framework of choice. If you do not have got one, make sure that you create a .gitignore file before you make your first commit/push to repository. When you are creating the file, make sure that you have “View file name extensions” enabled in the project folder because you are creating a file with no name and having just the extension ‘gitignore’.

Multiple sub modules

Some programming frameworks automatically create a local git repository for new projects. It is definitely a good practice to keep different projects under different repository. Therefore, git provides submodules feature using which you can still have all related projects under one folder but tracked by different remote repositories. But if you want only one repository then your solution is to delete git tracking in all the projects and keep git tracking only in the root folder (which you might have done when you did git clone or git init).

To remove git tracking, simply delete the .git folder from all projects. This is a hidden folder by default, so you can use following command to delete tracking from the current project folder

rm .\.git\

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.