Git and Github

Ebube .E
7 min readNov 27, 2022

--

Let’s discuss git. A version control system, such as git, is necessary if you are a software developer or find yourself needing to manage a project in a way that allows several individuals to separately work on features and contribute to the same project.

By creating a snapshot of a project’s current state at various points during development or production, a version control system often maintains the integrity of a project.

In the event that the current system is broken or is no longer advantageous, this enables the developers to go back to the earlier snapshot.

Git works by designating a primary (master) branch that keeps track of a project’s various development phases. Information about the project that is ready for production is often kept in this main branch.

Git also enables team collaboration between developers by enabling them to branch off from the main branch, make modifications, and if necessary merge their work back into the main branch.

By opening pull requests, which effectively clone a project or branch of a project with the goal of adding, removing, upgrading, or repairing features, developers can also contribute to the work of other developers (open source).

The project owner then evaluates these improvements and, if satisfied, incorporates them into the main project.

There are some commands and hints you should be aware of as a novice using git, and I’ll be presenting them in this article

How to open an account on GitHub

You must register for a GitHub account in order to store changes to your projects on a remote server and fully utilize git.

A dedicated computer located somewhere in the world with the fancy name of a remote server will virtually always have your data available for you. “Cloud storage” is a common term used to describe this.

Similar services are offered by other platforms like GitHub. GitLab, Bitbucket, Gitea, and Source Forge are a few of them. But as GitHub is the most widely used, it will be the focus of this article.

Go to https://github.com in any browser of your choosing, click the signup button, and then follow the on-screen instructions to create a GitHub account.

Congratulations! Your GitHub account is now yours.

How to set up a GitHub repository

Create a repo or repository once you’ve signed up for a GitHub account. Consider it a container in which to keep your project.

Follow these steps to create a new repository:

Find and click on the green “create repository” button in the left sidebar. Alternatively, you can find and click on the + icon next to your profile image in the top right corner of your system. There will be a drop-down menu. Pick “new repository” from the menu.

GitHub will now prompt you for a name for your repository as well as a succinct description of the repository. The latter is optional; only the former is necessary. However, either of them may be updated at any time.

The next decision is whether you want to keep your repository public or private.

A public repository may be viewed by anybody, but it cannot be written to without your express permission, whereas a private repository cannot be read or written to without your express permission.

It’s ok to start your repo as public if you’re a beginner.

You’ll also be prompted to decide whether to add a README.md file. This document was created using the markdown markup language, which is used to format plain text.

Normally, it includes a thorough description of the repository, including but not limited to: The project’s title and objective, directions for using it, dependencies, an invitation to work together

However, you have the option to omit this file for the time being.

After making the aforementioned settings, click the green button to start building your repository. If you decided to add a readme file when you created the repository, GitHub will automatically initialise it and bring you to a screen similar to the one below.

The repository can now be copied to your own device. Details are provided below.

If not, a page with instructions on how to initialise your project and push (submit your project to your repo) will be redirected to you. It will resemble this:

You currently have three choices:

  • Start your project by cloning the repository to your local device.
  • Setup git manually in the project folder of your empty project.
  • Create a manual git initialization in your current project.

You only need to choose one of the three steps, which are all very simple to follow.

For your repository to be locally cloned…

Run the following two commands after opening a terminal or command line interface on your computer, navigating to the directory where you want to build your project:

git init

git clone <repo_url>

Where <repo_url> will be substituted for your repository URL. You can obtain this URL from your repo when you create without a readme. It’s just at the top of the page above the two big code blocks.

If you initialized your repo with a readme then you’ll find the <repo_url> if you click the green button that says “code”.

It’s crucial to remember that running these commands on your computer requires that git be installed and set to your PATH environment variable. You can download the right version of git for your operating system here.

The command git init initialises a git repo in your current working directory. After running it, a folder called .git will be created to store all your git information. This singular folder manages all your local git operations and is where your data is stored when you stage files and make commits.

Git is told to look for a git repository at the URL denoted by repo url> and, in essence, copy all of its contents to your local machine in your current working directory by using the command git clone.

Your project will be initialised on your local machine and synced with your remote on GitHub after successfully executing these two commands.

Congratulations — you can now start working your magic.

Setup git manually in the project folder of your empty project.

For this process, you’ll have to make use of the code in the first block labelled (…or create a new repository on the command line).

echo “# my-first-repo” >> README.md:

In your current working directory (cwd), this command generates a new file named README.md and appends the string “# my-first-repo” to it.

Git init:

This initializes git in your CWD, as was already stated.

add README.md to git:

The README.md file is staged by this command. It says to git, “Hey, pay attention to me and keep track of me.”

git commit -m “initial commit,”:

This instructs git to take a snapshot of the current state of the files being tracked, which as of now only includes the README.md file but will eventually include all files you add using the git add, git branch -M main command.

The -M option modifies the command to rename the previous branch produced by default to this new branch while the git branch command produces a new branch named “main.”

So if branch “master” was created initially, running this command will change the name “master” to “main”.

The name “master” will become “main” after executing this command.

git remote add origin repo url:

When you reference the name “origin” when pushing, this command adds the repository at repo url> as a remote with the name “origin”.

git push -u main origin:

This command pushes all committed changes to the remote called “origin”main” “‘s branch. The default location (upstream) to push to is set to origin main by the -u option.

Initialize git manually in your already existing project folder:

It might be the case that you’ve been working on your beautiful project for a while and just now decided to push it to GitHub. If this sounds like your current situation, here’s what you need to do:

git remote add origin <repo_url>

git branch -M main

git push -u origin main

The functions of the above commands have already been explained above.

Make sure to initialize git, add (stage) your files to be tracked by git and commit your changes if you haven’t before running the above commands.

With this, you have successfully initialised your project with git, made your first commit and pushed it to GitHub.

This command adds the repo at <repo_url> as a remote named “origin” which you can then push to by referencing the name “origin”.git push -u origin main:

This command pushes all committed changes to the “main” branch of the remote named “origin”. The -u option sets origin main as the default location (upstream) to push to.Initialize git manually in your already existing project folder

A mastery of git and Github will help developers in dispensing their professional duties.

If you found this useful, leave 50 claps to show your thanks. Also, follow me for more content like this, and let me know your thoughts in the comments.

Thank you for reading. Ciao!👋

--

--

Ebube .E

Bringing you insights about programming, minimal living, psychology and mental health and everything in between