Working with Git command line

Working with Git CLI 

Core concepts:

  1. git config --global user.name=‘isharma’
  2. git config --global user.email='xyz@abc.com’
  3. git config  --list 
  4. git status [ Check the current status ] 
  5. Git remote add origin [Url to empty git branch on Remote] [This will basically create repo from local directory]
  6. Git steps: Working Area, Staging Area, History, Git Server
  7. git branch -r [ Check all the branches ]
  8. git branch -b ‘branch-name’ [create new local branch]
  9. Git add . [move changes to Staging Area from the working area]
  10. Git diff [Difference between changes in the working area to staging area]
  11. Git commit -m ‘msg’ [move changes from staging area to git history]
  12. Git diff  --staged [difference between changes in the staging area to git history]
  13. Git push origin master: master [move changes from history to git server]
  14. Git reset HEAD ‘added files’ [Revert the changes done to history  area]
  15. Git checkout  -- ‘committed files’ [Revert the changes done to the staging area: clean working directory]
  16. Git log --  ‘committed file’
  17. Git rm ‘added/committed files’  [Remove files from working directory, staging Area and history area]
  18. Git checkout ‘committed hash’ -- ‘Files to be restored from earlier commit’ [Restore changes into working directory and staging area from past commits]
  19. git log  --all --decorate --oneline --graph [All the commits History ] 
  20. Git -a -m ‘msg’ [Add the changes & commit simultaneously]
  21. Git log -p [print the lines changed along with commit hashes]
  22. Create new local branch & push code to remote in the same branch:  git checkout -b feature-branch; git push -u origin feature-branch

Branching and Merging:

  1. HEAD points to the latest Synced branches 
  2. Git diff master..release/5.0.2 [Difference between two branches]
  3. Fast Forward Merge[When there is a direct path from master to branch]
    1. Git merge release/5.0.2 [merge into master branch]
    2. Git branch  --merged [shows the merged branch into Master] 
    3. Git branch -d release/5.0.2 [Delete Merged Branch]
  4. 3-Way merge [ When there is no direct path from branch to master]
    1. Git merge release/5.0.2 [Merge made by recursive merges; no Manual interventions required]
  5. Merge Conflicts [when the same file is changed in Both Branches]
    1. Git merge release/5.0.2 [Merging will Fail due to conflicts]
    2. Next abort the merge: git merge --abort
    3. Git merge release/5.0.2 
    4. Resolve the conflicts & add/commit changes
  6. Detached HEAD Resolution [ When the HEAD is pointing to Non-branch commits ]
    1. Put new branch label: git branch stage 
    2. Git checkout stage [HEAD will point to stage branch]
  7. Git Stash [when checkouts fail we need to have the working directory clean]
    1. Git checkout stage [Throws error]
    2. Git stash [Git working directory clean]
    3. Git stash list [ List all the stashes]
    4. Git stash apply [Apply the latest stash]
    5. Or, git stash apply [Label from point-3]
    6. Or, git stash pop [ This will delete the latest stash]


Git Remotes:

  1. Git remote -v [ To see Fetch & Push Origin ]
  2. If there are changes in the remote branch by others, then run : 
    1. git fetch origin
    2. Git merge origin/master [ Local master Branch is now in sync with Origin Master Branch]
    3. Alternatively, you can run git pull instead of fetching & merge
  3. Forking [If the original branch doesn’t have permission]
    1. Git clone [Forked branch url]
    2. To keep sync up with Original branch :
      1. Git remote add upstream [original branch url]
      2. Git remote remove upstream [in case u need to delete]
      3. Git fetch upstream [To get the changes of the original branch in forked] — All in sync
      4. If there are changes in the original branch, the forked branch doesn’t know the changes
        1. So run: git fetch upstream [ Out of sync with forked]
        2. Git merge upstream/master
        3. Git push origin master [ Now all the repository in sync ] 
      5. If forked branch wants to make changes & wants to merge to original :
        1. Git checkout -b “new-branch”
        2. Make commits
        3. Git push origin new-branch
        4. Open the pull request from UI

Git SubModules:

  1. Add submodule to Repos Demo: 
    1. git clone [Demo git URL] 
    2. git submodule add -b [git url]
    3. Git add . ; git commit -m ‘adding submodule’; git push origin master
  2. Clone the repository with submodule: git clone --recursive [git URL --jobs 2 [No. Of submodules]

Comments