The ultimate guide to Git and GitHub

ยท

9 min read

The ultimate guide to Git and GitHub

What is Git and GitHub?

Git is a version control system that lets you manage and keep track of your source code history. from this line, the question that comes to your mind is what is a version control system?. version control system is a kind of software that keeps track of the changes you made to file or set of files over time so that you can go back to specific versions later. GitHub is a cloud-based hosting service that lets you manage Git repositories. If you have open-source projects that use Git, then GitHub is designed to help you better manage them.

How to install Git?

on windows, you can go to the official git website and download from there install git. Git provides an additional tool called Git Bash. Git Bash is an application for Microsoft Windows environments that provides an emulation layer for a Git command-line experience. by using this you can easily run the git commands.
Screenshot (64).png

on macOS or Linux you can download it from the terminal by using the below command :

sudo apt-get install git

git-install-linux.jpg

Setting Global Git Username and Password

To work with Git, you have to first set up a user name and password. Git uses a username to associate commits with an identity. The Git username is not the same as your GitHub username. you can use the below command to set up your user name and password. and you can change this at any time.

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

once done, you can confirm that the information is set by running:

git config --list

Repository

Repositories in GIT contain a collection of files of various versions of a Project. These files are imported from the repository into the user's local server for further updations and modifications in the content of the file. you can initialize a Git repository by using the below command:

Git workflow

Screenshot (79).png

1. Git init command

git init

Screenshot (66).png it will create an empty repository inside your working folder. in simple terms, the repository is like a container where we can put all of the files.

WARNING!.gif

2. Git status command

Git status gives information on the current status of a git repository and its content. after initializing the git repository, if you make any changes inside a repository, you can fire this command to see the changes. I added index.html, index.js, and style.css files inside the git-blog repository. file changes will show in red color text as you can see in the below image :

Screenshot (68).png

3. Git add command

We use the git add command to stage changes to be committed. It's a way of telling Git "please include this change In our next commit". use the git add to add specific files to the staging area. separate files with spaces to add multiple at once.

git add file1 file2

we can use the below command to stage every file -

git add .

After staging files, your git repository will look like this - Screenshot (88).png

4. Git commit command

We use the git commit command to actually commit changes from the staging area. When making a commit, we need to provide a commit message that summarizes the changes and work snapshotted in the commit.

git commit -m <commit message>

commit.png

you can use alternate commands for staging and committing files at the same time-

git commit -a -m <commit message>

Branches

Branches are an essential part of Git!. think of branches as an alternative timeline for a project. they enable us to create separate contexts where we can try new things, or even work on multiple ideas in parallel. if we make changes on one branch, they do not impact the other branches(unless we merge the changes).

branch_workflow.jpg The Mater Branch

In git, we are always working on a branch, The default branch name is master. it does not do anything special or have fancy powers. it's just like any other branch. In 2020, Github renamed the default branch from master to main. The default Git branch name is still master, though the Git team is exploring a potential change.

head.png What is HEAD? We'll often come across the term HEAD in git. HEAD is simply a pointer that refers to the current "location" in your repository. It points to a particular branch reference. HEAD is always pointing to the latest commit you made on the master branch.

Use git branch to view your existing branches. The default branch in every git repo is master, though you can configure this.

Creating Branches

Use git branch <branch name> to make a new branch upon the current HEAD. This just creates a branch. It does not switch you to that branch (the HEAD stays the same).

Once you have created a new branch, use git switch <branch name> to switch to it. Use git switch with the -c flag to create a new branch AND switch to it all in one go. switch-branch.png

Git Checkout command

The checkout command does a million additional things, so the decision was made to add a standalone switch command which is much simpler. this command will create a branch for you and switch you to that branch.

git checkout <branch name>

Merging

Branching makes it super easy to work with self-contained contexts, but often we want to incorporate changes from one branch into another!. We can do this using the git merge command.

Remember these two merging concepts:

  • We merge branches, not specific commits.
  • We always merge to the current HEAD branch.

To merge, follow these basic steps:

  1. Switch to or checkout to the branch you want to merge the changes into (the receiving branch)
  2. Use the git merge command to merge changes from a specific branch into the current branch.

I created a new branch called blogData and I want to merge blogData branch into the master branch. for that, I will have to switch to the master branch in order to merge blogData branch into it. you can see the image below -

git-merge.png

Merge conflicts

Depending on the specific changes you are trying to merge, Git may not be able to automatically merge. This results in merge conflicts, which you need to manually resolve.

Resolving commits

Whenever you encounter merge conflicts, follow these steps to resolve them:

  1. Open up the file with merge conflicts

  2. Edit the file to remove the conflicts. Decide which branch's content you want to keep in each conflict. Or keep the content from both.

  3. Remove the conflict "markers" in the document

  4. Add your changes and then make a commit!.

Finally we made it, Now we are on the second section of this article

Creating a repository on Github

We can create a repository on Github by clicking on the top right plus icon or by clicking on create a new repository button. you can see below-

Github.png

Cloning

So far we've created our own Git repositories from scratch, but often we want to get a local copy of an existing repository instead.

To do this, we can clone a remote repository hosted on Github or similar websites. All we need is a URL that we can tell Git to clone for use.

git clone

To clone a repo, simply run git clone <URL>. Git will retrieve all the files associated with the repository and will copy them to your local machine. In addition, Git initializes a new repository on your machine, giving you access to the full Git history of the cloned project.

git clone <url>

Make sure you are not inside of a repo when you clone!

How Do I Get My Code On Github?

Option 1: Existing Repo

If you already have an existing repo locally that you want to get on Github....

  • Create a new repo on Github
  • Connect your local repo (add a remote)
  • Push up your changes to Github

Option 2: Start From Scratch

If you haven't begun work on your local repo, you can...

  • Create a brand new repo on Github
  • Clone it down to your machine
  • Do some work locally
  • Push up your changes to Github

Remote

Before we can push anything up to Github, we need to tell Github about our remote repository on Github. We need to set up a "destination" to push up to. In Git, we refer to these "destinations" as remote. Each remote is simply a URL where a hosted repository lives.

To view any existing remotes for your repository, we can run git remote or git remote -v.

Adding a new remote -

A remote is really two things: a URL and a label. To add a new remote, we need to provide both to Git.

git remote add <name> <url>
git remote add origin https://github.com/deepak-webdev/crown-clothing

Screenshot (95).png The above command will tell Okay Git, anytime I use the name "origin" I'm referring to this particular Github repo URL. origin is a conventional Git remote name, but it is not at all special. It's just a name for a URL.

When we clone a Github repo, the default remote name set up for us is called origin. You can change it.

Pushing

Now that we have a remote set up, let's push some work up on Github! To do this, we need to use the git push command.

We need to specify the remote we want to push up to AND the specific local branch we want to push up to that remote. If you remember option 1 where we have a local git repository -

Screenshot (94).png

git push origin master tells git to push up the master branch to our origin remote.

Fetching

Fetching allows us to download changes from a remote repository, BUT those changes will not be automatically integrated into our working files. It lets you see what others have been working on, without having to merge those changes to your local repo. Think of it as "please go and get the latest information from Github, but don't screw up my working directory".

Git Fetch

The git fetch <remote> command fetches branches and history from a specific remote repository. It only updates remote-tracking branches.

git fetch origin would fetch all changes from the origin remote repository.

git fetch <remote>

if not specified, remote defaults to origin.

Pulling

git pull is another command we can use to retrieve changes from a remote repository. Unlike fetch, pull actually updates our HEAD branch with whatever changes are retrieved from the remote. "go and download data from Github AND immediately update my local repo with those changes".

Git Pull

git pull = git fetch + git merge

To pull, we specify the particular remote and branch we want to pull using git pull <remote> <branch>. Just like with git merge, It matters WHERE we run this command from. Whatever branch we run it from is where the changes will be merged into.

git pull <remote> <branch>

Conclusion :

Finally, you survived that was a lot of information to take in. and I am sure after reading this full article you are able to push your first code on Github. If you have any suggestions please let me know in the comment section.


Let's connect on LinkedIn | Github to learn and grow together.

๐Ÿ‘‹ Thanks for reading, See you next time.

ย