Moment For Technology

Git tutorial - Git commands and operations

Posted on Aug. 7, 2023, 2:29 p.m. by Prerak Thakkar
Category: The front end Tag: devops

【 Note 】 This article is translated from: Learning Git is as easy as using the tool. The purpose of this Git tutorial is to get this conundrum out of your head. I'm sure you'll understand all the concepts through this Git tutorial. I hope you've learned the basic concepts and terminology of Git, and learned all about version control in my first blog post in the Git tutorial series. In this tutorial, you will learn:

  • The Git command
  • Git operation
  • There are also some tips and tricks for managing projects effectively with Git.

Before you start using commands and actions, let's understand Git's main purpose. Git's purpose is to manage a project or set of files that change over time. Git stores this information in data structures called Git repositories. Repositories are at the heart of Git.

How do I view my GIT repository?

Specifically, a Git repository is the directory where all project files and associated metadata are stored. Git records the current state of a project by creating a tree chart from an index. It usually exists in the form of directed acyclic graph (DAG). Now that you understand the goal of dismantling Git, let's move on to actions and commands.

How to learn Git commands?

How Git works

  • Create a "repository" (project) using a Git hosting tool such as Bitbucket
  • Copy (or clone) the repository to the local computer
  • Add the file to the local repository and "commit" (save) the changes
  • Push changes to the main branch
  • Use git hosting tools to make changes to the file and commit
  • Pull changes to the local computer
  • Create a "branch" (version), make the changes, and commit the changes
  • Open a Pull request.
  • "Merge" your branches into the main branch

What is the difference between Git Shell and Git Bash?

Git Bash and Git Shell are two different command line programs that let you interact with the underlying Gi T programs. Bash is a Linux-based command line, while Shell is a native Windows command line.

Some operations and commands

Some basic operations in Git are:

  1. Initialize
  2. Add (Add)
  3. Commit
  4. Pull
  5. Push

Some advanced Git operations are:

  1. Shoot (branch)
  2. Merging.
  3. Rebasing

Let me start with a brief overview of how these actions work with Git repositories. Take a look at the Git architecture below:If you already understand the diagram above, that's great, but if you don't, don't worry, I'll explain each one in this tutorial. Let's start with the basics. You need to install Git on your system first. If you need help with installation,Please click here.

What is Git Bash used for?

This Git Bash tutorial focuses on the commands and operations you can use on Git Bash.

How to browse Git Bash?

Once you have Git installed on your Windows system, simply open the folder/directory where you want to store all your project files. Right click and select"Git Bash Here".This opens the Git Bash terminal, where you can enter commands to perform various Git operations. Now, the next task is to initialize the repository.


To do this, we use commandsgit init. Please refer to the screenshot below.   git initCreate an empty Git repository or re-initialize an existing repository. It basically creates one with subdirectories and template files.gitDirectory. Run in an existing repositorygit initDoes not overwrite content that already exists. It selects the newly added template. Now that my repository is initialized, let me create some files in the directory/repository. For example, I created two text files, edureka1.txt and edureka2.txt. Let's usegit statusCommand to see if these files are in my index. The index holds a snapshot of the contents of the working tree/directory for the next change to be made in the local repository.

Git status

  git statusThe command lists all the modified files that are ready to be added to the local repository. Let's type in the command and see what happens:This indicates that I have two files that have not been added to the index. This means that changes to these files cannot be committed unless they are explicitly added to the index.


This command updates the index with the current content found in the working tree, and then prepares the content for the next commit in the temporary area. So, after changing the working tree, and runningcommitBefore the command, you must use the add command to add all new or modified files to the index. To do this, use the following command:git add directorygit add fileLet me show yougit addSo that you can understand it better. I also created two files, edureka3.txt and edureka4.txt. Let's use the commandgit add -AAdd files. This command adds files from all directories to the index, but has not yet been updated in the index.Now that the new files have been added to the index, you can commit them.


It refers to recording a snapshot of the repository at a given time. The submitted snapshot will never change unless the welder completes it. Let me use the following diagram to illustrate how COMMIT works:Here, C1 is the initial commit, the snapshot of the first change, from which another snapshot named C2 is created. Notice that the primary node points to the latest commit. Now, when I commit again, another snapshot C3 will be created, and now the primary snapshot points to C3 instead of C2. Git's goal is to make commits as light as possible. So it doesn't blindly copy the entire directory every time it commits; It includes commits as a set of changes, or "deltas" from one version's repository to another. In simple terms, it copies only the changes made in the repository. You can submit using the following command:git commitThis will commit the temporary snapshot, and the text editor will launch, prompting you to enter a submit message. Or you can use:Git commit -m " message "Let's try it.As you can see above, the Git commit command has committed the changes in four files in the local repository. Now, if you want to commit a snapshot of all changes in the working directory at once, you can use the following command:git commit -aI created two more text files in the working directory viz. Edureka5.txt and edureka6.txt, but they have not yet been added to the index. I'm adding edureka 5.text using the command:git add edureka5.txtI have explicitly added edureka5.txt to the index, but have not added edureka6.txt to the index and made changes in the previous file. I want to commit all the changes in the directory at once. Please refer to the snapshot below.This command will submit a snapshot of all changes in the working directory, but only changes to trace files that were used at some point in historygit addAdded file. Therefore, edureka6.txt has not been committed because it has not been added to the index. However, changes to all previous files in the repository are committed, i.eEdureka1. TXT, edureka2. TXT, edureka3. TXT, edureka4. TXT and edureka5. TXT. I have now completed the required commit in the local repository. Note that before affecting changes to the central repository, changes should always be pulled from the central repository to the local repository to update the work of all collaborators contributing in the central repository. For this, we will usepullCommand.

Pull (Pull)

  git pullCommand to get changes from the remote repository to the local repository. It merges upstream changes in local repositories, a common task in Git-based collaborations. But first, you need to set the central repository to the original repository using the following command:Git remote add Origin Now that the origin is set, let's use pull to extract the file from the origin. To do this, use the command:git pull origin masterThis command copies all files from the master branch of the remote repository to the local repository.Since my local repository has been updated with files in the Master branch, the message is already up to date. See the screenshot above.Note: You can also try to extract files from other branches using the following command: git pull origin branch-nameYour local Git repository is now updated with all the recent changes. Now is the time to use itpushThe command is changed in the central repository.


This command transfers the commit from the local repository to the remote repository. It is the opposite of a pull operation. Commit imports to a local repository and exports to a remote repository. git pushIs used to publish local changes to a central repository. Once you have accumulated multiple local commits and are ready to share them with the rest of the team, you can push them to the central repository using the following command:Git push remote Note: * * * *This remote references the remote repository that was set up before using the pull command.This pushes changes from the local repository to the remote repository, along with all necessary commits and internal objects. A local branch will be created in the target repository. Let me show you.The files above are the ones we have already committed in the COMMIT section. They are all"push-ready". I'm going to use the commandgit push origin masterTo reflect these files in the central repositorymasterIn the branch.Now let's check to see if these changes took place in my central repository.Yes, indeed. :-) To prevent overwriting, Git does not allow push when it causes a non-fast-forward merge in the target repository.Note: A non-fast-forward merge is an upstream merge that merges with an ancestor or parent of a child branch.To enable this merge, use the following command:Git push remote - forceThe command above forces the push operation, even if it results in a non-fast-forward merge. By now, I hope you've learned the basics of Git. Now let's take a closer look at branching and merging in Git.

Branch (Bearing)

Branches in Git are just Pointers to specific commits. Git generally prefers to keep its branches as lightweight as possible. There are basically two types of branches, local branches and remote trace branches. The local branch is just another path in your working tree. On the other hand, remote trace branches have special uses. Some of them are:

  • They link your work from the local repository to work on the central repository.
  • When you use Git pull, they automatically detect which remote branch you want to fetch changes from.

You can check what the current branch is using the following command:git branchThe slogan you should always recite when branching is "Branch early, branch often." To create a new branch, we use the following command:git branch branch-name The figure above shows the workflow when creating a new branch. When we create a new branch, it comes from the Master branch itself. Because there is no storage/memory overhead to create multiple branches, logically dividing your work up is much easier than having large branches. The branch includes work for a specific commit and all parent commits. As you can see in the figure above, newBranch has been separated from the primary server, so a different path will be created. Use the following command:git checkout branch_nameThen performgit commitNow, let's look at how to use the commit branch.The branch includes work for a specific commit and all parent commits. As you can see in the figure above, newBranch has been separated from the primary server, so a different path will be created. Use the following command:git checkout branch_nameThen performgit commit Here, I created a new branch called "** EdurekaImages**" and switched to the new branch using the command git checkout. A shortcut to the above command is:git checkout -b[ branch_name]This command creates a new branch and checks the new branch at the same time. Now, when we are in the branch EdurekaImages, add and submit the text file edureka6.txt using the following command:git add edureka6.txt git commit -m "adding edureka6.txt"


Merging is a method of combining the work of different branches. This will allow us to branch, develop new features, and then merge them back together.The figure above shows us two different branches - newBranch and Master. Now, when we merge the work of newBranch into the Master, it will create a new commit that contains all the work of the Master and newBranch. Now, let's merge the two branches using the following command:git merge branch_nameThe branch name in the above command should be the branch that you want to merge into the currently checked out branch. Therefore, make sure you have checked out in the target branch. Now, let's merge all the work of the branch EdurekaImages into the Master branch. To do this, I'll use it firstgit checkout masterCommand to check out the master branch, and then usegit merge EdurekaImagesCommand to merge EdurekaImages.As shown above, all data in the branch name is merged into the Master branch. The text file edureka6.txt has now been added to the Master branch. Merging in Git creates a special commit that has two unique parents.


This is also a way of combining work between different branches. The pivot takes a set of commits, copies them, and stores them outside of your repository. The advantage of rebasing is that it can be used to make linear commit sequences. If rebenchmark is done, the history of the commit log or repository remains clean. Let's see how it happens:Now, our work on newBranch follows master's, and we have a nice linear commit sequence. Note: Resetting the benchmark also prevents upstream merges, which means you can't put master after newBranch.Now, to reset master, type the following command in Git Bash:git rebase master This command moves all of our work from the current branch to the master node. They appear to be developed sequentially, but in parallel. Now that you're done with this Git tutorial, here are some tips and tricks you should know. : -)

Archive repository

Git archive master -- format=zip -- output=.. / Stores all files and data in a zip file instead of a.git directory. Note that this only creates a snapshot, thus completely ignoring version control. This is handy when you want to send files for review to clients that don't have Git installed on their computers.

Bind repository

It converts the repository to a single file. Git bundle create.. / rebo. bundler Master This pushes the Master branch to a remote branch that is contained only in the file, not in the repository. Another way is: CD.. git clone repo.bundle repo-copy -b master cd repo-copy git log cd.. /my-git-repo

Hide uncommitted changes

When we want to undo temporary additions or any kind of data, we can temporarily "hide" them. Use the following command: git status git stash git status When you want to reapply the "hidden" changes, use the following command: I hope you enjoyed this Git Bash tutorial and learned about the commands and actions in Git.

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.