Tuesday, October 1, 2019

GIT : Commands and Operation

 Commands and Operation



Remote Master-------------------------------------Remote branch
|                                                                         |
|                                                                         |
|                                                                         |
|                                                                         |
Local Master ---------------------------------------Local branch


Usual Operations :

  1. Usually project will be made available in Git
  2. User clones the repository into local (once you clone master you get all the branches in the repository)
  3. creates a story branch locally if not available / if available checkout that branch
  4. Works on development work  
  5. commits the Changes to local branch
  6. Pushes the changes to remote branch
  7. Then the code pushed code is merged to master in remote.

Scenario 1: Developer wants to use the new code his colleague merged recently to master.
  1. Saves current changes in his current branch
  2. Developer switches to local master
  3. Perform PULL request to update his local master with latest changes.
  4. checkout the story branch
  5. Merges the updated the master to local branch
Scenario 2: Developer completed his coding while merging his code to remote master , he faces merge conflicts.
  1. checkout master
  2. pull all the changes from remote master to local master
  3. checkout story branch
  4. perform merge 
  5. Solve all the conflicts 
    1. Git shows "AUTO CONFLICT merge xxxx"
    2. Files turn red (Eg :intelliJ)
    3. rt ck > Git > resolve conflicts
    4. click Merge button
    5. Window with 3 screens open : middle screen - Current code , left and right screen shows the code in local staging and code in master
    6. Make required selection
    7. Save
  6. Commit changes
  7. Push to story branch
  8. Raise PR (merge to master) 
Sample Code :
git checkout master
git pull
git checkout my_branch
git merge master
(now use above conflict resolution to solve the issue )

Clone-Remote Master into local:
Usually any user will either clone the remote repository into his computer or create a local master repository.
git clone git_ssh_link #clone from git
git init #new repository without cloning

Check status: 
git branch

Result:
*master

Create / Use already existing branch 
once user has cloned / create a local repository
git checkout - b branch_new #Create a new branch in local and switch
git  branch branch_new #Just create a new branch in local

Message display / status
git branch #display all branches
git branch -vv #display all branches with their upstream branch
git status #display status of project wrt git
git log

Change branch 
git checkout branch_new #switch branch

Commit changes to staging whenever developer has reached a checkpoint
git add file #add file to local staging
git commit -m "description" #commit added file in staging

Push changes to story branch in remote (cloud)
git add file #add file to local staging
git commit -m "description" #commit added file in staging
git push -u origin branch_new #Create new story branch in remote and put changes there

Update the current branch if new changes are available in remote

git pull origin branch_name #1st time only
git pull #get latest updates from remote branch whichever is made upstream else same branch which we are currenlty

Remove the file from local git
git rm -rf file_name #delete file from local git

Reset
git checkout file #reset changes of the current file
git reset #save and reset changes of all the files to last commit
git reset --hard #reset to last commit without commiting

Delete branch
git branch -D branch-name

To Create a upstream
git branch -u masterbranchname # from where u want to pull latest code


Example to create a new project locally and push it to remote :
  • git init
  • git add README.md
  • git commit -m "first commit"
  • git remote add origin git@github.xxx.com:username/scala_protobuf.git
  • git push -u origin master

…or push an existing repository from the command line

  • git remote add origin git@github.xxx.com:username/scala_protobuf.git
  • git push -u origin master

Git Remove File from Face of the earth (all history included)
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
git push --all

Git delete remote branch
git push origin --delete branch_name