Most useful git commands

Vidhyadharan Deivamani - Apr 27 '22 - - Dev Community

Most useful git commands

Introduction

It was the year 2018, and I had joined a new team for the development of new enterprise cloud products. Before 2018, we had been utilizing SVN for decades.

I have considerable knowledge of git as a result of my experience working on open source projects.

In 2018, I addressed my boss about using git for our new product development. My boss put his faith in me ❤️ and requested me to oversee git codeline activity. We started with on-premises Bitbucket and then switched to business Github.

Many of my coworkers expressed reservations about the setup, commit and push.

And, as time went on, a slew of new questions arose 🤔.

As a result, I've developed 👨‍💻 git commands that we use frequently in our development.

This post is dedicated to git users today!

Table of contents

How to see my last commit

This command is useful to see 'my last commit'

git log --name-status HEAD^..HEAD
Enter fullscreen mode Exit fullscreen mode

How to output git log with the first line only

This command is useful to print commit history by one line

git log --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

How can i view a git log of just one user

This command is useful to see log by particular user

git log --author="vidhya"
Enter fullscreen mode Exit fullscreen mode

will match a commit made by "Vidhyadharan Deivamani" and also

git log --author=deiva
Enter fullscreen mode Exit fullscreen mode

credits

How do I force git pull to overwire local files

This command is useful when you got git < branch > | MERGING issues

git fetch --all
git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

or

git reset --hard origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

Note: the above command will overwrite your local files

credits

How do I determine the url of git repository

Get only the remote url

git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode

or

Get the entire url

 git remote show origin
Enter fullscreen mode Exit fullscreen mode

credits

How do I go to specific revision

Move to a specific commit or checkout to previous revision

First note the sha or revision number

git log --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

Then

Check out the specific commit

 git checkout <sha1 or revision>
Enter fullscreen mode Exit fullscreen mode

credits

How do I tag a specific commit

Tag a specific commit

First note the sha or revision number

git log --pretty=oneline
Enter fullscreen mode Exit fullscreen mode

Then

Tag with revision number

 git tag -a v1.2 444554738120233382c5912ebbca32592ba765ad -m "Message here"
Enter fullscreen mode Exit fullscreen mode

Warning: This creates tags with the current date

To add a tag with specific date, add GIT_COMMITTER_DATE environment variable with this YYYY-MM-DD HH:MM format.

 GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -a v1.2 444554738120233382c5912ebbca32592ba765ad -m "Message here"
Enter fullscreen mode Exit fullscreen mode

To push a single tag:

git push origin <tag_name>
Enter fullscreen mode Exit fullscreen mode

And the following command should push all tags (not recommended):

git push --tags
Enter fullscreen mode Exit fullscreen mode

credits

How do i delete a specific branch

To delete the git remote branch

git push --delete origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

To delete the local branch

git branch -D <branch_name>
Enter fullscreen mode Exit fullscreen mode

How to change author in a commit

Sometime working in opensource and closed source, you might commit closed source with your personal email id.

git commit --amend --author="vidhyadharan deivamani <it.vidhyadharan@gmail.com>" -m "commit description"
Enter fullscreen mode Exit fullscreen mode
git commit --amend --reset-author
Enter fullscreen mode Exit fullscreen mode

How to rename the git branch

To rename git local and remote branch

  • Rename your local branch and push to remote
git branch -m old_branch_name  new_branch_name
git push --set-upstream origin new_branch_name
Enter fullscreen mode Exit fullscreen mode
  • Delete the old remote branch and push new branch name
git push origin :old_branch_name  new_branch_name
Enter fullscreen mode Exit fullscreen mode

How do I branch a specific commit

You can create the branch via a hash:

git branch branchname <sha1-of-commit>
Enter fullscreen mode Exit fullscreen mode

To checkout the branch when creating it, use

git checkout -b branchname <sha1-of-commit or HEAD~3>
Enter fullscreen mode Exit fullscreen mode

How to reset the git password in windows

Some times checkout works but failed to push, we might get authentication failed without user name password challenging, in that case we have to clear our local windows credentials

In windows 10 you can find Windows Credentials at :

Control Panel\User Accounts\Credential Manager

Control Panel\All Control Panel Items\Credential Manager --> Windows Credentials

for your git server and then you can update password by clicking edit button.

Windows Credentials

How to reset a git branch

How to reset a local commit

Remove all the local commit and reset to the original remote branch

git reset --hard origin/master
Enter fullscreen mode Exit fullscreen mode

How to resolve merge conflicts

One of the challenging task is resolving merge conflicts on Pull Request. Below command help you to merge with theirs strategy

Merge on theirs

Step 1:

git checkout branch_A
git pull origin branch_B -X theirs
Enter fullscreen mode Exit fullscreen mode

Step 2: After the merge conflicts are resolved, stage the changes accordingly, commit the changes and push.

git commit
git push origin HEAD
or
git push origin origin/branch_A
Enter fullscreen mode Exit fullscreen mode

Step 3: The pull request will be updated and marked as merged.

How to view git stash files

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

git stash list --name-status
git stash list --name-only
Enter fullscreen mode Exit fullscreen mode

To view only the file name on a particular stash

git stash show --name-only -p stash@{3}
git stash show --name-status -p stash@{3}
Enter fullscreen mode Exit fullscreen mode

How to git stash specific files under a path

 git stash push -m "application conf" src/main/resources/config/
Enter fullscreen mode Exit fullscreen mode

How to revert merge commit

 git revert -m 1 <commit-hash> 
 where commit-hash is the merge commit id
 git push -u origin master 
Enter fullscreen mode Exit fullscreen mode

How to pick a commit from one branch to another using cherry-pick

Some times we need to copy a particular commit into another branch.
we can use cherry-pick

-x

When recording the commit, append a line that says "(cherry picked from commit …​)" to the original commit message (e.g. backporting a fix to a maintenance branch for an older release from a development branch), adding this information can be useful.

git checkout <branch you want to apply> or git checkout release_branch
git cherry-pick <commit hash> or git cherry-pick 3a756d18a21
or
 git cherry-pick -x 3a756d18a21

Enter fullscreen mode Exit fullscreen mode

How to fix or solve No url found for submodule path in .gitmodules

While working on submodules sometimes challenging , I initially add submoudles to different path and changed. While working on feature branch which is originally forked from initial submoudles. After merging in to develop branch showed bellow error

fatal: No url found for submodule path 'gradle' in .gitmodules

Execute the below command to check the index

git ls-files --stage | grep 160000
Enter fullscreen mode Exit fullscreen mode

In my case this gives the output

160000 c33757f1ac59f1728cca17f6a1d999704fcbcaaf      0      gradle

160000 c33757f1ac59f1728cca17f6a1d999704fcbcaaf      0      sci-bas-root/gradle

Then later I deleted the problamatic submodules, in this case gradle

$git rm --cached path-to-submodule

Hence i executed this command

git rm --cached gradle
git push
Enter fullscreen mode Exit fullscreen mode

How to pass custom ssh private key filename

When cloning and working on multiple repository from different accounts, maintaing the separate ssh file is apt way. The below will show you how to pass private key while cloning and override the default file id_rsa

 git clone git@github.com:vidhya03/my-useful-commands.git --config core.sshCommand="ssh -i /v/tools/putty/keys/vidhya03-github_rsa"
Enter fullscreen mode Exit fullscreen mode

or edit the .git/config file in the root directory and add the command sshcommand = ssh -i /v/tools/putty/keys/vidhya03-github_rsa under [core]

[core]
    sshcommand = ssh -i /v/tools/putty/keys/vidhya03-github_rsa
Enter fullscreen mode Exit fullscreen mode

How to mirror a git repository

Sometimes we wanted to move one repository to another repository

git clone --mirror http://repo/myrepo.git
cd myrepo.git/
git remote set-url --push origin https://github.com/myorg/myrepo.git
git fetch -p origin
git push –mirror
Enter fullscreen mode Exit fullscreen mode

How to add new line in shell - git commit message

In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this:

git commit -m ' 🐛 Fixed runtime issue

When connecting an api with multiple calls , Commit message body goes here


refer: footer label goes here
'
Enter fullscreen mode Exit fullscreen mode

How to do a empty commit

How to do a git empty commit

git commit --allow-empty
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .