Git & GitHub (A-Z)
Since you know what is Git and what it is used for I won’t go in that detail just talk about the commands and their usecases. I’ll try to be as concise as possible so that we don’t have to watch the same youtube videos again and again.
So there are 2 types of repositories :
1) Local Repository (the one residing on your system locally)
2) Remote Repository (the one which is located on a remote server)
Let’s take a look at the commands for execution on the local repository :
Git config -
After you have installed the Git program on your system you configure it with the ‘config’ command — things like your username and email-id.
To check that you have configured all the details correctly you can run the command — $ git config --list
Git init -
> Initialising an empty repository (locally).
> To create a new project/repo.
> Executing in a folder leads to .git directory on the directory indicating that you are currently working on this repository locally.
$ git init
Initialised empty Git repository in C:/usr/…/.git/
To see the contents of the hidden .git folder you can run ls-lart command
Git diff -
> Used to check the changes made to your files.
Git status -
> Tells you the status of the working directory and the staging area.
$ git status
This will show you the untracked files in red color which are the files to which you made some changes.
Git add -
> This command adds the newly created files or modified files to the staging area. The staging area is a like a storage of files before you have commited to them. We’ll come to commiting .
> To add some files we made changes to …to the staging area.
> Run this before running commit command.
if you run the status command after making some changes you will encounter the files which are untracked so you need to add them to the staging area.
$ git add .
Git commit -
> The commit command is used to make sure that all the changes you made to the files are saved on the repository (the files in the staging area so be sure you have added only the files you didn’t commit to already otherwise you will commit to files to which you didn’t want to).
> While creating a new version by introducing some changes to some file.
$ git commit -m “message”
When you create a new repository through the github webapp you get the instructions to clone or add files to it we are interested in the following :
$ git remote add origin https://github.com/user/repo_name.git
Executing this command establishes a link between your local repo and the remote repo .
After this and before pushing you execute the following command:
$ git remote -v
Git merge -
> Merge a branch to the main branch or any branch for that matter.
Now let’s take on the commands for the remote repos:
Git push -
> To push changes from local to remote repository.
$ git push -u origin main
After this if you refresh the github repository page you will see that the files have been uploaded (pushed) to the remote repository. HURRAY!!
Now this is all said and done but how will you push any new folders or files that you create after pushing initially or how will you update the remote repository to the same level as your local repository ??
After creating a new folder/file in your local repository you again run the git add . command to view the newly followed by git commit -m “new folder created” and ultimately git push -u origin main
Git pull -
> To fetch and merge the changes from the remote repo to the local repo.
> A combination of fetch and merge commands.
- origin/main is the state of master branch on remote repository
- main is the state of master branch on local repository
$ git pull <url or id of your repository>
You pull contents of a remote repo to your local system then make changes to it and then push it back to the remote server. Suppose you make a directory in which you add some files and wish to have on the remote server. What you do is , from the command line you go to the directory where you created the desired file/folder and run the git init command to initialise an empty directory. Then on the github webapp you go to the repository to which you want to ultimately push your changes and copy the cloning url provided.
And now run the git pull <cloning url> and all the files from you remote repo are now present on your local system also. Running the git status command will show you the changes you made from the previous commited version of your project.
After this you run the git add . followed by git commit -m “changes made”
Again , git remote add origin <cloning url > , git remote -v , git push -u origin master.
After this if you refresh the github repository page you will see that the files have been uploaded (pushed) to the remote repository. HURRAY!!
$ git pull origin main
This command copies all the files from the master branch of the remote repo to local repo.
ADVANCED GIT OPERATIONS
- Git branch- A branch represents an independent line on which the process of work , edit , delete , commit can be done. This command allows you to create, list, delete a branch .
- Git merge- To unify multiple commits to a single or merge two branches together.
If you want to view all these commands in action , chech this video by simplilearn →
<iframe width=”770" height=”433" src=”https://www.youtube.com/embed/ev_byvSWvr0?list=PLEiEAq2VkUUJs7lyLgSsRlnd9syrFBzSM" title=”YouTube video player” frameborder=”0" allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture” allowfullscreen></iframe>