Introduction
Git is a powerful version control system widely used by developers to manage their codebase efficiently. One of the key features of Git is its ability to handle branching and merging, allowing developers to work on different features or bug fixes in isolated environments without affecting the main codebase. Also, Git provides options like reverting changes and rebasing to maintain a clean and organized commit history. In this blog, we understand the concepts of Git branching, reverting, merging, and rebasing to help you with a better understanding of version control workflows.
Git Push
In Git, the git push
command is used to upload your local commits and branches to a remote repository, typically hosted on a service like GitHub, GitLab, or Bitbucket. When you've made changes locally and you're ready to share them with your team, you use git push
to send those changes to the remote repository. This makes your work accessible to others and allows for collaboration.
When you push a branch, Git transfers the commits and their associated changes to the remote repository, updating the branch there. This ensures that your local work is synchronized with the shared version, making it available for other team members to see, review, and build upon.
Create and commit "pushfile.txt" in the local Git repository, then push it to a connected GitHub repository for synchronization.
Git Pull
In Git, the git pull
command is used to update your local branch with the latest changes from a remote repository. It combines two actions: fetching changes from the remote repository and then merging those changes into your local branch.
When you pull, Git first contacts the remote repository and retrieves the latest commits and changes. It then attempts to automatically merge these changes into your local branch. If there are conflicts between the remote changes and your local changes, Git will prompt you to resolve those conflicts manually before the merge is complete.git pull
is a way to keep your local version of a branch up-to-date with the shared version on a remote repository. This ensures that you're working with the most recent changes and enables smooth collaboration among team members.
Here we successfully pull the code from the GitHub repository
What is Git Branching?
Think of Git branching like making copies of your project's code
so you can work on different features or fixes separately. Each copy is called a "branch
." It's as if you're working on different versions of your project at the same time.
These branches allow you to experiment without affecting the main version (usually called the "master
" or "main
" branch). If your experiments succeed, you can combine or "merge
" those changes back into the main version. If not, you can just delete the branch without causing any problems in the main version.
Branching is especially useful for collaborating with others. You can each work on your own branches, and when everything is ready, you can merge everything together. This keeps the main version stable while still letting you explore new ideas.
What is Git Reset and Revert?
Reset
The term reset itself stands for undoing changes. Reset is often referred to as a confusing command. Reset does different things in different contexts.
We know that if you want to move the branch we use the following commands commit, merge, Rebase
If you observed, these commands are not explicitly used for moving the branch. These are moved as a side effect of creating a new commit. So we have a command that is specially used for moving a branch. Reset does this. It is used to move the branch.
Reset has options
--hard -> moves the files to the working area and staging area
--mixed - moves the files only to the stage area. (default option)
--soft- does not move the file.
We can say that, Reset moves the current branch and optionally copies the data from the repositories to the working or staging areas.
Revert
In Git, the git revert
command is used to create a new commit that undoes the changes made in a previous commit. It's like applying a reverse change to your codebase. Instead of removing the changes directly from history (like with git reset
), git revert
creates a new commit that counteracts the changes introduced by a specific commit.
This is especially helpful when you want to fix mistakes or remove certain changes without altering the overall commit history. The process involves creating a new commit with the opposite effect of the commit you want to revert. This way, you can maintain a clear record of what changes were made and when, while still correcting errors or unwanted additions.
Here we have two files index.txt and test.txt.and we made the changes in both files and committed those changes.
Now, I realize that the changes I have made the latest in the test file (test file change2)I don't want to commit those changes in the test file I want to revert these changes
Now i want to revert the changes in the test.txt file (test file change 2)
Run the below command first git revert [commit ID]
Here you will get the message that reverting your changes
Here we revert the test file change successfully.
What is Git Merge?
In Git, the git merge
command is used to combine changes from different branches. When you have worked on a separate branch and want to integrate its changes into another branch (usually the main one), you perform a merge. Git analyzes the differences between the two branches and attempts to combine the changes automatically. If there are any conflicting changes (changes that occurred in the same lines of code), Git prompts you to resolve those conflicts manually.
Merging involves creating a new "merge commit" that brings together the history and content of the two branches. This allows you to incorporate your work from different branches into a unified codebase. However, it's important to review and test the merged code to ensure it works as expected before considering the changes fully integrated.
What is Git Rebase?
Rebase is one of the most powerful git command. Rebase is often used as an alternative to merging. Rebasing a branch updates one branch with another by applying the commits of one branch on top of the commits of another branch.
Git rebase is used to clean up our local commit history. Rebase is an advanced command that is used rarely. Merge preserves history while rebase doesn't preserve history.
Do not use rebase When
The branch is public when it is shared to all the developers. Most of the teams prefer to merge over rebase.
common places where we use rebase
cleaning up your commits before sharing your branch. Pulling changes from another branch without merge.
Git Checkout
In Git, the git checkout
command allows you to switch between different branches or commits in your repository. It updates your working directory to match the selected branch or commit, essentially letting you explore different snapshots of your project's history. This can be used to work on different features, review past changes, or even create new branches for experimentation.
Git Diff
Diff command is used in git to track the difference between the changes made on the file. Diff commands take two inputs and reflect the differences between them
To check the changes in the working area and staged area use the command git diff
To check the changes between the stage area and the repository area use the command git diff --staged
Hope this explanation for the advanced git techniques helpful to you
Conclusion
We've completed Advanced Git & GitHub Techniques. We learned about Git and GitHub features like branching, and merging. Advanced Git commands like revert, reset, rebase, diff and other important commands
Keep exploring and experimenting with Git, as it is a powerful version control system with a wealth of features to support seamless software development. Stay tuned for my next blog post, where we'll continue to explore more exciting topics in the world of DevOps and version control.
I look forward to continuing this journey of exploration and learning together. I will keep sharing my learnings and knowledge here with you.
Thank you!
Neha Bawane