< Blogs
Hanbo

Hanbo

May 4, 2023

Introduction to Git Controls (2)

In our previous article, we covered some of the basic Git controls that every developer should know. In this article, we'll go a step further and explore some more advanced Git controls that can help you manage your code more effectively.

Rebase

Rebasing is a powerful Git command that allows you to rewrite the history of your repository. It allows you to take a series of commits from one branch and apply them to another branch as if they had been committed there all along.

To rebase a branch, first, switch to the branch you want to rebase and then use the git rebase command followed by the name of the branch you want to rebase onto:

git checkout feature-branch
git rebase main

This will take all the changes you made on the feature branch and apply them on top of the main branch.

Rebasing can be useful when you want to keep your commit history clean and linear. It can also help you avoid merge conflicts by integrating changes from multiple branches before they are merged.

However, rebasing can be dangerous if you are working on a public branch that other developers are also using. If you rewrite the history of a public branch, you may cause conflicts for other developers who are working on that branch.

Interactive rebase

Interactive rebase is a variation of the git rebase command that allows you to modify and reorder your commits before applying them to another branch.

To start an interactive rebase, use the git rebase command with the -i flag:

git rebase -i HEAD~3

This will open up an interactive window that shows all the commits you want to rebase. From here, you can reorder, edit, or delete commits as needed.

Interactive rebase can be useful for cleaning up your commit history or combining multiple commits into a single commit before pushing them to a remote branch.

Cherry-pick

Cherry-picking is a Git command that allows you to apply a single commit from one branch to another branch.

To cherry-pick a commit, first, switch to the branch you want to apply the commit to, and then use the git cherry-pick command followed by the hash of the commit you want to apply:

git checkout main
git cherry-pick 123456

This will apply the changes from the specified commit to the main branch.

Cherry-picking can be useful when you want to apply a bug fix or feature from one branch to another branch without merging the entire branch.

Revert

Revert is a Git command that allows you to undo a previous commit without removing it from your commit history. It creates a new commit that undoes the changes made by the previous commit.

To revert a commit, use the git revert command followed by the hash of the commit you want to revert:

git revert 123456

This will create a new commit that undoes the changes made by the previous commit.

Revert can be useful when you want to undo a change that has already been pushed to a remote branch without rewriting the history of the branch.

Stashing

Stashing is a Git command that allows you to save changes that you are working on temporarily. This can be useful when you need to switch branches or work on a different feature without committing your changes.

To stash your changes, use the git stash command:

git stash

This will save your changes to a temporary storage area.

Once you're ready to apply your changes, you can use the git stash apply command to retrieve your changes from the stash:

git stash apply

Sometimes, when you stash changes using git stash, you may want to apply only a specific part of the stash instead of all the changes. This can be useful when you have multiple changes in the stash, but only need to apply one or a few of them.

To apply a specific part of the stash, you can use the git stash apply command with the --index flag and the index number of the specific stash entry you want to apply. For example, if you want to apply the second entry in the stash, you can use the following command:

git stash apply --index stash@{1}

By using these commands, you can apply specific parts of your stashed changes, giving you more control over how you apply changes to your code.

You can use git stash list to show a list of all stashed changes:

>>> git stash list
stash@{0}: WIP on develop: a23re14 lexer changes
stash@{1}: WIP on develop: a23re14 lexer changes
stash@{2}: WIP on master: 56d87e parser changes

You can use the git stash pop command to retrieve your changes and remove them from the stash:

git stash pop

Stashing can be useful when you need to switch contexts quickly without losing your changes.

Conclusion

Git is a powerful version control system that provides a wide range of controls for managing your code. By understanding the more advanced Git controls outlined in this article, you can take your Git skills to the next level and manage your code more effectively. However, it's important to use these controls carefully and to communicate with other developers to avoid conflicts and other issues. With practice and experience, you'll be able to use Git to its full potential and take your development skills to new heights.

More Topics

All Blogs
Team Spotlight
Startup Spotlight
How To
Blog
Podcast
Product Updates
Wearables
See All >
CEO and Co-Founder of Bioniq - Vadim Fedotov

CEO and Co-Founder of Bioniq - Vadim Fedotov

In this podcast with Kyriakos the CEO of Terra, Vadim Fedotov a former professional athlete turned entrepreneur, shares his journey in founding Bioniq.

Terra APITerra API
December 10, 2024
5 Lessons for Standing Out at HLTH

5 Lessons for Standing Out at HLTH

5 lessons from team Terra API for making a lasting impact at HLTH: from engaging senses to building real touch points, here’s what we learned from the HLTH event.

VanessaVanessa
December 5, 2024
November '24 Updates by Terra

November '24 Updates by Terra

Terra’s Latest Updates: Zepp Metrics, Support Revamp, and Teams API Enhancements 🚀✨

Alex VenetidisAlex Venetidis
December 1, 2024
Strava Pulls the Plug on their API: What This Means for Developers

Strava Pulls the Plug on their API: What This Means for Developers

Strava discontinued their API service, changing the ecosystem of third-party apps that have relied on their platform. How can developers react to this?

Terra APITerra API
November 21, 2024
Alternatives to the latest changes in the Strava API

Alternatives to the latest changes in the Strava API

Strava just introduced big changes to their API program. These changes will basically kill off a lot of apps. Use Terra API instead to avoid this

Kyriakos EleftheriouKyriakos Eleftheriou
November 19, 2024
next ventures
pioneer fund
samsung next
y combinator
general catalyst

Cookie Preferences

Essential CookiesAlways On
Advertisement Cookies
Analytics Cookies

Crunch Time: Embrace the Cookie Monster Within!

We use cookies to enhance your browsing experience and analyse our traffic. By clicking “Accept All”, you consent to our use of cookies according to our Cookie Policy. You can change your mind any time by visiting out cookie policy.