Skip to main content

Command Palette

Search for a command to run...

Git Commands Decoded: Simplifying Version Control for Developers

Updated
6 min read
Git Commands Decoded: Simplifying Version Control for Developers
D

Data Engineer @Midoffice Data

Welcome to Day 10 of the #90DaysOfDevOps challenge.

Introduction

In this blog, we aim to unravel the complexities of Git commands and provide you with a clear understanding of how they work. Whether you're a beginner exploring version control or an experienced developer looking to enhance your Git workflow, this blog will serve as your go-to resource. We'll break down each command, explain its purpose, and guide you through practical examples, simplifying the process of managing your codebase and collaborating with others. Get ready to unlock the power of Git commands and streamline your development journey. Let's dive in and decode the world of version control with ease!

Basic Git Commands

git init: Initializes a new Git repository in the current directory.

git init

git clone [repository]: Creates a local copy of a remote repository.

git clone https://github.com/username/repository.git

git config --global user. name "[Your Name]": Sets your username globally for all Git repositories.

git config --global user.name "USER_NAME"

git config --global user.email "[your-email@example.com]": Sets your email globally for all Git repositories.

git config --global user.email "xyz@gmail.com"

git add [file(s)]: Adds file(s) to the staging area.

git add myfile.txt

git commit -m "[message]": Commits the changes in the staging area with a message.

git commit -m "Added new feature"

git status: Shows the status of the repository.

git status

git diff: Displays the differences between the working directory and the staging area.

git diff myfile.txt

git log: Shows the commit history.

git log

git pull: Fetches and merges change from a remote repository.

git pull origin master

git push: Pushes local commits to a remote repository.

git push origin master

git remote: Lists remote repositories.

git remote -v

git fetch: Retrieves changes from a remote repository.

git fetch origin

git rm [file(s)]: Removes file(s) from the repository.

git rm myfile.txt

git mv [oldpath] [newpath]: Renames or moves a file.

git mv oldfile.txt newfile.txt

Git Branching

Branching in Git is a powerful feature that allows you to create separate lines of development within a repository. It enables collaboration, isolation of work, and the ability to experiment with new features or fixes without impacting the main codebase.

The main branch in Git is typically called "master" or "main" (depending on the naming convention). When you create a new branch, it starts as an identical copy of the current branch (usually the main branch). From there, you can make changes, add commits, and merge them back into the main branch or other branches as needed.

Branching Commands

Creating a New Branch:

To create a new branch called "feature-login2" without committing on master, you can use:

git checkout -b feature-login2

Switching to a Branch:

To switch to a branch, you can use the git checkout a command followed by the name of the branch. For example, let's switch to the "feature-login2" branch:

git checkout feature-login2

Now, any changes you make and commits you create will be isolated within the "feature-login" branch.

Making Changes and Committing:

Let's say you make some changes to implement a login feature. After making the changes, you can stage and commit them using the standard Git commands:

git add .
git commit -m "file added into new branch"

Merging Branches:

Once the changes in the "feature-login" branch are complete and tested, you may want to merge them back into the main branch (e.g., "master" or "main"). To do that, switch to the main branch and use the git merge command:

git checkout master
git merge feature-login2

Git will automatically merge the changes from the "feature-login2" branch into the main branch, incorporating your work into the main codebase.

Deleting a Branch:

After merging the changes, you can delete the branch if it's no longer needed. The following command will delete the "feature-login" branch:

git branch -D feature-login

Git Revert and Reset

In Git, git revert and git reset are two different commands that serve distinct purposes:

  1. git revert: The git revert command is used to create a new commit that undoes the changes made in a previous commit. It allows you to reverse the effect of a specific commit while preserving the commit history. This is useful when you want to undo changes without discarding any previous commits.

  2. git reset: The git reset command allows you to move the current branch pointer to a specific commit. It can be used to undo commits, unstaged changes, or move the branch pointer to a different commit, potentially discarding commits.

Difference Between Git Revert & Reset

ResetRevert
OperationRewrites commit history by moving the branch pointerCreates a new commit to reverse the changes
ImpactPotentially dangerous as it discards commits and historySafer option as it preserves commit history
CollaborationNot recommended for shared branches, as it can cause conflicts for other developersA safer option for shared branches, as it preserves commit history and allows others to easily understand the changes made
UndoingCan be used to undo commits and return to a previous stateCan be used to undo commits without altering the commit history
Usagegit reset [commit]git revert [commit]

Task

- Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. 
  This should be in a branch coming from `master`, 

- swithch to `dev` branch ( Make sure your commit message will reflect as "Added new feature").

- version01.txt should reflect at local repo first followed by Remote repo for review. 

- Add new commit in `dev` branch after adding below mentioned content in Devops/Git/version01.txt:
  While writing the file make sure you write these lines

 - 1st line>>  This is the bug fix in development branch
 - Commit this with message “ Added feature2 in development branch”

 - 2nd line>> This is wrong code
 - Commit this with message “ Added feature3 in development branch

 - 3rd line>> This feature will destroy everything from now.
 - Commit with message “ Added feature4 in development branch"

- Restore the file to a previous version where the content should be “This is the bug fix in development branch”
- Merge the dev branch with master

Solution

Restore the file to a previous version where the content should be “This is the bug fix in the development branch”

1) Using the reset command

Now the HEAD is pointing to commit "Added feature2 in development branch" which consists of the content “This is the bug fix in the development branch”

2) Using the revert command

Merge the branches

Conclusion

In conclusion, understanding and utilizing Git commands can greatly simplify version control for developers. By mastering commands such as git init, git add, git commit, and git push, developers can effectively manage their codebase and collaborate with ease. Advanced commands like git branch, git merge, and git rebase provide additional flexibility and control over branch management and commit history. By decoding Git commands, developers can streamline their workflows, enhance productivity, and maintain a well-organized and collaborative development environment.

More from this blog

Dhanashri's DevOps Corner

12 posts