GIT AND GITHUB BASICS

GIT AND GITHUB BASICS

If you have entered in programming world then one must have asked you about GITHUB.

So if you don't know about Git or GitHub yet no worries! We will cover few basics and will try to understand concepts in very simple language as possible.

What we will cover in this article


1. What is Git & GitHub?
2. What is the difference between pushing and pulling?
3. How to initialise a new git repository[ Describe all the steps ]??
4. What is the use of git clone and how to use it?
5. How to ignore some files/folders from pushing?
6. What do you mean by Branch?
       ? Which branch should be used to keep deployment-ready code?
       ? Create a new branch called development from the main branch.
       ? Checkout one more branch deployment from the previous branch.
       ? Push different data into both branches.
       ? Merge data from both branches to the main branch.
7. How to resolve conflict with merge?
8. What is rebase and how is it different from merge in git?        

So before we dive into our main article, we need to understand one term i.e.Version Control.

*Version Control---It is a system that records changes to a file or set of files that has been made over time called 'versions'.These versions will help us to keep track over the changes made in code/project.Now versions comes very handy to reduce conflicts in code/files when many people are working on same project.

*Repository---In simple understanding, it is a place where we can store our files(code/project files), as well as history of changes made to those files.

*Commit---It is a change that you made to a file(code/set of files) and saved it.This allows to keep record of what changes were made when and by who.

You will understand above terms as we go through.

1. What is Git & GitHub?

* Git : It is version control system(basically a software) that let us manage and keep track of changes to our code/files over time on our local machine(own pc). It is good for individual purpose.

* GitHub : It is a cloud-based service that let us manage our version repositories.In lay man language it is like a social media platform for developers from all over the world where they can come together to share there work(codes) and help each other in developing projects.

Basic Git Commands

  • git config?
  • git init
  • git add?
  • git diff
  • git commit
  • git reset
  • git status
  • git merge
  • git push
  • git pull

2. What is the difference between pushing and pulling?

*Push means when you have to save your code to git.

*Pull means when you want to retrieve existing code from the git.

We use push and pull by using command line.The git push command is what we use to send or push commits or files from our local branch to the remote repository we created on GitHub.(we will learn how to create new repository on GitHub)

The git pull command is the opposite of the push command. While the push is used to send to the remote repo, the pull command on the other hand is used to update our local repository from the remote repository.

3. How to initialise a new git repository?

Before we initialise new git repository we need to install git on our system.

If you are on Windows, then you just?download the installer?and run it.

If are on Mac, you just need to open up the terminal and type?git.

For Linux, open the terminal and type?sudo apt-get install git-all.

After installing Git, create a?GitHub?account.

Now go to your terminal and introduce yourself to Git! To set your username for?every repository?on your computer, type

git config — global user.name “<your_name_here>”        

Now tell Git your email, and make sure it’s the same email you used when you signed up for GitHub

git config — global user.email “<your_email@email.com>”
        

After that, you are ready to create your first repository. Go to GitHub. Click on the plus sign(+) on the upper right corner and select “New repository”.

Type your repository name and click on “Create repository” and you are all set.

Although there are a couple of options for?git init, you will almost?always?use it with?no?other arguments:

$ git init        

This command will create a new and empty Git repository in the current working directory. It doesn't matter if other files in this directory existed or not; the command only creates the?.git?repository folder.

If you would then like to put your current project files under version control, you can make your first commit:

# Add all files to the staging area (= tell Git to include them in the next commit)

$ git add .

# Wrap these changes in a commit and save them to the local Git repositormit"

$ git commit -m "First commit"        

4. What is the use of git clone and how to use it?

Cloning a repository is pretty much exactly what it sounds like. It takes the entire online repository and makes an exact copy of it on your local machine.

Commands to clone a Repo from GitHub:

git clone?https://github.com/username/myfirstrepo.git

cd myfirstrepo?— or whatever you named your repo.        

5. How to ignore some files/folders from pushing?

We can ignore certain files and directories — that is, exclude them from being tracked by Git — by creating one or more .gitignore??files in your repository.

When a file or directory is ignored, it will not be:

  1. tracked by Git
  2. reported by commands such as?git status?or?git diff
  3. staged with commands such as?git add -A

6.What do you mean by Branch?

No alt text provided for this image

We can think of our git repo as a tree. The trunk of the tree, the software that goes live, is called the Master Branch. That’s the one that goes live. The branches of that tree are, well, called branches. These are separate instances of the code that offshoots from the main codebase.

7.Which branch should be used to keep deployment-ready code?

First Create a new branch called development from the main branch. First, make sure to?cd?into your local repository. Once you’re in the right folder, execute

$ git branch <branch-name>        

This will create a new branch. But before you start making changes to your code, you will have to first switch to the new branch you just created. To do that, run

$ git checkout <branch-name>        

Many developers, especially when they’re just getting started, forget switching to the new branch. That’s why you can use this command that will create the new branch and immediately switch you to it:

$ git checkout -b <branch-name>        

Once you’ve created a new branch and switched to it, you can start making changes in your code. However, all of these changes (including the new branch) is still only in your local machine.

To publish the new branch you created in GitHub and make it available for everyone in your team, run the following command:

$ git push -u <remote> <branch-name>        

Checkout one more branch deployment from the previous branch.

Fetch all remote branches

git fetch origin        

This fetches all the remote branches from the repository.?origin?is the remote name you're targetting. So if you had an?upstream?remote name, you can call?git fetch upstream.

List the branches available for checkout

To see the branches available for checkout, run the following:

git branch -a        

The output of this command is the list of branches available for checkout. For the remote branches, you'll find them prefixed with?remotes/origin.

Git allows you to check out a remote branch by git checkout command. It is a way for a programmer to access the work of a colleague or collaborator for review and collaboration. Each remote repository contains its own set of branches. So, to check out a remote branch, you have first to fetch the contents of the branch.

$ git fetch --all

$ git checkout?<remotebranch>        

Push different data into both branches.

It's very simple. Suppose that you have made changes to your Branch A which resides on both place locally and remotely but you want to push these changes to Branch B which doesn't exist anywhere.

Step-01: create and switch to the new branch B

git checkout -b B        

Step-02: Add changes in the new local branch?

git add . //or specific file(s)        

Step-03: Commit the changes

git commit -m "commit_message"?        

Step-04: Push changes to the new branch B. The below command will create a new branch B as well remotely

git push origin B        

Now, you can verify from bitbucket that the branch B will have one more commit than branch A. And when you will checkout the branch A these changes won't be there as these have been pushed into the branch B

Merge data from both branches to the main branch.

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the feature branch into the main branch. Note that this is a fast-forward merge.

git checkout main

git merge feature        


7. How to resolve conflict with merge?

A merge conflict is an event that?occurs when Git is unable to automatically resolve differences in code between two commits. When all the changes in the code occur on different lines or in different files, Git will successfully merge commits without your help.

Let’s assume there are two developers: Developer A and Developer B. Both of them pull the same code file from the remote repository and try to make various amendments in that file. After making the changes, Developer A pushes the file back to the remote repository from his local repository. Now, when Developer B tries to push that file after making the changes from his end, he is unable to do so, as the file has already been changed in the remote repository.

To prevent such conflicts, developers work in separate isolated branches. The Git merge command combines separate branches and resolves any conflicting edits.

There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.

  1. The easiest way to resolve a conflicted file is to open it and make any necessary changes
  2. After editing the file, we can use the?git add a?command to stage the new merged content
  3. The final step is to create a new commit with the help of the?git commit?command
  4. Git will create a new merge commit to finalize the merge

Let us now look into the Git commands that may play a significant role in resolving conflicts.

Git Commands to Resolve Conflicts?

1. git log --merge?

The git log --merge command helps to produce the list of commits that are causing the conflict

2. git diff?

The git diff command helps to identify the differences between the states repositories or files

3. git checkout?

The git checkout command is used to undo the changes made to the file, or for changing branches

4. git reset --mixed?

The git reset --mixed command is used to undo changes to the working directory and staging area

5. git merge --abort

The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began

6. git reset

The git reset command is used at the time of merge conflict to reset the conflicted files to their original state

8.What is rebase and how it is different from merge in git?

The first thing to understand about?git rebase?is that it solves the same problem as?git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways.

*Git merge--Git merge is a command that allows you to merge branches from Git.

*Git rebase--Git rebase is a command that?allows developers to?integrate changes from one branch to another.

If you're working alone or on a small team, use rebase. If you're working with a big team, use merge.?



要查看或添加评论,请登录

社区洞察

其他会员也浏览了