My Git Useful Commands
Useful git commands 💕
Table of contents
- How to see my last commit
- How to output git log with the first line only?
- How can i view a git log of just one user
- How do I force git pull to overwire local files
- How do I determine the url of git repository
- How do I go to specific revision
- How do I tag a specific commit
- How do I delete a specific branch
- How to change author in a commit
- How to rename the git branch
- How do I branch a specific commit
- How to reset the git password in windows
- How to reset a git branch
- How to reset a local commit
- How to resolve merge conflicts
- How to view git stash files
- How to git stash specific files under a path
- How to revert merge commit
- How to pick a commit from one branch to another using cherry-pick
- How to fix or solve No url found for submodule path in .gitmodules
- How to pass custom ssh private key filename
- How to add new line in shell - git commit message
How to see my last commit
This command is useful to see ‘my last commit’
|
|
How to output git log with the first line only
This command is useful to print commit history by one line
|
|
How can i view a git log of just one user
This command is useful to see log by particular user
|
|
will match a commit made by “Vidhyadharan Deivamani” and also
|
|
How do I force git pull to overwire local files
This command is useful when you got git < branch > | MERGING issues
|
|
or
|
|
Note: the above command will overwrite your local files
How do I determine the url of git repository
Get only the remote url
|
|
or
Get the entire url
|
|
How do I go to specific revision
Move to a specific commit or checkout to previous revision
First note the sha or revision number
|
|
Then
Check out the specific commit
|
|
How do I tag a specific commit
Tag a specific commit
First note the sha or revision number
|
|
Then
Tag with revision number
|
|
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.
|
|
To push a single tag:
|
|
And the following command should push all tags (not recommended):
|
|
How do i delete a specific branch
To delete the git remote branch
|
|
To delete the local branch
|
|
How to change author in a commit
Sometime working in opensource and closed source, you might commit closed source with your personal email id.
|
|
|
|
How to rename the git branch
To rename git local and remote branch
- Rename your local branch and push to remote
|
|
- Delete the old remote branch and push new branch name
|
|
How do I branch a specific commit
You can create the branch via a hash:
|
|
To checkout the branch when creating it, use
|
|
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.
How to reset a git branch
How to reset a local commit
Remove all the local commit and reset to the original remote branch
|
|
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:
|
|
Step 2: After the merge conflicts are resolved, stage the changes accordingly, commit the changes and push.
|
|
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.
|
|
To view only the file name on a particular stash
|
|
How to git stash specific files under a path
|
|
How to revert merge commit
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
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]
|
|
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:
|
|