Introduction
This article helps to understand and setup the GIT. A free and Open Source Distributed Version Control which is used for Source Code Management. It helps to Edit, Create, Save, Update your Project/Content/file and keep track of entire Change History.
GIT Audience
GIT can be widely used by any knowledge worker like, Developer, DevOps Engineer, Automation Engineer, Content/Technical Writer.
GIT Architecture
Git is distributed version control system. Distributed means each end user can create their own working branch and after completion of task, they can merge the Changes to Master Branch. This gives lots of flexibility to the developer to work on their own feature without impacting others.
GIT Branching
In GIT we only keep the single Active branch "Master" for complete workflow. Feature or any working branch can be created from the Master and after finishing the task the changes will be merge back to Master with Pull Request. We will see this under the Demo.
In the above example, lets a assume one feature assigned to Single developer or a specific team. They can easily branch out from the master to Feature1 and after completion of task they can merge back changes to the Master branch. Similarly for Feature2. CI/CD will be implemented through Master Branch.
GIT Demo
Now Lets understand after creating the Branch how we can create Local working copy, commit the change and merge back to the Master Branch.
In GIT there will always be two setup. One is Remote System and another is your Local System. Local system will be defined as your working copy.
Now lets setup GIT on local system and see the complete workflow of creating the Local Copy, making the change and Merging Back to the Master Branch.
Git Setup
Install GIT on your local machine. Based on your operating System you can click on below URL.
Step 1: Download and Install Git on your local system.
Since I am using Windows I will choose Git for Windows and Install it.
Step 2: Check Installed GIT version.
Open GIT Command Prompt as Admin and Run below command.
git version
Git Change Workflow with Demo
Now assume, I have assigned to work on Profile Feature. In this case first I will branch out from Master to create feature/profile branch. Next I will use below workflow, starting from cloning the repository to merge the changes to Master Branch using Pull Request.
Create Feature Branch
Step 1: Login to the respective GIT project repository and create Branch from Main/Master.
Clone the Repository to local system
Step 1: Since I am using the GIT Repository from the Azure DevOps. I will select the respective Repository and get the Clone URL.
If you want to learn Azure DevOps. You can follow below article. Beginner's Complete Guide to Azure DevOps
Step 2: Create working folder on your local system and get the Clone URL from the Repos.
Step 3: Use below command and clone the Repository
git clone https://test0103@dev.azure.com/test0103/demo/_git/demo
Clone with Credential: If your windows account is not authorized you can use the git clone with Credential to clone the repository.
git clone https://username:password@github.com/username/repository.git
Step 4: Now after cloning you can see all the Files and Folder will be copied over to local system.
Create Local Branch
Step 1: Let's reinitialize the cloned repository.
git init
Step 2: As per the requirement I will be working on the feature/profile. So in this case I will create same branch locally.
git checkout -b feature/profile
Now Currently the git is pointing to the feature/profile branch
Make the change
Step 1: I will further work on my changes and Save it. In this case lets assume I have created one additional file "profile.cs".
Now to check the change run git status
git status
Step 2: Here you can see we are at branch feature/profile and one untracked file "profile.cs".
Stage/Index the change
Staging is nothing but collection of changes which are ready to commit.
Step 1: Stage the changes using below command
git add .
Step 2: After staged now lets check the status. Now we can see we are on branch feature/profile and change is ready to commit.
git status
Commit the change
Step 1: Now after the stage we are ready to commit.
Note: After the commit the changes will be still at the local system.
git commit -m "profile feature added"
Push the Changes to Feature Branch
To follow the best practice before pushing to the master lets first push the changes to the feature/profile branch.
Step 1: First check the existing remote branch.
git branch -r
Step 2: Now as per the result there are multiple branch and and we want to push the commit to feature/profile branch.
git push --set-upstream origin feature/profile
Now we can see the profile.cs file is pushed to feature/profile Branch
Create Pull Request
To merge the changes to Master/Main Branch we need to follow the Review process and Merge the changes.
Step 1: We will go to the Azure DevOps and now I will Create a pull request [PR].
Step 2: We need to select the target branch, Reviewers and might be the Work Item. Click on Create.
Approve Pull Request and Merge
Once PR created, notification will be sent to the reviewer. Reviewer can be your Lead/Manager/TeamMember.
Step 1: Check the Review and if it approved by everyone proceed with Merge else as per the comment re-work and follow the same procedure.
Step 2: Complete the Merge
Important GIT Commands
Clone branch
git clone "Repo URL"
Clone branch with credential
git clone https://username:password@github.com/username/repository.git
Stage/Index Changes
git add .
Commit Changes
git commit -m "write info"
Push to specific Remote Branch
git push --set-upstream origin feature/profile
If local branch exist on remote
git push
Branch
List Local Branch --> git branch
List Remote Branch --> git branch -r
List all branch --> git branch -a
Create new branch --> git checkout -b feature/login
Switch to branch --> git checkout feature/profile
If local branch and remote branch is different and and you want to push
git push origin <local>:<remote>
git push origin main:feature/profile
If local branch exist on remote
git push
Get the latest changes from Remote
git pull
Check the branch status
git status
Delete local branch
git branch -d feature/profile
git branch -D feature/profile
Cherry-Pick
git fetch origin
git checkout -b bugfix/ID23 origin/hotfixv1.0.1
git checkout bugfix/ID23
git cherry-pick <commit HashID>
git push origin hotfixv1.0.1
Get the Commit History
git log
Working with remote branch
git remote -v # gives the detail of Remote repository
git fetch origin
git fetch -p #This command help to synchronize the remote origin
Rollback Specific commit
git log --oneline
git revert <commit ID>
git push
Go Back to Specific commit
git log --oneline
git checkout <> <commit ID> <File Name>
git status
git commit -m "go back to this commit"
Deleting the commit items
git log --oneline
git reset --mixed <commit ID> --> Uncommit and keep the changes in
Unstage Area
git reset --soft <commit ID> --> Uncommit and keep the changes staged
git reset --hard <commit ID> --> Unstage and Delete All the changes
Config the email for the accessing the repo
git config --global user.name "Full Name"
git config --global user.email "email@address.com"
Comentarios