2. DevOps: Git

2. DevOps: Git

Git is by far the most used and reliable tool for source code and version control.?Git integrates with services like GitHub, GitLab, Bitbucket, and others to facilitate collaboration, code review, and project management. ?

So, what do we do with Git?

Simply put, we use commands like git pull, then make changes, git add, git commit, and git push to get our work done

But wait. How does this works? We're pretty sure that this works, but how? What does it mean to pull and push? What actually happens when we commit? And why can't I merge my changes sometimes to master branch without causing some ugly conflicts?

Let's go one step deeper !


?

To understand this properly, let's first look at Git Components:? ?

  • Remote repository is a version of your Git repository hosted on a server or online service, like GitHub or GitLab. It acts as a shared space where team members can collaborate, push updates, and fetch changes from each other. It helps keep a centralized and synchronized copy of your project accessible over the internet.?
  • Working directory is the folder on your computer where you modify and create files for your project. It reflects the current state of your project’s files, showing the latest changes that have not yet been committed to your local repository. This is where you make edits, add new files, and test your code before staging and committing your changes.?
  • Staging area (or index) is a space in Git where you prepare changes before committing them to the local repository. It acts as a buffer where you can review and select which changes to include in the next commit?
  • Local repository is the version of your Git repository stored on your computer. It contains the full history of your project, including all commits, branches, and tags. It allows you to track changes, manage versions, and perform local operations before syncing with remote repositories.?



Now that we know about these components, let's look at how Git Commands?works.?

1.git clone:? This is typically the first step you take when you want to work on an existing project that is hosted in a remote repository. Here you will give an URL which will use HTTP(S) or SSH to establish a connection between your machine and remote repository. ? ?It makes a complete copy of a project from remote repository to your computer.?

Remote Repository? ------git clone <URL>--------> New Working Directory ?

2.git pull:? It?updates your local project by downloading and integrating the latest changes from a remote repository. It fetches new data from the remote and automatically merges it into your current branch. This keeps your local copy in sync with the remote version of the project.? Actually it is combination of two commands :

git pull = git fetch + git merge?

Remote Repository? ------git pull-------> Updated Working directory

3.git add <file>: When you make any changes in your file, you need to add them to the staging area using this command. If you have too many files and you want all?to get to staging area then use git add .?

Working Directory with changes ------git add <file> ---> Staging Area with updated files

4.git commit: This command saves the changes you've prepared in your staging area as a snapshot in your project's history. It adds a message describing what you changed, making it easy to track updates.?

Staging Area ------git commit -m "message"------> Local Repository?

5.git push: This command uploads your local commits from your local repository to a remote repository. It updates the remote with your latest changes, making them available to others and synchronizing your work with the shared project.?

Local Repository ------git push------> Remote Repository?

6.git revert: This command creates a new commit that undo the changes made by a specific previous commit without altering the project’s commit history.

Local Repository ------git revert <commit-hash>------> Updated Local Repository ?

7.git reset --soft: This command moves the current branch pointer to a specified commit but leaves changes in the staging area and working directory intact. It means, your changes are still there in working directory or staging area but they are not committed.?

Local Repository ------git reset --soft <commit-hash>------> Updated Branch Pointer (with changes still staged)?

8.git reset --hard: This command moves the current branch pointer to a specified commit and discards all changes in the staging area and working directory. It is useful for completely removing commits and their associated changes from your local repository.?

Local Repository ------git reset --hard <commit-hash>------> Updated Branch Pointer (with discarded changes in Staging Area and Working Directory)?

9.git reset <file>: This command removes the specified file(s) from the staging area, effectively un-staging them while keeping the changes in your working directory. It is useful for undoing the staging of files that you don’t want to include in the next commit.?

Staging area-----git reset <file>------> Working Directory (file is un-staged but changes remain)? ?

10.git checkout:??This command is used to switch branches or create new branches or restore files in your working directory to a last committed state. ?

Working directory (some changes) ---- git checkout --<file> --> Working directory (to last committed state)

Working Directory (branch1) ---git checkout <branch2 name> --> Working Directory (branch2)??

Working Directory (old branch) ----git checkout? -b <new branch name> --> Working Directory (new branch)?

11.git merge <branch>:? As name suggest, when you want to merge your changes in master branch git merge will be used. First, go into your main branch and then use this command to incorporate commits from feature branch in main branch.? ?

master branch -----git merge <your branch> ---> master branch with all your commits.? ?

12.git stash:??So let's say , you made some changes and then you need to store it somewhere (at top of a stash) temporarily but not commit it. And you want a clean working directory and staging area. At that time git stash can be used.

Working Directory/Staging area (with some changes) --git stash--> Working Directory/Staging Area (without those changes) + Stash (with those changes)??

13.git stash pop: Now you want to get back those changes and wants to continue your work. You will be using git stash pop.? ?

Working directory/ Staging area + Stash(with some changes) ----git stash pop---> Working directory/Staging area (with changes from top of stash)?

14.git rebase <branch>:?Suppose you have a master branch with commits A and B, and you create a new branch from it. Meanwhile, commit C is added to master. If you want to incorporate commit C into your branch before merging it back to master, you would use git rebase.

master branch with commits A, B, C + your branch with commits A, B, D? ---git rebase master--->? your branch with commits A, B, C,D ----git merge? ---> master branch with commits A,B, C,D? ?

15.git cherry-pick <commit>: This is? useful when you have one commit committed somewhere at some other branch but you want same changes in your current branch as well. Now instead of repeating all those changes again , one can use git cherry-pick? ?

Working directory ---git cherry-pick <commit-hash>---> Working Directory with changes from given commit



Okay, we got it. We can now work with git knowing what is happening. But if you wonder how does git manages all this things for us? What are the bricks and stone for Git to function?

Now for answer that, let's?go one more step deeper, understanding how files changes, commits are tracked.

This will lead us to Git Objects.?Git stores data as objects (blobs, trees, commits, and tags) in its database. These objects are immutable and identified by unique SHA-1 hashes.? Let's understand with simple example.?? ?

Let's say you are working on a project in working directory and you made a change in a file.? ? Then you used git add <filename>? to put that file in staging area.

Git will create a Blob (binary large object)?which is basically a type of storage, storing the updated version of files content. Different blob for different versions of same file?

Git will create a Tree object which will have reference (address) to that one blob above mentioned. ? Let's say you made changes in 5 files from main folder and 3 files from one of the subfolder. Git will create a tree object , of which 5 branches will have reference to those 5 blobs and one branch will have reference to another tree object which will have 3 branches referencing to 3 blobs.?

Then you committed those changes and local repository is updated. Git will create an Object called Commit which will have??

-Tree reference which are created as mentioned above?

-Parent commit reference if any??

-Author??

-Committer?

-Date?

-Commit Message

Now let's say you have one very special commit, which can be a final commit before release or a very important commit and you want to bookmark it. There you will be needing Git Tag?.

Tag as an?object are simple pointers to specific commit. This is how you can use it.? ?

git tag -a <tag-name> <commit-hash> -m "Tag message"?



Now that we have understood and seen how git command actually getting performed.?? You? must be pretty sure that someone might have set some guidelines for avoiding different practices within a given project. A Git workflow is a set of guidelines and best practices for using Git to manage your codebase. These guidelines are different for different kinds of branches like Feature branch for making changes,?Develop branch used for integrating all changes, Release branch for releasing all tested changes, Main branch as a master branch? or most stable branch. Each of these branch can have some more specific flow guidelines.? ?



Finally, let's discuss git hooks. Git Hooks are just some scripts written to get triggered when certain action is performed.?? For example one might want to enforce to put a commit message while committing. Then?the user?can go to .git/hooks folder inside your working directory. Create a file named as 'commit-msg'(file names are reserved by git. You cannot use random name). Here is an example of script.? ?

#!/bin/bash
#Arguement is taken by understanding the type of hook specified in file name.

commit_msg_file=$1   # Read the commit message file path from the first argument
commit_msg=$(cat "$commit_msg_file")  # Read the commit message

# Check if the commit message is empty
if [ -z "$commit_msg" ]; then
    echo "Error: Commit message cannot be empty."
    exit 1
fi
exit 0        


We’ve now covered the essentials of Git. I hope you found this information both interesting and valuable. Feel free to share any comments, suggestions, or corrections.

If you are new to DevOps or want to understand it better, you can refer to my previous article -

Thank you for reading!



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

Gopal Katyarmal的更多文章

  • 3. DevOps : Jenkins

    3. DevOps : Jenkins

    Hey Guys, So far, we have looked into Basics of DevOps and Git. Today, let's take a step ahead and start learning about…

  • 1. DevOps : Basic Concept

    1. DevOps : Basic Concept

    DevOps is a combination of development and operations practices aimed at improving collaboration, efficiency, and…

    2 条评论

社区洞察

其他会员也浏览了