7 Rules of Git Commit Log

The seven commonly accepted rules on how to write a git commit message are:

  1. Limit the subject line to 50 characters.
  2. Capitalize only the first letter in the subject line.
  3. Don’t put a period at the end of the subject line.
  4. Put a blank line between the subject line and the body.
  5. Wrap the body at 72 characters.
  6. Use the imperative mood.
  7. Describe what was done and why, but not how.

Reference


How to Pick a Commit of Git Submodule?

Current State

There is a new submodule ‘test’ and we want to add it to the repo.

Steps

Git add submodule

$ cd my_repo
$ git submodule add git@github.com:vishalkanaujia/dev-tools.git

Initialize the repo

$ git submodule update --init --recursive

cd to the submodule

$ cd dev-tools

By default, the latest version is picked. We can checkout to a commit for moving a different version.

$ git checkout abdcca6

Written with StackEdit.


How to use Gist with Stackedit for WordPress blog?

How to use Gist with Stackedit for WordPress blog?

  • Create the gist and copy the git number
    An example is https://gist.github.com/vishalkanaujia/c7d692de9a3b337e9ad8e655533b9856
    The gist id is the last string of the address:
    “c7d692de9a3b337e9ad8e655533b9856”
  • In the stackedit source (markdown file), add the following code:
    [g i s t]c7d692de9a3b337e9ad8e655533b9856[/g i s t]
    Please type as “gist”
  • WordPress will automatically interpret the command and pull your gist.

Reference

Written with StackEdit.


Git Cheat Sheet

  • Describe a change number
    $ git show dc54076

  • Reset the current branch with HEAD of master

    $ git fetch origin
    $ git reset --hard origin/master
    
  • Keep your branch in sync with origina master branch
    $ git pull origin master

  • Arrange all changes in order. Fixes the log too.
    $ git rebase origin wip_my_work

  • Squash commits in one commit. 2 represents number of commits to review.
    $ git rebase -i HEAD~2

  • Compare two branches
    $ git diff master..my_branch

  • Modify a commit message
    $ git commit --amend

  • Find diff of local file against remote master

    $ git fetch origin master
    $ git diff origin/master -- [local-path]
    
  • Elaborate comment amend techniques

  • Stash current commits

    $ git stash
    $ git stash list
    $ git stash apply stash@{0}
    
  • Recover/ revert a file to original/master copy

    # For a committed file
    $ git checkout origin/master filename  
    
    # For a local, uncommitted file 
    $ git checkout -- filename
    
  • Delete a local branch

    $ git branch -d branch_name
    $ git branch -D branch_name
    
  • Squash local commits
    $git reset --soft HEAD~3 && git commit

  • Delete files from staging

    $ git reset file_name
    
  • Pull master’s changes to your local branch
    $git checkout my_branch
    $git rebase master

  • Push your local branch changes to master branch
    $git checkout my_branch
    $git merge master

  • Alias to add and commit a changed file

    $ git config --global alias.c "!git add -A && git commit -m"
    

Reference


git push error “remote: Invalid username or password. fatal: Authentication failed”

I have 2-factor authentication enabled on my account. The repo is my own and I was unable to push changes to a private branch.

Steps to resolve

1. Check your remote of repo

$ git remote -v
origin https://github.com/vishalkanaujia/gnome-connection-manager-1.git (fetch)
origin https://github.com/vishalkanaujia/gnome-connection-manager-1.git (push)

Looked carefully, I was trying to push using https.

2. I updated local system’s SSH keys to my github account.

3. Next, I changed the remote to SSH.

# Remove original origin
$ git remote remove origin 

# Added SSH origin
$ git remote add origin git@github.com:vishalkanaujia/gnome-connection-manager-1.git

#Pushed changes
$ git push origin fix_shortcuts
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 536 bytes | 536.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: 
remote: Create a pull request for 'fix_shortcuts' on GitHub by visiting:
remote: https://github.com/vishalkanaujia/gnome-connection-manager-1/pull/new/fix_shortcuts
remote: 
To github.com:vishalkanaujia/gnome-connection-manager-1.git
* [new branch] fix_shortcuts -> fix_shortcuts