Git Essentials

Git Introduction


  • Version control system falls into two categories:

    • Centralized (Subversion, Team foundation server)

    • Distributed (Git, Mercurial)


  • Using Git:

    • The command line

    • Code editors and IDE (VSCode, GitLens)

    • Graphical user interface (GUI Client, GitKraken, Sourcetree)



  • Configuring Git: We need to specify four settings while configuring git…

    • Name

    • Email

    • Default Editor

    • Line Ending


  • There are three levels where we can add this settings

    • System - All users

    • Global - All repositories of the current user

    • Local - The current repository


  • Settings command:

    • Git config --global user.name “Monowarul Islam”

    • Git config --global user.email abc@gmail.com

    • Git config --global core.editor “code --wait”

    • Git config --global core.autocrlf true   (For windows set true, For mac/linux set input)

    • git config --global pull.rebase true  

      (git pull always makes rebase. So there will be not merge commit)

    • git config --global mergetool.keepBackup false

      (After performing a merge, the original file with conflict markers can be saved as a file with a .orig extension. Setting keepBackup value false will not save)


  • To see the settings into default editor

    • Git config --global -e


  • To set default editor as Notepad++

    • git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"


  • Plugin for VSCode: GitLens, Git Graph

  • Diff Tools: Kdiff3, P4Merge, WinMerge, VSCode 


  • Add default diff tool as P4Merge:

    • Git config - -global merge.tool p4merge

    • Git config - -global mergetool.p4merge.path “C:\Program Files\Perforce\p4merge.exe”

    • Git config –global mergetool.keepBackup false


  • Add default editor as VSCode:

    • git config --global core.editor 'code --wait'

    • git config --global diff.tool vscode

    • git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

    • git config --global merge.tool vscode

    • git config --global mergetool.vscode.cmd 'code --wait $MERGED'





Git Commands

    • Check git version: git  --version

    • Get the git help
      Git config --help (Details help same as web documentation)
      Git config --h (short summary of the command)

    • Initialize empty git repository: git init

    • To See the staged/committed all files: git ls-files

    • Get short status: git status -s

    • Log in one line: git log - -oneline - -all - -graph

    • Finding contributors: git shortlog -n -s -e

    • To remove the last commit: git reset –hard HEAD~1
      (Actually the last commit is not deleted, just break the pointer.If you know the commit address then you can add it again)

    • To revert the merged commit: git revert -m 1 HEAD
      (There would be a new revert commit by reverting the actual merge. Both history is found at log)

    • Continue rebase after conflict resolution:
      Git rebase master
      Git rebase –continue  (After solving conflict)
      Git rebase –skip  (To skip the rebase process)
      Git rebase –abort ()

    • Picking a file from another branch:
      Git restore –source=feature/send-email toc.txt

    • Cloning a repository
      Git clone https://github.com/monowar123/xyz.git xyz

    • Details of remote repository
      Git remote
      Git remote -v

    • Diff between local and remote tracking branch:
      Git branch -vv

    • Git pull:
      Git pull (Fetch + Merge, It includes merge commit)
      Git pull –rebase (Fetch + Merge, and then rebase) (Means the local commit is above the remote)

    • Create local branch to map with remote one:
      Git switch -C feature/change-password origin/feature/change-password

    • Update with deleted remote branch
      Git remote prune origin

    • Remove the untrack file from local directory:
      Git clean -fd

    • Cherry-Pick commit while accepting others changes  git cherry-pick --strategy=recursive -X theirs a11c256872e

    • Partial clone (Checkout only specific folder) git clone --sparse https://github.com/cirosantilli/test-git-partial-clone.git PartialClone cd PartialClone git sparse-checkout set d1

    • Modify last pushed commit git commit --amend (It will open the last commit at default editor. Modify and save)

      git push --force-with-lease  (Ensures do not overwrite someone else work by force pushing)

                  








Comments