Git & Github...
Published: 2022-04-02 02:54:17

## What is Git?

Free and open source version control system.

## What is version control?

The management of changes to documents, computer programs, large websites, and other collection of the information.

## Some of the useful commands -

![Some of the useful commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc3oad3ov1hcn46g7444.png)
  
## Some of the important terms to remember -

![Some of the important terms to remember](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvsdysi2xd7q3884i5rz.png)
 

## Github Introduction.

### _Creating a new repository in Github._

- We can create our new repo in github using the buttons highlighted in red and yellow.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhqk17irmx7hpq5iksbv.png)

- Once you click any one of the above mentioned buttons it will open a new window as shown below.

![new repo form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx60008m5333pfodppih.png)

1. Name of repository.
2. Its description.
3. You can make your repo Public ( Anyone can access it) or Private (Only you can access).
4. Create repo button. 
 

- Repo will look like this.
- Click on highlighted text to create the new file inside a repo.

![repo look](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9t0qvhzpfvv89ra8mzd.png)

- New file window will look like this.
- Highlighted portion shows file name.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwrxameiwx163r6ewmyx.png)

- After writing the code you will be asked to enter other details like **1-** Commit Message (Is given during every commit ) **2-** Description message.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zy47p3l310m48qapz9v2.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux24kdzljdxnfoyiz8kc.png)
 

- Once committed the file or any change in file then it will look like this.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4z5cfe9a99x12z0fyps.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vwsipir91mg66108jem.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4p9t6ov5cwki5sm2bgm.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1oa8d4dydg6bshjyvwe.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulxe6s5inuyupqfsd54z.png)

## Git - Cloning the repository-

- Visit any existing repository in github and follow the steps to clone the repo.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnp93nr2vt574khpnr95.png)
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1szjlxsn8ns8rtf906th.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oue8v5yqhfg4ns7ba81o.png)

- Since we cloned the repo in the demorepo so move in that repo.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lvtohbfm7uyxd359hge.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itfeew7mh4ovo7557t6l.png)

- CLI will look some thing like this {Master will be mentioned in this or any other style which shows you are in master branch}. 

## Listing the hidden files.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ag5m1vio255c07ys178.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqjq5ssk6pkwvhrckp7f.png)

- Both of this command is used to list all the files and folders (including hidden). 
 

## Checking status of files.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhl69ex908iqzak0moj7.png)
- To check the status of file - Red means un tracked Green means staged.
- Gives the info about all the files ever created , deleted, untracked or committed.

## Stages of file.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kerlnqwluvx2ozt3lyb6.png)

#### Un Tracked files –
- Initial stage of file.
- This files can’t be tracked by the git.

#### Staged –
-  Those files are put in this stage which are going to be committed.
 
#### Unmodified –
- After staging area once the files are committed they come to this section.

- From here they can go in two direction:
1. If the file is removed then it goes back to untracked.
2. If some changes are don then it goes too modified area.

#### Modified:
- From here files are send to the staged area.


## Git commit-

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sedhfr2xqcck8f1r4ji.png)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nunxjtflghy71imgw8db.png)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkyicylw9vm8ju860k21.png)
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b68xcpkh5dz5sd196lxw.png)
    

## SSH Key -

- First generate SSH key to your own machine using the command shown below.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jcyhl8yuna32pf4sns8.png)
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr9jbi24j49xzw1b2vx2.png)

- You can rename your file / key name.
- And can put a password on that.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4p4e9llz3xc8vgrvn5r.png)
 - This is how key looks like.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ksxir2988skykxpro8w.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12xb49v6qd4v6oqkfov7.png)

- Go to profile and then settings.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8848uxubx6bvw1fjfer.png)

- Go to SSH and GPG Keys.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51yd34xe2ptwgy8rp3uz.png)

- Paste the key in box like mine pasted.
- When you will click on new SSH key this dialog box will open.      

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue7mikys99f33a8x98ad.png)

## Git Push -

- If you remember in git commit section we committed index.html.
- Now we will push it to git hub.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xoqbegz7dxef2o75a2aq.png)

- In above figure-:
          Location of our git repo set as default to origin. 
- This was the command to push index.html to git hub.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o8jywct4aeteul5w4mw.png)
 
- If we run this command then we never need to give origin and master(Means branch name Master in this case only it will change accordingly) we can only use git push to push it to git hub.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xc93j7f720jpdp7qa82u.png)

-  Index .html to git hub.

## Git Init -

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtk2o99igg6k3l5kf1rn.png)

- Demo-repo2 is not a git repo.
- In order to make it a git repo we need to enter that folder. By following command.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31813t53sxd8rnfuiu0w.png)

- And run this command.
- Now it is a git repo and u are in master branch.
- In order to push it to git hub you need to make an empty repo on git hub.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88hdtrp81fh5eetxnaox.png)

- This dialog box will open.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/620kfbnkl3tgusc52sbt.png)

- After creating the new repo copy the SSH key.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyg75i9jhzvgybq5kers.png)

- And run this command.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xg3cf6zic2qu9zutc81.png)

- This command shows that this local repo is connected to which remote repo.

## Work Flow of Git & Git hub -

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lno42gcgbhjezq6mpqw4.png)
 

## Git Branching -

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syo2c5qp8qvskr3ryjm5.png)

- Three types-:

 
- **Master Branch-**Master branch is the main branch having main code of the project which is the best in present time.

- **Feature Branch-**This branches are made to build and test some new features, Once we are confident with that feature we merge it to the main branch.

- **Hot Fix Branch-**This branch is made to fix some bugs in the main program.
   
- **_Command to check the number of branches of the git repository -_**

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmgjfldxgk2fnussyal5.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag85w5j85geafb8pvej3.png)

- It has only master branch now.
- Star denotes that we are currently on this branch.
**_Need to hit "q" in order to come out of this window._** 

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bkqg3tlt2htx8ve60k9.png)

- Command to create a new branch.
- Feature denotes the name we can use according to our requirements.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/730af1amxre1zzfqajxe.png)
 
- This command is used to differentiate between master and the branch name specified.
- You need to run it from master branch and u can go there by checkout command.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thplkgmsboq2k5y3zkpf.png)
 
- The UI of the same has been discussed above.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11b9c9238ddthexgtduh.png)
 
**Command to merge any branch to the main branch of the repo.**

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sh22usd71jgqfsefthia.png)
 

## Pull Request-

**_In this fig the branch is just pushed to git hub not merged with the Main branch._**

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68otvj1t18gsl6oxmlw6.png)
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69x1z07qyg9wtsiz2mkx.png)
 
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1s7aivglh3e2wj7uq1g.png)

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7papq79rzkd3m6mf1sa3.png)
  
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b37hvf19bfa03cbjqyxv.png)
 
- Just below merge pull request button is given to merge after fixing the bug.

**_This changes are done in git hub we need to bring this changes to our local machine we can do it by this command._**

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqlesrzoa6n2dnkv7vus.png)
 
- Since we merged the features to the master branch so we need to delete that feature branch.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciix0418odk8bqi1t32o.png)
 
- We can do it by this command.

## Merge Conflicts-

- A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0c384rp5xt2ec295tpe.png)

- When we modified the file then instead of moving it to stage area we can directly commit it using this command. {only for modified files which were committed previously}.

- After solving the merge conflicts you need to commit the file again it doesn’t matter that u changed something in file or not .
 

## Un Doing in git-

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edw094hxus84v1uly3n9.png)

- This command reset the previously done staging on file name specified.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adbcewnqt1stzc96649o.png) 

- This will also do the same thing.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsfplrba934vy627il0m.png)
 
- This is to reset last commit.
- Here HEAD is a pointer which tells that we need to reset 1 commit or go 1 commit back.
- So in order to un do lots of commits we need to use the commit code.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpgbov60pepxnpd8br0n.png)

- To get the log of every commit made on that branch.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z3nvlwl3y4tk5lbrmh7z.png)

- This will show up all the previous commits copy the code of the commit.
- In order to scroll down use space bar and q to quit.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx65xtdypeb7yujut2p0.png)

- This will undo all the commits done after this specified commit but this commit remain untouched.
- This commands unstage the commit.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t7ef7ug7embal762rjv.png)
    
- This will not unstage the commit it will remove them completely.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crrgewfzbbbhjg8wpjfi.png)
 
- This command can be used to un stage the file.

## Forking -

- Forking is a git clone operation executed on a server copy of a projects repo. ... You create a new feature branch in your local repo. Work is done to complete the new feature and git commit is executed to save the changes. You then push the new feature branch to your remote forked repo.

- This is done to bring the whole repo to your own account not the branch.

- After making the desired changes we can create the pull request to accept the changes you made in the project.