Git Cheat Sheet

6 December 2022 Link



Terminology

  • HEAD is not the latest revision, it's the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be.
  • master is a name commonly given to the main branch, but it could be called anything else (or there could be no main branch).
  • origin is a name commonly given to the main remote. remote is another repository that you can pull from and push to. Usually it's on some server, like github.

Good GUI Software

  • Git Extensions - Very good for bash and windows explorer integration
  • SourceTree - Excellent GUI to manage the whole repository and look at it visually. Better visual representation by default then git extensions since gives the exact date and time.

Updating a submodule

  • Open a git bash at the sub-module directory then type:
cd submodule_name
git checkout master && git pull
cd ..
git add submodule_name
git commit -m "updating submodule to latest"
git push

Informational Commands

  • Repository Status
git status
  • Commit History
git log
  • Get current branch name
git branch

Checkin Commands

  • Push code to a remote at a particular branch
git push -u <remote name> <branch name>
-u option points your current local branch to the remote branch named
  • Stage files
    • Stages all
git add -A
    • Stages new and modified, without deleted
git add . 
    • stages modified and deleted, without new
git add -u 
  • Commit
git commit -m "Message"
  • Aborting a merge
git merge --abort
  • Discard local changes
git reset --hard
  • Stash Local changes and drop them
git stash save --keep-index (--include-untracked)
git stash drop

Checkout

  • Fetch all information about new branches etc. from remote
git fetch <remote name>
  • Pull the branch data from remote
git pull <remote name> <branch name>

Branching

  • Switch to existing branch
git checkout <branch name>
  • Switch to new branch on the current workspace i.e. branch off
git checkout -b <new branch name>
  • Push current branch to remote and track it
git push -u origin <branch name>

Remotes

  • List Remotes
git remote -v
  • Change Remote URL
git remote set-url <remote name> <url>
  • Change Remote name
git remote rename <old name> <new name>
  • Remove a remote
git remote rm <remote name>
  • Add a remote
git remote add <remote name> <url>

Tutorial and Help


Issues

Unsafe repository

After upgrading GIT started receiving unsafe repository - not the owner error whenever I tried running pull on the previous repository folders. So need to change the owner to myself. In the folder properties dialog I don't see the security tab. So needed to enable it. For that needed the Group Policy Editor. Windows Home edition does not have it so had to install it:
  1. Open cmd as adiministrator.
  2. Run a batch file with the following contents:
@echo off
 pushd "%~dp0"
 dir /b %SystemRoot%\servicing\Packages\Microsoft-Windows-GroupPolicy-ClientExtensions-Package~3*.mum >List.txt
 dir /b %SystemRoot%\servicing\Packages\Microsoft-Windows-GroupPolicy-ClientTools-Package~3*.mum >>List.txt
 for /f %%i in ('findstr /i . List.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
 pause
Now Group Policy editor is installed. Next Steps are:
  1. Open it with windows+R key and type gpedit.msc.
  2. Now navigate to: User Configuration -> Administrative Templates -> Windows Components -> File Explorer On the right hand side, double-click the “Remove Security tab” setting. See here.
  3. Select disabled and the policy setting will take effect immediately.

This also did not bring the security tab. Realized that my repository is in my USB drive formatted with the FAT filesystem. So could not figure out how to change the owner.
Finally just did this from the git bash:
git config --global --add safe.directory 'Path/to/repository'
For example:
> git config --global --add safe.directory 'E:/Milind/My Work/My Programs/C_C++/luasql/github'

Reference