Useful git command for developers

Git log

git log --pretty=format:"%h%x09%an%x09%ad%x09%s"

Adding it to the bash script

echo "alias gitl='git log --pretty=format:"%h%x09%an%x09%ad%x09%s"'" >> ~/.bashrc
source ~/.bashrc

From now on you can type:

gitl
3918a7c8        Vivek Bhadra    Wed Oct 23 14:02:16 2019 +0100  Integrating the esp8266 wifi module in SPRESENSE sdk.
cbce5ee6        Vivek Bhadra    Tue Oct 22 13:16:11 2019 +0100  Fixes.
d4fc23c7        Vivek Bhadra    Wed Oct 16 16:01:03 2019 +0100  Enhanced camera App for better user interactivity.
6cc05f93        Vivek Bhadra    Wed Oct 16 13:47:02 2019 +0100  Customized for delayed capture of 1 mins.

and see the formatted git log, change the format as you would like.

Checking your current git branch

git branch -av

Add it to the .bashrc

echo "alias gitb='git branch -av'" >> ~/.bashrc
source ~/.bashrc

Type:

gitb
* customize_for_12_demo             3918a7c8 Integrating the esp8266 wifi module in XYZ sdk.
  master                            ee8f2ddf Merge pull request #41 from XXX/release-v1.4.1
  remotes/origin/HEAD               -> origin/master
  remotes/origin/auto_build_doxygen a5681292 Updated readme and only build doc from master
  remotes/origin/master             ee8f2ddf Merge pull request #41 from XXX/release-v1.4.1
  remotes/origin/release_1.0.1      ec7f3dab Release tag XYZ_1.0.001 YYY.0.001 release.

on the console to check the git branch you are on.

Creating a patch from multiple commits

git format-patch -x --stdout > patch-ddmmyyy.patch
git format-patch --ignore-space-change --ignore-space-at-eol --ignore-all-space -n --stdout > ~/patch4-LH3-18466-19092019.patch

where n is the number of commits you want to get back.
–ignore-space-change
–ignore-space-at-eol
–ignore-all-space
the above options are self-explanatory.

How to remove untracked files in git?

  • Print out the list of files which will be removed (dry run)
git clean -n
  • Delete the untracked files from the repository
git clean -f

How to not display the untracked files with git status?

git status --untracked-files=no

or


git status -uno

Renaming a local branch in command line

switch to the branch you want to rename

git checkout branch-name

Then rename it as below:

git branch -m new-name

How to work in private branch in GIT

Time to time we have to work on source code which are downloaded from a git repository, modify the code, implement the functionality, test it, get the code reviewed and then push it up to the repository. Now, how do we achieve this with the following in mind:

1. I do not want to do a git pull on my local source code base frequently simply because it is cumbersome.

2. I however, want to catch up with the mainline code (let’s call it master branch) so that when I am ready to push the code up I do not have enormous amount of merge conflicts, or conflicting other features which somehow difficult to accommodate at the last minute. So to avoid any surprise I would like to catch up with the mainline (be it master branch)  time to time (I do it one a week at least).

3. I do not want to use merge command to catch up with the mainline code as it messes up with the git log.

All these points in mind I would recommend the below working principle:

1. git clone <git repository>

2. git checkout <branch name> (or by default you maybe already in the master branch. This would depend from source code to source code or repository to repository, so better check where you want to base your implementation.)

3. Now, after the above step 2, we are sitting at the branch where we want to start our implementation/bug fixing etc. Let’s call this “master” branch.

4. I would now like to create my private branch called “my_private”. To create a private branch do the below:

git checkout -b my_private

5. Now carry on with your implementations.

Multiple commits in private branch

While developing a new feature you might like to do it in a incremental way, i.e. when a little milestone is achieved you may like to commit it before carrying further. To do this do the below:

git add <file names>

git commit -m “commit message”

Now, if you carry on like this you would end up with multiple commits on your private branch. However, for doing review and pushing it up, it would be nice if all your changes/new implementations comes under one single commit. To achieve this you can use git interactive rebase command as below:

git rebase -i HEAD~n

where n stands for the number of commits (from the latest commits) to be included in the single commit.

This will bring up a screen which will look like as below:


pick 794969c79 Revert all commits that comprise 10x feature
pick 808bc82dd Added database version and product code into DiagnosticsPresenter.
pick 0b4d6973c Implementing Recent Tab
pick 0c284d078 Implementing Recent Tab

# Rebase 75ff6ea39..1f778b29d onto 75ff6ea39 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Leave the first pick but convert the second and all other picks to squash as below:


pick 794969c79 Revert all commits that comprise 10x feature
squash 808bc82dd Added database version and product code into DiagnosticsPresenter.
squash 0b4d6973c Implementing Recent Tab
squash 0c284d078 Implementing Recent Tab

# Rebase 75ff6ea39..1f778b29d onto 75ff6ea39 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Write and save, and you will be presented with the screen where you can decide what you would like to have as the new single commit message. Save and exit and you would have all your local commits squished into one single commit as below:


107b084c0 vbhadra Mon Sep 9 10:36:26 2019 +0100 Rework Recents presenter to communicate with STEDs facade instead of other presenter.
1aef95ec0 Andrei Tucma Tue Sep 10 08:30:19 2019 +0100 ITC5 calibration: calibrate popover option only available for itc5
8f761ce9c Robert Fry Mon Sep 9 15:57:09 2019 +0100 Possible fix for internal GPS to show signal strength
475af1405 dlangford Mon Sep 9 15:43:23 2019 +0100 STEDS Message popup design change
eb1eaf342 Algirdas Balciunas Mon Sep 9 14:24:20 2019 +0100 GPS source switching fix
460b05fee Algirdas Balciunas Mon Sep 9 14:11:25 2019 +0100 Fixed ship/cargo values in AIS data dialog
d7a47f77b Algirdas Balciunas Mon Sep 9 10:29:52 2019 +0100 Fix for app slowdown on Atom devices

Now, let’s say you want to catch up with the master branch. Go back to your master branch as below:

git checkout master (remember your base code maybe from a branch which not called master. It doesn’t matter, just go to the branch where you started)

git status (this will tell you how many commits your master is behind the latest commit)

git pull (this will bring all the latest changes delivered by others into your local master branch)

Now, go back to your private branch:

git checkout my_private (move to my_private branch)

git rebase master  (this will rebase your my_private branch with the master)


107b084c0 vbhadra Mon Sep 9 10:36:26 2019 +0100 Rework Recents presenter to communicate with STEDs facade instead of other presenter.
1aef95ec0 Andrei Tucma Tue Sep 10 08:30:19 2019 +0100 ITC5 calibration: calibrate popover option only available for itc5
8f761ce9c Robert Fry Mon Sep 9 15:57:09 2019 +0100 Possible fix for internal GPS to show signal strength

Time to push up your changes

1. Go back to the master branch

2. Merge your private branch with master

3. git push


<strong>git checkout master</strong>

<strong>git merge my_private</strong>

<strong>git push</strong>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Git Rebasing

 

Problem Description

Suppose you have a local GIT repository where dev-stable-topic is the development branch and time to time the releases are made to the customer from a remote GIT repository which has a branch called release-stable from where the selective changes are merge in a controlled way. The remote repository is hosted in a separate linux server.

Read More »