Git v/s GitHub
Sayyed Nargis Fatima
ERP Business Analyst at Schneider Electric, Bangalore, India.
Wait! Git and GitHub are different?! Let us see how!...
Git - Git is a revised control system which is installed "locally" and aims on controlling the version control and code sharing.
GitHub - GitHub is a web-based hosting service which is maintained by Microsoft for creating git repositories (directories). It allows hosting central repositories on remote server.
Therefore, Git is a tool which is used by GitHub.
Few terminologies which need to be go through before digging further into Git and GitHub.
Repository
This can be imagined as a folders in your system which includes several files in it. Likewise repositories are same as the folders in GitHub which contains all the project's files. There are two types of repositories.
- Remote Repository - When you want to share your data with your teammates then Remote Repository comes into picture. It can be compared with the "file server" that you used to exchange data with your colleagues. This repositories are hosted on a server that is accessed by all the teammates.
- Local Repository - This repositories are located on the local machine of each team members. Maximum of Version Control related work like staging, committing, viewing log files, etc. are done in the local repository.
Branching
It similar to the branch of trees as there are multiple branches in a single tree, branching in GitHub happens when you want to work with multiple features at the same time. Like tree has its trunk which is the main branch starting from the root, in GitHub there is also main branch called as Master Branch.
Commit
This command is used to save the changes to your repository. For this you have to execute the commit command to save the changes to your local repositories as it does not get autosaved.
But it does not commit the changes to your remote repositories. For this, push and pull commands are executed which are explained below.
Pull
This is used to fetch and merge the changes on the remote server to your working directory.
Push
The process of uploading or pushing the commits (saved changes) from local repositories to remote repositories is called pushing.
Merge
This operation is used to combine or merge the changes into Master branch. Master is nothing but the naming convention of the main working directory.
Clone
Clone with the name itself will make the copy of the target repository to the system.
What is the difference between pushing and pulling?
Push
Pushing sends the recent saved activity which is committed from your local to the GitHub. This command sends your changes to GitHub so that they can be shared to other team members. Pushing is opposite of Pulling. Pushing will commit the remote repositories whereas Pulling will commit the local repositories.
Some Push commands :
$ git status
As status in the command, this will let us know about the last push that we have committed or the local changes that are pending.
$ git push <remote>
This command will where is the remote target to generate the local branch. It will join together the changes from local repositories to the remote repositories.
$ git push origin master
This will push the changes in the branch master (Main Branch) to the remote location.
Options available in Git Push command :
$ git push -prune remote ABC
The -prune option will delete the ABC branch from the remote repository.
Note: It will delete the branch if there is no branch with the same name in local repository.
$ git push -dry-run <remote> <local_branch>
Dry Run option in git push command will only show the execution of the git push command but it will not send any update to the remote repository.
$ git push -atomic <remote_repo> <working_branch>
This command will provide atomic operation that is either all are updated or nothing is updated at all on the remote repository.
$ git push -all <remote>
By using -all option in Git, it will push all the branches and their last committed changes to the remote repository.
Pull
Pull with the name itself depicts that the user is trying to fetch something from the repository.
Git Pull can perform two operation fetch and merge in the single command. So if the user wants to fetch and perform merge operation then Git Pull command can be used.
Note: Git fetch, and Git merge is used together for merging the changes and then accepting it.
$ git pull
Executing pull command will merge the changes without notifying the user.
But this can lead the user in puzzlement that when to use fetch and when to use pull commands?
It is considered as safe to use fetch instead of pull when the user is new to Git. One can use Git Pull command only if the user is confident enough to work with it or is an experienced in using Git.
Suppose one of your team mates will ask you to go through some changes they have done on branch and tells you to merge if you like it. But now you are not sure to merge the changes you will first fetch the changes and then review it and then merge it. So Git Pull is use when one is working alone on the branch since you alone are going to review it you can directly pull them to your repository.
Options available in Git Pull command :
$ git pull --no-commit
The no-commit option in the git pull command will pull the changes but the merge committed list will not be displayed.
$ git pull --rebase
This commands creates a linear history of commits after the merge of one branch into another.
Note: Git rebase makes the developers and testers difficult to recognize the small commits and changes in the repositories.
If only one member is working on the repository then it will be easy for that member to use Push. However, when there are multiple members who want to access the same repository then it will need to get Pull.
How to initialize a new git repository?
start a new repository from scratch, you need to write the following command :
$ git init
- Creating new repository from scratch :
- First you need to create a directory or a repository in which your files and projects will be there.
- After creating the new directory, go into that directory and type the git command : $ git init.
- Then write your code if needed otherwise type $ git add command to add the files in the repository.
- After adding the file, type $ git commit to save the project.
- Creating new repository from an existing project :
- Go into the directory in which you have already created the project and type $ git init. Here, you have the existing project that is going to be tracked by using git.
- Then type the command $ git add to add or upload all the files in the directory.
- If you don't want to track all the files (only the specific files) then you can use the command $ git add .gitignore.
- At last you need to save the changes, for this, type $ git commit.
What is the use of git clone and how to use it?
Here, clone means to make the copy of any file or repository. The git clone command will copy an existing repository which the user can add/remove or merge the files and push larger commits to the cloned repository. Following command is used to make the clone of any repository.
$ git clone <url>
This command will download the copy of the original repository including all the files, branches and also commits.
By cloning a repository to your local machine, it pulls down all the data that GitHub contains. You can push the changes made in the remote repository in the GitHub or pull other person's changes on the GitHub.
Using git clone with SSH :
Cloning with SSH will provide authenticate remote server. You need to specify SSH path for the repository instead of providing the URL. Basically, developers are authenticated with SSH from machine level i.e.; you can make clone with HTTPS or with SSH but not both for your repositories.
Common usage and options for git clone :
$ git clone --mirror
This will clone the repository but without being able to edit any of the files. It is used when you want to make a secondary copy of repository and match all the branches.
$ git clone --sparse
This will only fill the files which are present in the root directory. --sparse command is used when you want to clone large repositories which contains many directories or sub-directories.
$ git clone --recurse-submodules[=<pathspec]
It will make sure that all sub-modules are cloned and initialized once the main project has be cloned within the provided path. This option will save the time to manually initialize and update the modules.
$ git clone --single-branch
As the name single-branch, this command will clone only a single branch.
How to ignore some files/folders from pushing?
While working on projects you want exclude some files from being pushed to the remote repository. To ignore some files or folders in git you will need to apply the command .gitignore which specifies what files git should exclude.
Global .gitignore :
Git allows you to create "global .gitignore" file where you can define your own ignore rules for every git repository on your local machine. You can give any name to this file and can be stored in any location. I will recommend to store in the home directory as it will be easy to configure it from there.
Lets have one example, here we are setting ~/.gitignore_global as the Global Git ignore file, following are the step to perform :
- Creation of a file.
$ touch ~/.gitignore_global
2. Adding file to configure Git.
$ git config --global core.excludesfile ~/.gitignore_global
3. Now open the file with any text editor to add some ignore rules.
Note: Ignore Rules are patterns that are limited to your local repository and are not shared to other repositories.
Local .gitignore :
This file is placed in the root directory of any repository. Local .gitignore files can be shared with other developers and should contain patterns which can be used by other users of the repositoriy.
Ignoring previously committed files :
To ignore the committed files you first need to unstage it and then remove the file from the index, after that add rules for the files in .gitignore .
$ git rm --cached filename
--cached option will not delete the file from working but only removes it from the index. So if you want to delete the file from both index and local system, don't provide --cached option.
$ git rm -r -n directory
When you want to recursively delete the directly use -r option and -n will performs a dry run and display what files will be removed.
To display all the ignored files :
S git status --ignored
The status command with --ignored option will display all the ignored files.
What do you mean by Branch?
As explained above, branching occurs when you want to work on multiple attributes at the same time. It allows you to take the original repository, create replica and make changes and then submit your changes to get merged into the original repository.
For creating a local branch in Git, ensure that you have a clean working directory (no commits) using the $ git status command. Then open your Git Bash and go to the local working repository and type the following command to view all your branches :
$ git branch
- Which branch should be used to keep deployment-ready code?
Lets assume that you are working on a project with your teammates. So how you will make sure that you have a stable version and it is ready for deploy when you have multiple teammates working on it?
So the answer to this question is by creating multiple branches. Every repository in GitHub can have multiple branches.
The main branch or the Master branch, is the stable version of your code. This branch is used to keep deployment-ready code. This branch is never edited directly when you are working on it simultaneously.
So, when you are working with your teammates and you want to make some changes, you would create a new branch from the master branch and when all set (changes made and your code is ready) you would request your changes to get merged into the master branch.
- Create a new branch called development from the main branch.
To create and list a new branch using Git Bash (CLI) use $ git branch <branch_name> and to switch to a newly created branch type $ git checkout <branch_name> command.
$ git branch development $ git checkout development
- Checkout one more branch deployment from the previous branch.
$ git checkout -b deployment development
- Push different data into both branches.
$ git push origin development $ git push origin deployment
- Merge data from both branches to the main branch.
$ git checkout development $ git merge main
Now what are you waiting for?! Start your first commit!. Because
“The most effective way to do it, is to do it.”
-Amelia Earhart
Happy Learning!
Enthusiastic recent graduate with a strong belief in the power of continuous learning and embracing challenges to stay ahead of the rapidly evolving tech landscape.
3 年Well said
Proprietor - DROPEX WATERPROOFER
3 年Good Job ??
NISM CERTIFIED INVESTOR | ENGINEER
3 年Thanks for posting
Production Infrastructure Support Engineer
3 年Good Job
Software Engineer at JPMorgan Chase & Co.
3 年Well done ??