Git Cheatsheet

Git for Github:

See the diff between local and remote:

git diff --stat --color remotes/main/master..origin/master

Create the branch on your local machine and switch in this branch :

$ git checkout -b [name_of_your_new_branch]

Push the branch on github :

$ git push origin [name_of_your_new_branch]

When you want to commit something in your branch, be sure to be in your branch.

You can see all branches created by using :

$ git branch

Which will show :

* approval_messages
  master
  master_clean

Add a new remote for your branch :

$ git remote add origin [name_of_your_remote]

Push changes from your commit into your branch :

$ git push origin [name_of_your_remote]

Update your branch when the original branch from official repository has been updated :

$ git fetch [name_of_your_remote]

Then you need to apply to merge changes, if your branch is derivated from develop you need to do :

$ git merge [name_of_your_remote]/develop

Working with Branch

Switching Branch:

Check out branch

$ git checkout -b [branch name]

Switch Tracking

$ git branch -u origin/[branch name]

Switch Branch

$ git checkout [branch name]

Deleting Branch

Delete a branch on your local filesystem :

$ git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

$ git branch -D [name_of_your_new_branch]

Delete the branch on github :

$ git push origin :[name_of_your_new_branch]

Recover a File that you deleted that you have not committed:

$ git checkout {file path}

How to setup Commit-Id in GIT

GIT Errors

  • git -> Fatal: bad Object Head
  • Resolution – do git pull – following is an example
    $ git pull origin master

Git Revert Changes

  • git reset --hard $commit-hash
    git push -f $remote $local_branch$:$remote_branch$
    git push $local_branch$ $remote_branch$ --force
    

Git Clone Error in Windows:

Following error occurs when doing git clone. This error is due to that you did a copy and paste and it has extra characters. If you type in the whole command it will work.

git clone ssh://git@.......

Cloning into 'xxx projectnam'...
fatal: protocol 'https' is not supported

GIT trace settings

export GIT_TRACE_PACKET=1
export GIT_TRACE=1 
export GIT_CURL_VERBOSE=1

References:

  1. https://git.eclipse.org/r/Documentation/user-changeid.html
  2. https://gerrit.googlecode.com/svn/documentation/2.0/cmd-hook-commit-msg.html
  3. https://wiki.opendaylight.org/view/GIT_Cheat_Sheet