GIT

                                                                Global Information Tracker



It is a version control system (VCS) or source code management (SCM).













VERSION CONTROL SYSTEM (VCS)



WHAT IS GIT?

  • Git is used to track the files.
  • It will maintain multiple versions of the same file.
  • It is platform-independent.
  • It is free and open-source.
  • They can handle larger projects efficiently.
  • They save time and developers can fetch and create pull requests without switching.
  • It is 3rd generation of vcs.

VCS HISTORY











GIT is a Distributed Version Control System





GIT tracks changes you made, so you have a record of what has been done, and you can revert to specific versions. it makes collaboration easy, allowing changes by multiple people to all merged into one source



GIT STAGES:


WORKING DIRECTORY:

  • In this stage git is only aware of having files in the project.
  • It will not track these files until we commit those files.

STAGING AREA:

  • The staging area is like a rough draft space, it's where you can git add the version of a file or multiple files that you want to save in your next commit.
  • In other words, in the next version of your project.

REPOSITORY:

  • Repository in Git is considered as your project folder.
  • A repository has all the project-related data.
  • It contains the collection of the files and also history of changes made to those files.



TYPES OF REPOSITORIES:

LOCAL REPO:

The Local Repository is everything in your .git directory. Mainly what you will see in your Local Repository are all of your checkpoints or commits. It is the area that saves everything (so don’t delete it).

REMOTE REPO:

The remote repository is a Git repository that is stored on some remote computer.

CENTRAL REPO:

This will be present in our GITHUB




GIT ALTERNATIVES:

GIT LAB, SVN, BIT BUCKET, P4, STASH, HELIX



GIT work flow:





GIT INSTALLATION:

  • yum install git -y
  • git init .

COMMIT A FILE:

  • Create a file : touch filename
  • Now add that file : git add . (Dot represents current directory)
  • commit the file with message : git commit -m "commit message you want" filename
  • To see details of that file : git log
  • To untrack : git rm --cached filename
  • To show commit file : git show commit id --name-only file_name
  • To see only commit id's and msgs : git log --oneline
  • To see last 2 commits : git log -2
  • To see all commits for a single file : git log --follow --all filename

GIT ADD:

  • Git add command is a straightforward command. It adds files to the staging area.
  • We can add single or multiple files at once in the staging area.
  • Every time we add or update any file in our project, it is required to forward updates to the staging area.
  • The staging and committing are co-related to each other.





GIT COMMIT:

  • It is used to record the changes in the repository.
  • It is the next command after the git add.
  • Every commit contains the index data and the commit message.

GIT STATUS:

  • The git status command is used to display the state of the repository and staging area.
  • It allows us to see the tracked, untracked files and changes.
  • This command will not show any commit records or information.


GIT CONFIGURE:



if you want to give your username and E-mail id to those commits then

  • git config user.name "username"
  • git config user.email "userxyz@gmail.com"

NOTE: now give the git log command to see changes, it won't work because after configuring we haven't done anything.

Now create a file and commit that file and give git log you will see changes as you configure.

GIT IGNORE:

  • It will be useful when you don't want to track some specific files then we use a file called .gitignore
  • create some text files and create a directory with "jpg" files.


  • vi .gitignore
  • *.txt

Now all the txt files will be ignore


GIT BRANCHES:

  • A branch represents an independent line of development.
  • The git branch command lets you create, list, rename, and delete branches.
  • The default branch name in Git is master.




  • To see current branch : git branch
  • To add new branch : git branch branch-name
  • To switch branches : git checkout branch-name
  • To create and switch at a time : git checkout -b branch-name
  • To rename a branch : git branch -m old new
  • To clone a specific branch : git clone -b branch-name repo-URL
  • To delete a branch : git branch -d <branch>
  • To delete branch in github : git push origin --delete branch_name


The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

Now all the things you have done is on your local system.

Now we will go to GIT HUB.


EXTRA TOPICS:

GIT merge conflicts:



GIT makes merging super easy!

CONFLICTS generally araise when two people two people have changed the same lines in a file (or) if one developer deleted a file while another developer is working on the same file!

In this situation git cannot determine what is correct!

Lets understand in a simple way!

cat>file1 : hai all

add & commit

git checkout -b branch1

cat>file1 : 1234

add & commit

git checkout master

cat>>file1 : abcd

add & commit

git merge branch1 : remove it

git diff file1

vim file1


Brain-Computer Interface




Presentations are communication tools that can be

used as demonstrations, lectures, speeches, reports,

and more. It is mostly presented before an audience.


Presentations are communication tools that can be used

as demonstrations, lectures, speeches, reports, and more.

It is mostly presented before an audience.



identify merge conflicts:
see the file in master branch then you will see both the data in a single file including branch names

resolve:

open file in VIM EDITOR and delete all the conflict dividers and save it!

add git to that file and commit it with the command (git commit -m "merged and resolved the conflict issue in abc.txt")


GIT REBASE:

if you have 5 commits in master branch and only 1 commit in devops branch, to get all the commits from master branch to devops branch

we can use rebase in git. (command: git rebase branch_name)

GIT cherry-pick:

if you have 5 commits in master branch and only 1 commit in devops branch, to get specific commit from master branch to devops branch

we can use cherry pick in git. (git cherry-pick commit_id).


GIT stash:


Using the git stash command, developers can temporarily save changes made in the working directory. It allows them to quickly switch contexts when they are not quite ready to commit changes. And it allows them to more easily switch between branches.

Generally, the stash's meaning is "store something safely in a hidden place."

  • git stash
  • ls
  • git stash apply
  • ls
  • git stash list
  • git stash pop
  • git stash clear

SOME EXTRA COMMANDS:


git show <commit> --stat : you’ll see the commit summary along with the files that changed and details on how they changed.

git commit --amend -m “New commit message” : to edit the commit message

git commit --amend --no-edit : used to add some files in previous commit. (--no-edit means that the commit message does not change.)

git update-ref -d HEAD : used to delete 1st commit in the git

git reset commit: used to delete all the commits (upto the commit id)

git commit --amend --author "Author Name <Author Email>" : used to change the author of latest commit


merge vs rebase:

When there are changes on the main branch that you want to incorporate into your branch, you can either merge the changes in or rebase your branch from a different point.

merge takes the changes from one branch and merges them into another branch in one merge commit.

rebase adjusts the point at which a branch actually branched off (i.e. moves the branch to a new starting point from the base branch).

Generally, you’ll use rebase when there are changes are made in main/master branch that you want to include in your branch. You’ll use merge when there are changes in a branch that you want to put back into main.

to merge: git merge branch_name

to rebase: git rebase branch_name


GIT-HUB





GitHub is a web-based platform used for version control.

It simplifies the process of working with other people and makes it easy to collaborate on projects.

Team members can work on files and easily merge their changes in with the master branch of the project.



push your code to git hub:



GIT CLONING:



It means having same files in another folder.

To clone a git repo we need to have a repository and also check our present working directory.



  • git clone repo_url

now we just cloned the files in repo-A to repo-B.

But before cloning we need to add and commit our files.

GIT merge:

  • If you want to merge branch-1 with branch-2 switch to branch-1 first and give command

git merge branch-2

  • now that command had merged the content of branch-1 to branch-2.
  • Whatever the content in branch-1 will be seen in branch-2 now.


git merge branchname


GIT fork:

A fork is a rough copy of a repository. Forking a repository allows you to freely test and debug with changes without affecting the original project


ADVANTAGES:


  • Speed
  • Simplicity
  • Fully Distributed
  • Excellent support for parallel development, support for hundreds of parallel branches.
  • Integrity

DISADVANTAGES:

  • Windows support issue.
  • Entire download of the project history may be impractical and consume more disk space if the project has long history.

COMPARISION:



GIT tags:

By using this tags, we can tag the commits for further reference.


To see current tags                                     :             git tag
To add new branch                                     :             git tag tag-name
To show tag details                                    :              git show tag name
To push a specific tag.                               :             git push origin tag2
To delete a branch                                     :             git tag -d <branch>



GIT pull:

push some from local to central and add some commits in central. you can see those central commits from local

by using git fetch.

git fetch is used to get the updates from central repo.

git fetch : used to get the changes from central to local

git status : to see the differences of the commits

git log origin/branch_name : to see the logs of remote repo

to get those changes from central to local we use

git pull origin branch_name