git for little learners

Jyothi
4 min readApr 20, 2020

--

https://medium.com/@jyos.sw/git-starters-689fcf86bec1 explains the basic git clone, commit, push commands and pull request process.

Now we need to understand little more usage of git based on some project requirements.

I will list down some of the project requirements and corresponding git commands.

Requirement#1 : Multiple active branches on main repo

Project team maintains multiple branches based on the customer releases. One master or some branch can be an active branch for active development.

For each customer release, team might maintain a branch for specific features and development.

In these cases there are multiple active branches on main repo.

In these cases our task (set of code changes) should go into multiple branches on main repo. How do we do that , its simple.

Here “git log” and “git cherry-pick” commands come into picture:

  • “git log” command shows list of commits made from latest to the beginning.

“git log” shows each commit full description, if you want to avoid full description, you can use “git log — oneline”.

  • “git cherry-pick commit-id” command applies the commit corresponding to commit-id to the current branch.

Now we will see how to use these commands to apply your code changes to another branch.

Suppose you are in branch1 and you have a commit-id1 , you need to merge the commit-id1 to branch2:

  • First check the branch you are into using “git branch” command.
  • If you are not in branch1 use “git checkout branch1” command to move to branch1.
  • Use “git log — oneline” command to display commits of branch1
  • Note commit-id which you need to merge.
  • Use “git checkout branch2” command to move to branch2.
  • Use “git cherry-pick “ command with the noted commit-id to apply those changes to branch2.

These above steps are used to merge the commit in another branch in your development PC.

Now we need to push these changes to your local repo in git server and raise a pull request to merge to main repo.

“git push -f local_repo branch2” command with your local_repo details and branch name can be used to push to git server. Then raise a pull request to merge to main repo.

I hope you are now familiar with the usage of “git cherry-pick” :o)

Requirement#2: generation of patches for your committed changes

Sometimes or many times we may have to give patches to the customer for the fixes or features you developed.

You have made the changes and pushed to the git server, now how to generate patch files for those commit-ids.

Very simple. git provides a facility using a command “git format-patch -n”, where is n is number of latest commits.

Suppose if you want to generate patch files for 3 latest commits, pass -3 to git command as “git format-patch -3”.

It generates 3 patch files for last 3 commits. You can view those files by listing the files (“ls”).

Requirement#3: applying patches to your code

sometimes we might have to apply patches to your code, so how do you apply patches.

There are 2 types of patches and you can apply patches in 2 different ways.

There are 2 types of patches: patch file with only code changes another is patch with code changes and git commit info.

Patch file containing only code changes:

  • These changes can directly applied using “git apply patch-file-name” command by passing your patch file name. Before using this command you make sure that you have copied your patch file to the current directory.

Patch file containing the code changes and git commit info:

  • This patch can applied using either “git apply” or “git am” commands.
  • When you use “git apply” command, only code changes will be applied to your code.
  • Whereas when you use “git am path-file-name” command, along with the code changes , commit info also gets added to your code.
  • After using “git am” command, you can view “git log” for the new commits.
  • Latest commit info can be modified using “git commit — amend” command.

If you want to modify commit info for other commits than latest commit , you need to use “git rebase” command.

These commits and code changes can be pushed to your local repo using “git push” command and you can raise a pull request for merging to main repo.

Requirement#4: Patch reversal.

There are some code changes (mentioned in a patch file) exist in your code and you need to revert those code changes.

Simple , you can do using “git apply -R” command by passing the patch file name.

There are other powerful commands “git reset” and “git rebase” will be explaining in other posts.

Hope you have learned useful stuff.

--

--

No responses yet