Git merge, rebase and extras

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
This series will teach basics of git and how to start contributing to projects by making pull requests etc. git is a version management tool which helps us store code in repos.
What is version control? When we write code in a team of developers, we can expect others to work on the same code base. How can we achieve that? One way is - one person makes the changes and shares the project with next person, then they make more ...
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

Both git merge and git rebase are used to integrate the changes from one branch into the other branch. We usually create a feature branch - make the code changes in form of commits and then either merge or rebase it into master. See the below image which demonstrates merge and rebase.

In case of merge, we take all the changes of the feature branch and make one merge commit on the master branch. This is a non-destructive operation. The feature branch is not affected in any way. In case of merge - we lose the history of commits of the feature branch. But we get a much cleaner looking master branch.
git checkout feature
git merge master
# or
git merge feature master
In case of rebase, we move all the commits of the feature branch to the top of master branch. This way we copy all commits as is. We get the entire history of the changes in the feature branch in the master branch.
git checkout feature
git rebase master
git push --force this can rewrite the history in git and cause lot of trouble.Git log command is used to see the commit history of the currently checked out branch.
git log
We could also apply some nice filters
git log --author <name>
git log --before <date> # --after is also supported
git log --oneline # one line per commit
git checkout can be used to do multiple things.
# Create a new branch using
git checkout -b <branch>
# checkout to existing branch
git checkout feature
If you have made some changes to a file, but you want to get the original file
git checkout filename
# checkout to a commit
git checkout <commit_id>
When you have made some half-baked changes and you want to switch branches - you can save your changes in a stash and switch branches. When you are done, you can switch back and pop the stash.
# save changes to stash
git stash
# load changes from stash
git stash pop
It is possible to have multiple stashes
# list stashes
git stash list
# apply a specific stash
git stash apply stash@{2}
# create branch from stash
git stash branch testbranch
When you want to undo some commits/move the git HEAD to a previous commit, we make use of git reset. There are three types of reset - soft, mixed, hard
This is the most direct, DANGEROUS, and frequently used option. When passed --hard The Commit History ref pointers are updated to the specified commit. Then, the Staging Index and Working Directory are reset to match that of the specified commit. Any previously pending changes to the Staging Index and the Working Directory gets reset to match the state of the Commit Tree. This means any pending work that was hanging out in the Staging Index and Working Directory will be lost.
git reset --hard <commit_id>
This is the default operating mode. The ref pointers are updated. The Staging Index is reset to the state of the specified commit. Any changes that have been undone from the Staging Index are moved to the Working Directory.
git reset --mixed <commit_id>
When the --soft argument is passed, the ref pointers are updated and the reset stops there. The Staging Index and the Working Directory are left untouched.
git reset --soft<commit_id>
If you want to undo a specific commit from the history, we can make use of git revert. This creates a new commit on the HEAD which undoes the changes of the specified commit id.
git revert <commit_id>
git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.
git cherry-pick <commit_id>
If you want to modify the last commit, use --amend. This will edit the last commit, in case you want to add more changes to it.
git commit --amend
If you made some changes which you don't need anymore. You never commited or even staged it, we can use git clean to remove the changes.
git clean