Article on GIT & GITHUB
Git is a version control system used for keeping track of changes of any file uploaded on Github.
Difference between Git and Github :-
GIT :
- Git is mainly used to keep track of changes in any set of files.
- A group of people can work on the same code without rendering the main code and add or remove changes according to their requirement.
- Its goal include speed, data integrity and support for distributed, non-linear workflow.
GITHUB :
- Github is a cloud-based hosting service that lets you manage Git repositories.
- It offers all of Git's DVCS SCM and have some additional features. As it has all the codes in one place in the same repository making it easy code hosting.
- It has a feature of pull requests and issues in which all the developers can stay on the same page and organize.
Q1. What is the difference between pushing and pulling?
PUSHING : The push command is used when one has to upload or send the commits to a remote repository from a local repository. It sends content or codes to another repository.
PULLING : The pull command helps to fetch code or files from a remote repository to one local repository. It also pulls any request from the Git repository and merges them to a local repository.
Q2. How to initialize a new git repository (describe all the steps)?
- Create a directory to store the project.
- Go into the new directory.
- Enter the command "git init".
- Write some code.
- Enter "git add" to add the files.
- Lastly, enter "git commit" to commit all the files.
Q3. What is the use of a git clone and how to use it?
Cloning is a process of creating an identical copy of a Git Remote Repository to the local machine.
When we clone a repository, all the files are downloaded to the local machine but the remote git repository remains unchanged. Making changes and committing them on your local repository (cloned repository) will not affect the remote repository that you cloned in any way. These changes made on the local machine can be synced with the remote repository anytime the user wants.
Steps to Clone a Repository or use Git Clone Command :-
- To clone a repository, go to the repository page which we want to clone.
- Press "Clone or download button".
- Copy the code or web URL that appears after pressing the button.
- Now, open "Git bash" on your system.
- Change the current working directory to the location where we want the cloned directory.
- Press "git clone" and then paste the URL we copied earlier.
- The git_clone_successful message will appear as we press "enter".
- Confirm the cloning by listing the directories once again using the ls command which lists all the files and folder.
Q4. How to ignore some files/folders from pushing?
To ignore some files, we can create a .gitignore file in our repository's root directory to tell Git which files and directories to ignore when we make a commit. To share the ignore rules with other users who clone the repository, commit the .gitignore file into your repository. There is no explicit command to make a .gitignore file, instead, users have to make the file themselves and edit it accordingly.
Steps to create a .gitignore file :-
- Open "Git Bash".
- Navigate to the location of your Git repository.
- Create a .gitignore file for your repository.
The files mentioned in the .gitignore file should match the exact same name as the original file, else it will be ignored and the purpose of the .gitignore file will not be accomplished.
Q5. What do you mean by Branch?
Branch is an independent line of development. It works as a pointer to our next commits. Whenever a new branch is created, Git creates a new pointer while keeping the original code base untouched.
While some will be fixing bugs the others would be implementing new and different features. The problem raises, how to maintain different versions of the same code base?
This is where the branch function shines! Branch allows each developer to isolate his/her work from others by creating a new branch from the original code base.
It consists of five types of branches, each with different roles :-
- Master branch : Upon making the first commit in a repository, Git will automatically create a master branch by default. Subsequent commits will go under the master branch until we decide to create and switch over to another branch.
- Feature branch / Topic branch : When we start working on a new feature/bug fix, we should create a feature/topic branch. A feature/topic branch is normally created off a develop/integration branch. This feature/topic branch can reside in our local machine throughout the entire development lifecycle of the feature. We will push this branch to the remote repository whenever you are ready to merge the change set with the develop/integration branch.
- Release branch : When we roll out a new release, we create a release branch. A release branch helps us to ensure that the new features are running correctly.
- Hotfix branch : When we need to add an important fix to our production codebase quickly, we can create a Hotfix branch off the master branch. The advantage of a hotfix branch is that it allows us to quickly issue a patch and have the change merged with the master branch without having to wait for the next release.
- Develop branch / Integration branch : A develop/integration branch should be kept stable at all times. This is important because new branches are created off of this branch, and this branch could eventually go out live on production.
(a) Which branch should be used to keep deployment-ready code?
Production Branch (Develop)
(b) Create a new branch called development from the main branch.
git checkout -b development (Now, we are in the master branch and created development branch)
(c) Check out one more branch deployment from the previous branch.
git checkout development (checkout in development branch from master branch)
git checkout -b deployment (Now, we are in the development branch and created deployment branch)
(d) Push different data into both branches.
git push (Push data in development branch)
git checkout deployment (checkout in deployment branch from a development branch)
git push (Push data in deployment branch)
(e) Merge data from both branches to the main branch.
git checkout master (checkout in master branch from deployment branch)
git merge development (Merge development branch with master branch )