Working with Git and GitHub

Working with Git and GitHub

By - GJS

Why Version Control System?

Version control systems, such as Git, are used in software development projects to manage changes to code over time. They provide a way to track changes, collaborate with other developers, and revert changes if necessary. Version control systems also enable developers to maintain multiple versions of the codebase and provide a complete history of the project's development. Using a version control system can help improve the quality and reliability of software development projects.

Types of VCS :

There are two main types of version control systems :

  • Centralized Version Control Systems (CVCS) - In a CVCS, all code changes are stored on a central server, and developers check out a copy of the codebase from the server to make changes. Examples of CVCSs include Subversion and Perforce.

  • Distributed Version Control Systems (DVCS) - In a DVCS, each developer has a complete copy of the entire codebase, including its history. Developers can work independently on their copy of the codebase, and merge their changes with other developers' copies when they are ready. Examples of DVCSs include Git and Mercurial.

    What is Git?

Git is a popular version control system used to track changes in software development projects. It is a DVCS.

What is GitHub?

GitHub is a web-based platform for hosting and collaborating on software development projects using the Git version control system. It includes features such as repository hosting, collaboration tools, and community features, and integrates with a wide range of other tools commonly used in software development.

The Three-State Architecture:

  1. Working Directory: Where you modify files.

  2. Staging Area: Where you prepare a snapshot of the changes you want to commit.

  3. Local Repository: Where Git permanently stores your committed changes locally.

  4. Remote Repository: Where Git permanently stores your committed changes in a remote location, such as GitHub.

Using git on AWS EC2:

  • Create an instance and connect to it:

  • Create a directory :
    -> mkdir devops-dir

  • Create a git repository:
    -> git init

  • Create a hello.txt file
    -> touch hello.txt

  • Configure git :

    1. git config --global user.name “YOUR_USER_NAME”

    2. git config --global user.email “YOUR_Email”

  • Check git status:
    -> git status

  • Now add the "hello.txt" from working directory to staging area:
    -> git ls
    (git ls will list the files - hello.txt )
    -> git add .
    -> git status
    (now you can see the colour of "hello.txt" changed)

  • To unstage the file "hello.txt"
    ->git reset .

  • Now we commit the changes:
    "before commiting make sure you have staged the file"

       git commit -"feature1 added"
    
      git log --oneline
    

  • Now create access token in GitHub:
    Go to the Settings->Developer settings->Personal access tokens->Tokens(classic)->Generate new token (classic)

  • Now give the Description and select the scope - "repo" :

  • Now click generate :

  • Now copy the token :

  • Now copy your repository address and write the command as shown below:

  •      git remote add origin https://github.com/GJS2162/django-todo-cicd.git
    
      git remote -v
    

  • Now use the access token and write the following command :

  •     git remote set-url origin https://<Your_Access_Token>@github.com/<USERNAME>/<Repo_Name>.git
    

  • Now add an upstream :

  •     git remote add upstream https://github.com/LondheShubham153/django-todo-cicd.git
    

  • Now pull the repo contents :

      1) git fetch origin
      2) git branch -r
      3) git merge origin/develop
      4) ls
    

  • Now push the changes :

 git push origin master

Another method which involves cloning the repository :

1) clone https://github.com/GJS2162/node-todo-cicd.git

2) ls

3) cd node-todo-cicd/

4) git remote -v

5) git remote set-url origin 
https://<Acces_Token_You_Created_Earlier>@github.com/GJS2162/node-todo-cicd.git

6) git remote -v

7) touch today.txt
8) git add .
9) git commit -m "added feature 1- today.txt"
10) git push origin master

(I added a file named "my-new-file" on GitHub)
11) git pull

git revert :

1. git revert is a Git command used to undo changes made in a previous commit by creating a new commit.

  1. It undoes the changes made in the specified commit, effectively reversing the commit.

  2. The original commit still exists in the repository, but its changes are overwritten by the changes in the new revert commit.

  3. This is useful when you want to undo changes that have already been committed without deleting the commit itself, which can cause issues with the Git history.

     git revert <Commit_Id>
    
     or
    
     git revert -m "This commit undos the work done by Commit-1" <Commit-1_Id>
    

git reset :

The git reset command is a powerful command in Git that allows you to move the HEAD and branch pointer to a specific commit, effectively "resetting" the state of your working directory to a previous state. There are three different modes of git reset: --soft, --mixed, and --hard.

  • --soft: This mode resets the HEAD to a specific commit, leaving the changes in the staging area intact.

  • --mixed: This mode resets the HEAD to a specific commit, and also resets the changes in the staging area.

  • --hard: This mode resets the HEAD to a specific commit, and also resets the changes in the staging area and working directory, effectively discarding all changes made after the specified commit.

git merge :

git merge is a Git command used to integrate changes from one branch into another.

  1. It combines the changes made in one branch into another branch, creating a new commit that incorporates the changes from both branches.

  2. If there are conflicts between the changes in the two branches, Git will prompt you to resolve them manually before the merge can be completed.

  3. This is useful when you have made changes to a feature branch and want to incorporate those changes into the main branch of your repository, or when you want to merge changes from a forked repository back into the original repository.

Let's say you have a Git repository with two branches: master and feature-branch. You've been working on some changes in feature-branch and now you want to merge those changes into the master branch. Here's how you would do that:

  1. First, make sure you are on the master branch. You can do this by running git checkout master.

  2. Next, run the command git merge feature-branch. This will attempt to automatically merge the changes from feature-branch into master.

  3. If there are no conflicts between the changes in the two branches, Git will automatically merge the changes and create a new commit with the merged changes.

  4. If there are conflicts, Git will prompt you to manually resolve them by editing the affected files and then committing the changes. Once you have resolved the conflicts, run the command git commit to create the new merge commit.

  5. Finally, run the command git push to push the merged changes to the remote repository.

    git rebase :

    Git rebase is a command used to modify the commit history of a branch by applying a series of commits from another branch on top of the current branch, resulting in a new, linear commit history.

    The rebase process involves identifying a common ancestor commit between the current and target branches, saving changes made on the current branch into temporary files, applying the changes from the target branch, and then reapplying the saved changes on top of the new changes.

    Rebasing can be useful for keeping a clean and linear commit history, resolving conflicts between branches, and ensuring that changes are based on the latest version of a branch.

    However, it is important to use rebase with caution, as it can modify the commit history and cause conflicts if not used correctly.

    Lets take take the above diagram :
    We have two branches first is master and the other is feature1 (Let's say)

    now we will make commit in the following order :
    - commit no. 1 on master i.e. m1
    - commit no. 2 on master i.e m2

    - commit no. 3 on feature1 i.e. f1

    - commit no. 4 on feature1 i.e. f2

    - commit no. 5 on master i.e. m3

    The master branch's file "hello.txt" looks like :

    The feature branch's file "hello.txt" looks like :

Now follow the following commands :

On branch main, type : git rebase feature1

on branch main, type :git status

1) vi hello.txt
2) Perform the required changes - Resolve the conflict (if any)
3) git add hello.txt
4) git status
5) git rebase --contiue
6) To Check the file hello.txt on branch main : cat hello.txt

git merge example:

( this example is performed after the five commits shown previously )

If instead of rebase you wanted to do merge : 
1) git switch feature1
2) git merge master
3) git status
4) vi hello.txt

1) git add hello.txt
2) git commit

Write the commit message if any and then press : control and X

To see the changes you did : cat hello.txt

Thank you:

If you reached here then here's a message for you - "You are going to achieve great things in life"