Git and GitHub Beginner Tutorial — Multiple Accounts

Ahmet Ekiz
12 min readDec 24, 2021

--

If you’re a programmer and don’t use git, it’s pretty hard to keep track of your codes and versions. I was always copy-pasted project folders with names v1,v2,v3, etc. You should use Git to fix this problem.

As a beginner, I followed several tutorials and videos to learn Git and GitHub, but every time something was wrong. I mean, I had to find more tutorials to learn the correct way. So, I decided to write a simple tutorial for beginners like me.

Contents

  1. What is Git?
  2. What is GitHub?
  3. Terms
  4. Installing Git
  5. Git Commands
  6. Branches
  7. Merge Branches
  8. Start to Github
  9. For Multiple GitHub Account
  10. Push code to GitHub Repo

1. What is Git?

Git is a version control system that lets you track all changes of codes and files. It is a command-line tool that allows you to create different versions of the project called branches. You can make changes, then merge all codes and files. That makes it easier to work with a lot of versions of projects.

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

2. What is GitHub?

GitHub is a cloud-based Git repository hosting service owned by Microsoft. You can upload your project on GitHub, work remotely by downloading the project and sharing it with other people publicly or privately.

Git and GitHub

A quick aside: git and GitHub are not the same thing. Git is an open-source, version control tool created in 2005 by developers working on the Linux operating system; GitHub is a company founded in 2008 that makes tools which integrate with git. You do not need GitHub to use git, but you cannot use GitHub without using git. There are many other alternatives to GitHub, such as GitLab, BitBucket, and “host-your-own” solutions such as gogs and gittea. All of these are referred to in git-speak as “remotes”, and all are completely optional. You do not need to use a remote to use git, but it will make sharing your code with others easier.

3. Terms

  • Directory: Folder
  • Terminal or command line: Interface for Text Commands
  • CLI: Command Line Interface
  • cd: change directory
  • Code Editor: Word Processor for Writing Code (ex: VSCode, Atom, PyCharm)
  • Repository: Project, or the folder/place where your project is kept

4. Installing Git

To download Git: https://git-scm.com/downloads

I mostly use Ubuntu, and I will show you commands on Ubuntu. The commands are the similar as Linux, and macOS include built-in Unix command line terminals. But since the Windows command line is used in Windows Command Prompt, some commands will be different.

To download Git: https://git-scm.com/downloads

If you are using Ubuntu:

apt-get install git

5. Git Commands

First, create the project folder and then initialize Git to create local repository. If you are working on Windows, use Git Bash to execute these commands.

I opened terminal on Ubuntu:

mkdir test_1  # to create new folder named test_1cd test_1  # change directorygit init  # to initialize git

If you have multiple accounts or using it the first time, I suggest modifying Git config.

git config user.name “name”
git config user.email “your_email”
# that’s my config
git config user.name “WhiteAIcode”
git config user.email “whiteaicode@gmail.com
create project folder and initialize git

First Code

When you write the first code, VSCode that I use as a code editor shows file status. In the terminal, you need to write git status to see file status.

git status  # to see file status

This green U means Untracked files, so you have to save it if you don’t want to lose the changes you had. This part is called the staged state. That is the step before committing the files.

show file status

Add Specific File to Stage to Track Files

git add "file name"   # add specific file to stage to track filesgit add first_code.py # If you want to add all files to stage
git add .
Add specific file to stage to track files

When you created or changed a new file:

Add all files to track

git add .
Add all files to track

Remove Tracked Files

git rm --cached "file name"git rm --cached second_code.py
Remove tracked files

Commit Files

Committing files is saving your changes to the local repository.

git commit -m “title or short message” -m "description" git commit

You can write what is changed or added to files for a short title and description. (Title and description is optional.)

committing files

6. Branches

You don’t want to break the working code every time you want to add a new feature to the project or try something. Instead of copying all the project files manually, you can copy them as a new branch and save and change the files without changing the main project files.

By default, every git repository’s first branch is named “master” (and is typically used as the primary branch in the project). (source)

Branching means you diverge from the main line of development and continue to do work without messing with that main line. (source: git)

example visual representation of branches

To create new branch with copying master branch:

git checkout -b "branch name"git checkout -b feature_1  # copy master(opened branch) branch to branch named feature_1git branch  # shows the all branches. (Asterix* shows you are in which branch)
creating new branch with copying master branch

Creating a new file on the feature_1 branch. And then switching back master branch. As you can see below, the new file doesn’t exist in the master branch.

Creating a new file on the feature_1 branch

I committed the new file and then switched back master branch.

git checkout "branch name"git checkout master  # switch master branch
Master branch doesn’t have the third_code.py file

As we expected, files are in the master branch were not changed.

Change branch name:

git branch -M "new branch name"  # rename currently branch namegit branch -M main # rename currently branch name as "main"

7. Merge Branches

You developed a new feature on another branch, and now you want to save these changes on one branch. It’s called merging branches.

I want to merge feature_1 branch to master branch. So, I need to switch to the master branch. I’ve already switched to the master branch.

git merge “branch name which you want to merge”git merge feature_1
merge feature_1 branch to master branch

The third_code.py file that we created on the feature_1 branch appears on the master branch.

8. Start to Github

The first thing you need to do to upload your project to GitHub is that sign up GitHub account. Then create an SSH key and add it to the GitHub account. After that, add the remote GitHub ssh link to the local repository. This step is most challenging for beginners because most tutorials don’t mention this step. And I will show you how to use multiple GitHub accounts on one pc.

First thing first, sign up for GitHub. (If you do not want to use GitHub, You can skip this part.)

Creating SSH Key

First, you need to go to the .ssh folder.

cd ~/.ssh

To creating ssh key with your Github Account Mail:

ssh-keygen -t ed25519 -C “your email”

Most tutorials show that generating ssh keys with RSA. You can do that but I wondered which is better and someone replied the same question at stackexchange.com. And ED25519 is more secure and better than RSA for ssh keys. If you wonder details you can click the link and read about it.

If you don’t have GitHub Account:

ssh-keygen -t ed25519

If you want, you can name to the ssh keys in “enter file in which to save the key”. I will say “whiteaicode”. The second thing is the passphrase, it allows encrypting keys with the password you choose.

creating ssh keys

And then ssh keys are generated. One for the public, one for private. Public ssh key ending in “.pub”. To be clear “whiteaicode.pub” is a public ssh key, “whiteaicode” is a private ssh key.

ssh keys that we created

Add Public SSH Key to Your Github Account

First, we need to read and copy our public ssh key. Realize that key is ending with our mail.

Go to your account GitHub settings:

Adding ssh keys settings on GitHub
Adding ssh keys on GitHub
ssh key that we added on our GitHub account

And that’s it. We added ssh keys to the GitHub account.

If we did not add an ssh key until this step, problems could occur while pushing and pulling, and it’s a waste of time. There are only a few steps left to complete these steps. After completing these, we will simply be familiar with using git and GitHub.

9. For Multiple GitHub Accounts

You can add new keys to accounts by following the previous steps for the same or different accounts. To save ssh keys with accounts we need to create a config file in ~/.ssh folder. I open the config file with VSCode to change it.

cd ~/.sshtouch config # to create config file
code config # to edit config file on vscode
# alternative edit file methods
gedit config # edit file on default text editor
notepad config # edit file on Windows

I’ve just created new keys for the second account.

ssh key’s config file
# Account 1 WhiteAIcode
Host github.com-WhiteAIcode # github.com-USERNAME
HostName github.com
User git
IdentityFile ~/.ssh/whiteaicode # ssh key directory and key name
# Account 2 BertramGilfoyle1
Host github.com-BertramGilfoyle1
HostName github.com
User git
IdentityFile ~/.ssh/rusher # ssh key name

10. Push code to GitHub Repo

First thing you do, create repository on GitHub.

There are two ways after creating the repo on GitHub. You will either copy the repo or add a remote link. In addition, if you have created a repo locally and started creating files without creating a repo on GitHub, you should create a repo on Github without creating any files, including the README file. After doing this, we will add a remote link to the local repo. I will show you both ways.

Creating new repo on GitHub

For now, don’t create any file like README file on GitHub!

Setting up and create GitHub repo

Give name and when you didn’t create any project file except the main project file, you can add other files such as README. Then copy ssh link. Open a terminal and go to the folder where you want to save the repository. (If you’ve already created a project and added codes on local, do not clone the empty repository and don’t create a README file or any file on GitHub.)

I didn’t create a README file on the GitHub repo.

GitHub repo SSH link

Add GitHub repo remote link to the local repo:

git remote add origin git@github.com:WhiteAIcode/test_1.git  # to bind repository with GitHub repository
# When you set config and multiple accounts, add text GitHub username as shown below (added text is bolded: "-WhiteAIcode"):git remote add origin git@github.com-WhiteAIcode:WhiteAIcode/test_1.git # in my casegit remote -v # to see the repository's remote links
ssh key’s config file
remote link added local repo

Changing local repo remote link

git remote set-url "new remote ssh link"git remote set-url git@github.com-WhiteAIcode:WhiteAIcode/test_1.git

Cloning empty GitHub repo

If you start a new project and haven’t written any code, you can first create a Github repo and then copy it to the empty folder. By the way, when creating a GitHub repo, you can add files like README. The reason I do this is to prevent conflicts in files.

After creating a new repo, clone the repo:

git clone git@github.com:WhiteAIcode/test_1.git# you can check the remote link, after cloning from GitHub.
git remote -v

Push code

I have just realized that if you have multiple accounts or global git name and email, you may push code with the email that you do not want to push to code. So, before pushing code to GitHub repo, you need to modify the git config with the mail that you want to commit code to avoid confusion. (We did that at the beginning)

# to upload(push) code to the GitHub
# -u mean: when you write git push, it means push master branch
git push -u origin master
# orgit push

Also master is the branch name that you want to push on GitHub. So you can push other branches by changing names. And when you write -u origin master once, you don’t need to write every time. You need to write git push.

push code to GitHub

We successfully pushed code to GitHub.

our GitHub repo

Note: When trying to upload the code to GitHub, you may be asked for the password you set when creating the ssh key. Enter the passphrase.

Push other branches

As you can see below, we just pushed the master branch.

Master branch pushed

We can push other branches:

git push -u origin "branch name"git push -u origin feature_1  # in my case
feature_1 branch pushed

I’ve added fourth_code.py to the feature_1 branch. As we expected, feature_1 has four_code.py but not in master branch.

Conclusion

Simply, after first-time setup I use:

git add .  # If you want to add all files to stagegit commit -m "title or short message"  -m "some description"git push

I wanted to share what I learned, thanks for reading. I hope it helps.

--

--

Ahmet Ekiz

Machine Learning Engineer | MSc. CmpE | Computer Vision | Data Science | Robotics | BSc. Mechatronics