Contents

My Git Useful Commands

Useful git commands 💕

Table of contents

How to see my last commit

This command is useful to see ‘my last commit’

1
git log --name-status HEAD^..HEAD

How to output git log with the first line only

This command is useful to print commit history by one line

1
git log --pretty=oneline --abbrev-commit

How can i view a git log of just one user

This command is useful to see log by particular user

1
git log --author="vidhya"

will match a commit made by “Vidhyadharan Deivamani” and also

1
git log --author=deiva

credits

How do I force git pull to overwire local files

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

1
2
git fetch --all
git reset --hard origin/master

or

1
git reset --hard origin/<branch_name>

Note: the above command will overwrite your local files

credits

How do I determine the url of git repository

Get only the remote url

1
git config --get remote.origin.url

or

Get the entire url

1
 git remote show origin

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

1
git log --pretty=oneline --abbrev-commit

Then

Check out the specific commit

1
 git checkout <sha1 or revision>

credits

How do I tag a specific commit

Tag a specific commit

First note the sha or revision number

1
git log --pretty=oneline

Then

Tag with revision number

1
 git tag -a v1.2 444554738120233382c5912ebbca32592ba765ad -m "Message here"

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.

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

To push a single tag:

1
git push origin <tag_name>

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

1
git push --tags

credits

How do i delete a specific branch

To delete the git remote branch

1
git push --delete origin <branch_name>

To delete the local branch

1
git branch -D <branch_name>

How to change author in a commit

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

1
git commit --amend --author="vidhyadharan deivamani <it.vidhyadharan@gmail.com>" -m "commit description"
1
git commit --amend --reset-author

How to rename the git branch

To rename git local and remote branch

  • Rename your local branch and push to remote
1
2
git branch -m old_branch_name  new_branch_name
git push --set-upstream origin new_branch_name
  • Delete the old remote branch and push new branch name
1
git push origin :old_branch_name  new_branch_name

How do I branch a specific commit

You can create the branch via a hash:

1
git branch branchname <sha1-of-commit>

To checkout the branch when creating it, use

1
git checkout -b branchname <sha1-of-commit or HEAD~3>

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.

/posts/my-git-useful-commands/windows.credentials.png

How to reset a git branch

How to reset a local commit

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

1
git reset --hard origin/master

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:

1
2
git checkout branch_A
git pull origin branch_B -X theirs

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

1
2
3
4
git commit
git push origin HEAD
or
git push origin origin/branch_A

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.

1
2
git stash list --name-status
git stash list --name-only

To view only the file name on a particular stash

1
2
git stash show --name-only -p stash@{3}
git stash show --name-status -p stash@{3}

How to git stash specific files under a path

1
 git stash push -m "application conf" src/main/resources/config/

How to revert merge commit

1
2
3
 git revert -m 1 <commit-hash> 
 where commit-hash is the merge commit id
 git push -u origin master 

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.

1
2
3
4
5
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

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

1
git ls-files --stage | grep 160000

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

1
2
git rm --cached gradle
git push

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

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

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]

1
2
[core]
	sshcommand = ssh -i /v/tools/putty/keys/vidhya03-github_rsa

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

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:

1
2
3
4
5
6
7
git commit -m ' 🐛 Fixed runtime issue

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


refer: footer label goes here
'

Try https://user-images.githubusercontent.com/534679/120312502-6b20ff00-c2f6-11eb-8414-fb6a5676eee0.png