Know about Git and GitHub in simple way✨

ajaytumula2

Ajaytumula

Posted on June 10, 2023

Know about Git and GitHub in simple way✨

What is Git ?

It is a free and open-source version control system.
If you have a code implemented and kept at some place and if you want to refer and check that code later, you can do that with the help of Git, and if you want to want to contribute to open source then you can use Git.

What is version control system ?

The management of changes to documents, computer programs, large websites, and other collections of information. Git is the version control system, that helps the software development teams to manage and control the changes made to software by each developer. Thus, keeping track of the version changes and better error detections.

What is GitHub ?

GitHub is a website or UI platform that helps us to host the repositories that is the source code folders. To use this platform, we need to know how to create a repository, clone a repository, fork a repository and the commands to interact with the developer local machine and the GitHub platform.

Initially you need to download Git, the link to download Git is 👇
https://git-scm.com/downloads

In this blog I will simplify 🤝 the approach to work with the Git repositories into three different scenarios🫵.

Scenario-1:
To create a repository, go to "your repositories" icon in your GitHub profile section, once you click on the new button then a screen to create new repository opens, where you can create a new repository by mentioning a repository name.
Once you have created the repository on the GitHub platform, then you need to clone that repository into your local machine, to clone a repository, you need to:

· Use the command git clone and copy paste the URL of the repository.
For example:

git clone https://github.com/AjayTumula/StrugglersInDev-Git.git
Enter fullscreen mode Exit fullscreen mode

· Then you can open the repository in any of your local IDE based on the programming language such as IntelliJ, Eclipse, PyCharm, Visual studio code etc.

Scenario-2:
If you want to work on other's repository or want to contribute to other's repository then you must fork the repository, that will create a copy of the repository with your name in your repository lists. You must select the fork icon on the GitHub UI. Then you need to clone the repository(URL) to get the repository or project in your local machine.

· The command to do that is 👇

git clone <<repository_url>>
Enter fullscreen mode Exit fullscreen mode

· From where you forked the project is known as upstream URL by convention so to add the upstream URL we use the command below 👇

git remote add upstream <<repository_url>>
Enter fullscreen mode Exit fullscreen mode

· The command to check the URLs, you use the below command 👇

git remote -v
Enter fullscreen mode Exit fullscreen mode

Scenario-3:
Now, lets consider if you have project or repository in your local machine and you want to add it to your GitHub. Then you need to follow the commands 👇

· At first you need to initialize git by using the command 👇

git init
Enter fullscreen mode Exit fullscreen mode

· Then an empty git repository is initialized in your local machine.
For example,
Initialized empty Git repository in C:/Users/ajayt/java/NewProject/.git/

· Then after initializing an empty git repository, we need to add all the file created and the changes made to the files using the command, git add . this command is used to add all the files. If you want to add a specific file then use the command 👇

git add <<nameOfTheSpecificFile>>
Enter fullscreen mode Exit fullscreen mode

✨ This git add command adds all the files to the staging area.

· After adding all the files to the staging area then you need to commit your changes, to commit the changes use the below command 👇

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

The message in the double quotes is used to indicate the commit and it is always represented in double quotes.

· Then you need to add the remote repository to your local repository for that you use the command 👇

git remote add origin https://github.com/AjayTumula/StrugglersInDev-Git.git
Enter fullscreen mode Exit fullscreen mode

· Once the remote repository is added, you need create a branch before you push the changes made, the command used to create a branch is 👇

git branch -M main
Enter fullscreen mode Exit fullscreen mode

· After creating a branch to push the changes to the repository we use the command 👇

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

· You can see the files changed, and insertions made to the file after pushing all the latest changes.

The below figure shows the file changes👇

Image description

You can also see all the repository setup commands once you create a repository on the GitHub platform, the below 👇 screenshot shows the following commands to create a repository on the command line.

Image description

As I mentioned staging area there is another point to consider like if you want to make changes to the file and want to keep all the changes safe at a place so that you can add and commit the changes later then you need backstage your changes that is keeping your changes in an unstaged area (If you want to keep your changes safe at some other place other than committing it or deleting it then you need to stash your changes).

Here comes the git stash command, which is used to save the local modifications away and reverts the working directory to match the HEAD commit.

⭐ Head is the pointer which points towards the branch.

· The command used to backstage the file is 👇

git stash or git stash push
Enter fullscreen mode Exit fullscreen mode

· To remove a single stashed state from the stash list and apply it on top of the current working tree state, use the command 👇

git stash pop
Enter fullscreen mode Exit fullscreen mode

· The command to clear all the stashed files is 👇

git stash clear
Enter fullscreen mode Exit fullscreen mode

· To restore a file which is deleted unfortunately, use the command 👇

git restore –staged <<nameOfTheFile>>
Enter fullscreen mode Exit fullscreen mode

Commits in the Git:

Each commit in the git are built on top of each other and Each commit in the git has a HashId.

Git and GitHub allows you to maintain the history of the project, which helps us to know which person made what change in the project.

What are branches in Git ?

The branch is something which is created to make changes, initially a user must create a branch instead of pushing the code changes directly to the main or master account.

· The command used to create a branch is 👇

git branch <<branchname>>
Enter fullscreen mode Exit fullscreen mode

· The command used to navigate to a branch is 👇

git checkout <<branchname>>
Enter fullscreen mode Exit fullscreen mode

Note: One branch can open one pull request. In simple terms one branch is one pull request. We can only open new pull request with new branches. And here if a branch has already had a open pull requests all the next commits will be committed to that branch.

· If you want to delete an extra commit in your branch then you need to use the command 👇

git reset <<hashidofcommitonebelowwhichneedtoberemoved>>
Enter fullscreen mode Exit fullscreen mode

· Then that commit or change will be unstaged or removed, after that use the command 👇

git add .
Enter fullscreen mode Exit fullscreen mode

· And now to backstage the changes use the command 👇

git stash
Enter fullscreen mode Exit fullscreen mode

· After that if you use this command you will only see the commit which you wanted the extra one will be removed from there. The command is 👇

git log
Enter fullscreen mode Exit fullscreen mode

Now, if you want to push this change you need to force push this change as the online repository contains the commit which the local repository doesn't. The command to force the change is 👇

git push origin main -f
Enter fullscreen mode Exit fullscreen mode

So, now if you want to keep your forked repository updated with the main repository then you need fetch the upstream URL. You can do this in two ways one is with option on UI by clicking on the refresh button below the code button in the GitHub platform and the other is using the command 👇

git pull upstream main
Enter fullscreen mode Exit fullscreen mode

What are merge conflicts and how we resolve them ?

In simple terms when two developers or peoples make the changes in the same line, then git gets confused about the changes made.
You need to resolve the conflict manually.

Squashing your commit:

If you have a lot of commit and want to merge that commits into one commit, then the command for that is 👇

git rebase -i <<below_commit_url>>
Enter fullscreen mode Exit fullscreen mode

Some useful Git commands and information😍:

· To initialize git in the local machine we use the command 👇

git init
Enter fullscreen mode Exit fullscreen mode

· To know about the code changes in the local machine we use the command 👇

git status
Enter fullscreen mode Exit fullscreen mode

· Any changes made to current files in the local machine are added to the staging area with the use of the command 👇

git add .
Enter fullscreen mode Exit fullscreen mode

· Example of the add command is 👇

git add <<nameofthefile>>
Enter fullscreen mode Exit fullscreen mode

· To commit the changes which are added to the staging area we use this command 👇

git commit -m <<" commit_message">>
git restore --staged <<nameofthefile>>
git log
Enter fullscreen mode Exit fullscreen mode

· Command to delete the file (this is the linux command) 👇

rm -rf <<nameofthefile>>
Enter fullscreen mode Exit fullscreen mode

· Command to delete file in windows is 👇

git rm <<nameofthefile>>
Enter fullscreen mode Exit fullscreen mode

· Command used to unstage the commits or remove the commit just copy past the hasdid of the commit which is just below it, the command is 👇

git reset <<hashidofthecommit>>
Enter fullscreen mode Exit fullscreen mode

· To attach the URL to our project, use the command 👇

git remote add origin <<URLOfTheGitRepository>>
Enter fullscreen mode Exit fullscreen mode

· Command that shows all the URLs attached to the main repository is 👇

git remote -v
origin https://github.com/AjayTumula/StrugglersInDev-Git.git (fetch)
origin https://github.com/AjayTumula/StrugglersInDev-Git.git (push)
Enter fullscreen mode Exit fullscreen mode

· Command used to push the changes to the origin URL and the main branch is 👇

git push origin main
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ajaytumula2
Ajaytumula

Posted on June 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related