Contents

Git & GitHub

Click to create an account https://github.com/

What is Git?

  • Git is a modern and widely used distributed version control system in the world. It is developed to manage projects with high speed and efficiency. The version control system allows us to monitor and work together with our team members at the same workspace.
  • Git is foundation of many services like GitHub and GitLab, but we can use Git without using any other Git services. Git can be used privately and publicly.

Benefits Of Git

  • A version control application allows us to keep track of all the changes that we make in the files of our project. Every time we make changes in files of an existing project, we can push those changes to a repository. Other developers are allowed to pull your changes from the repository and continue to work with the updates that you added to the project files.

Why Git ?

  • Git Intergrity : Git is developed to ensure the security and integrity of content being version controlled. It uses checksum during transit or tampering with the file system to confirm that information is not lost. Internally it creates a checksum value from the contents of the file and then verifies it when transmitting or storing data.

  • Trendy Version Control System : Git is the most widely used version control system. It has maximum projects among all the version control systems. Due to its amazing workflow and features, it is a preferred choice of developers.

  • Everything is Local : Almost All operations of Git can be performed locally; this is a significant reason for the use of Git. We will not have to ensure internet connectivity.

  • Collaborate to Public Projects : There are many public projects available on the GitHub. We can collaborate on those projects and show our creativity to the world. Many developers are collaborating on public projects. The collaboration allows us to stand with experienced developers and learn a lot from them; thus, it takes our programming skills to the next level.

Git DataBase.

  • The primitives of Git are not a source-code management system inherently. Git has integrated the full set of aspects expected of a classic SCM, with aspects mostly being made as required, then refined and increased over time from this starting design approach.

  • Git includes two different data structures. The first data structure is a mutable index (also known as cache or stage) that caches details about the active directory and the upcoming revision to be devoted. The second data structure is an append-only immutable object database.

The immutable database includes five object types:

  • Blob
  • Tree
  • Commit
  • Tag
  • Packfile

What is GitHub ?s GitHub?

  • GitHub is a Git repository hosting service. GitHub also facilitates with many of its features, such as access control and collaboration. It provides a Web-based graphical interface.It hosts source code of your project in the form of different programming languages and keeps track of the various changes made by programmers.

Git Commands

  • Git init : The git init command is used to create a new blank repository. It is used to make an existing project as a Git project. Several Git commands run inside the repository, but init command can be run outside of the repository.The git init command creates a .git subdirectory in the current working directory. This newly created subdirectory contains all of the necessary metadata. These metadata can be categorized into objects, refs, and temp files. It also initializes a HEAD pointer for the master branch of the repository.

TO create a blank repository:

        $ git init      

The command will create an empty .git repository under a directory named NewDirectory:

        $ git init NewDirectory

To create a file, run the cat or touch command as follows:

        $ touch <file name>

To add files to the repository, run the git add command as follows: It adds file to the staging area

        $ git add <file name>  or  $ git add . (To add all filesz)

To undo an add operation, run the below command:

        $ git reset <filename>

We can list all the untracked files by git status command.

        $ git status 
  • 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. Every commit forms a parent-child relationship. When we add a file in Git, it will take place in the staging area. A commit command is used to fetch updates from the staging area to the repository.

To record the changes in the repository :(-m lets you write commit messagez)

        $ git commit -m"any message"

The amend option lets us to edit the last commit. If accidentally, we have committed a wrong commit message, then this feature is a savage option for us. It will run as follows:

        $ git commit -ammend
  • Git clone :The git clone is a command-line utility which is used to make a local copy of a remote repository. It accesses the repository through a remote URL. Usually, the original repository is located on a remote server, often from a Git service like GitHub, Bitbucket, or GitLab. The remote repository URL is referred to the origin.

          $ git clone <repository url>
    
  • Git stash :Sometimes you want to switch the branches, but you are working on an incomplete part of your current project. You don’t want to make a commit of half-done work. Git stashing allows you to do so. The git stash command enables you to switch branches without committing the current branch.

Store something safely in a hidden place

        $ git stash 
        $ git stash list (Lists the stored stash)
        $ git stash drop (The git stash drop command is used to delete a stash from the queue)
        $ git stash clear (The git stash clear command allows deleting all the available stashes at once)

Git Ignore :In Git, the term “ignore” is used to specify intentionally untracked files that Git should ignore. It doesn’t affect the Files that already tracked by Git.Sometimes you don’t want to send the files to Git service like GitHub. We can specify files in Git to ignore.

  • Git Remote : In Git, the term remote is concerned with the remote repository. It is a shared repository that all team members use to exchange their changes. A remote repository is stored on a code hosting service like an internal server, GitHub, Subversion, and more. In the case of a local repository, a remote typically does not provide a file tree of the project’s current state; as an alternative, it only consists of the .git versioning data.

          $ git remote 
          $ git remote -v (Shows the url)
          $ git remote add origin <remote url>  (adds the remote url)
          $ git remote rm <destination>   (remove a remote connection from a repository)
          $ git remote rename <old name><new name>  (renames the ild remote)
    
  • Upstream & Downstream :The term upstream and downstream refers to the repository. Generally, upstream is from where you clone the repository, and downstream is any project that integrates your work with other works. However, these terms are not restricted to Git repositories.

    • There are two different contexts in Git for upstream/downstream, which are remotes and time/history. In the reference of remote upstream/downstream, the downstream repo will be pulled from the upstream repository. Data will flow downstream naturally.

    • In the reference of time/history, it can be unclear, because upstream in time means downstream in history, and vice-versa. So it is better if we use the parent/child terms in place of upstream/downstream in case of time/history.

How To Check The Current Directory is Git or Not ?

  • Use this command in your Terminal to determine if the directory you are currently in is a Git repository or not:

         $ git rev-parse --is-inside-work-tree
    
    • The shell will print TRUE if your in git repository working tree ,else it will print FALSE if your inside the .git but not inside the working directory.
  • TO know the toplevel of the git :

         $ git rev-parse --show-toplevel