Skip to main content
Working with Git command line
Working with Git CLI
Core concepts:
- git config --global user.name=‘isharma’
- git config --global user.email='xyz@abc.com’
- git config --list
- git status [ Check the current status ]
- Git remote add origin [Url to empty git branch on Remote] [This will basically create repo from local directory]
- Git steps: Working Area, Staging Area, History, Git Server
- git branch -r [ Check all the branches ]
- git branch -b ‘branch-name’ [create new local branch]
- Git add . [move changes to Staging Area from the working area]
- Git diff [Difference between changes in the working area to staging area]
- Git commit -m ‘msg’ [move changes from staging area to git history]
- Git diff --staged [difference between changes in the staging area to git history]
- Git push origin master: master [move changes from history to git server]
- Git reset HEAD ‘added files’ [Revert the changes done to history area]
- Git checkout -- ‘committed files’ [Revert the changes done to the staging area: clean working directory]
- Git log -- ‘committed file’
- Git rm ‘added/committed files’ [Remove files from working directory, staging Area and history area]
- Git checkout ‘committed hash’ -- ‘Files to be restored from earlier commit’ [Restore changes into working directory and staging area from past commits]
- git log --all --decorate --oneline --graph [All the commits History ]
- Git -a -m ‘msg’ [Add the changes & commit simultaneously]
- Git log -p [print the lines changed along with commit hashes]
- 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:
- HEAD points to the latest Synced branches
- Git diff master..release/5.0.2 [Difference between two branches]
- Fast Forward Merge[When there is a direct path from master to branch]
- Git merge release/5.0.2 [merge into master branch]
- Git branch --merged [shows the merged branch into Master]
- Git branch -d release/5.0.2 [Delete Merged Branch]
- 3-Way merge [ When there is no direct path from branch to master]
- Git merge release/5.0.2 [Merge made by recursive merges; no Manual interventions required]
- Merge Conflicts [when the same file is changed in Both Branches]
- Git merge release/5.0.2 [Merging will Fail due to conflicts]
- Next abort the merge: git merge --abort
- Git merge release/5.0.2
- Resolve the conflicts & add/commit changes
- Detached HEAD Resolution [ When the HEAD is pointing to Non-branch commits ]
- Put new branch label: git branch stage
- Git checkout stage [HEAD will point to stage branch]
- Git Stash [when checkouts fail we need to have the working directory clean]
- Git checkout stage [Throws error]
- Git stash [Git working directory clean]
- Git stash list [ List all the stashes]
- Git stash apply [Apply the latest stash]
- Or, git stash apply [Label from point-3]
- Or, git stash pop [ This will delete the latest stash]
Git Remotes:
- Git remote -v [ To see Fetch & Push Origin ]
- If there are changes in the remote branch by others, then run :
- git fetch origin
- Git merge origin/master [ Local master Branch is now in sync with Origin Master Branch]
- Alternatively, you can run git pull instead of fetching & merge
- Forking [If the original branch doesn’t have permission]
- Git clone [Forked branch url]
- To keep sync up with Original branch :
- Git remote add upstream [original branch url]
- Git remote remove upstream [in case u need to delete]
- Git fetch upstream [To get the changes of the original branch in forked] — All in sync
- If there are changes in the original branch, the forked branch doesn’t know the changes
- So run: git fetch upstream [ Out of sync with forked]
- Git merge upstream/master
- Git push origin master [ Now all the repository in sync ]
- If forked branch wants to make changes & wants to merge to original :
- Git checkout -b “new-branch”
- Make commits
- Git push origin new-branch
- Open the pull request from UI
Git SubModules:
- Add submodule to Repos Demo:
- git clone [Demo git URL]
- git submodule add -b [git url]
- Git add . ; git commit -m ‘adding submodule’; git push origin master
- Clone the repository with submodule: git clone --recursive [git URL --jobs 2 [No. Of submodules]
Comments