Part 1 Git version control integration in Visual Studio Code
Part 2 Git master branch source control integration in Visual Studio Code
Part 3 Git clone version control integration in Visual Studio Code
I have already wrote few articles on how to use git earlier, but they where completely for Visual Studio Code, GUI with little exposure to Command line. In this article I will perform the same tasks using complete git commands. I have already downloaded and installed git application from https://git-scm.com/downloads. Git is a command line tool a distributed version-control system for tracking changes in source code during my DevOps scripting development. It helps me to collaborate my all scripting work among my colleagues and acts as good Central repository, it also helps to track changes in any files and can check in going point in time to review what changes where made when committing/pushing files. Git works very well for local repository. GitHub.com is a central Git repository hosting service, but it adds many of its own features. GitHub provides a centralized Web-based graphical interface. It also provides access control and several collaboration features, such as a pull requests, wikis and basic task management tools for every day projects.
Solved Visual studio Code make sure you configure your user.name and user.email in git
First to start I have created an account on github.com, After login create a new repository (Basically it is a folder/directory contains all the project code/scripts files, including revision history) by pressing + button on the right top side of the screen, select New repository from the list. Provide repository a name and description (optional), I am keeping this repo public so anyone can view it and we can also choose who can modify it to it by assigning permissions later. Press Create repository.
Once the repository is created This shows some very basic useful information regarding git command line introduction and how to quick setup. Grab/copy the git url location with HTTPS (Another protocol you can use is SSH), this will require later.
I have opened my PowerShell and using oh-my-posh module to add flavors to my PowerShell console. That helps to enhances the prompt and git operations. I am creating a folder called Git-Demo.
New-Item -Path D:\Scripts -Name Git-Demo -ItemType Directory
I will keep all the my codes/script on the same directory later, go the the directory to Git-Demo.
It is empty. Run below command to initialize the empty repository on the folder, Every time you run git command keep eye on the prompt head it says master with color green (means there are no changes inside directory everything is up to date nothing to stage), it is a master root branch also can be called first base branch
It creates .git folder, This folder contains actual version control with all the metadata regarding this local git repository, all the changes will be captured in the folders. I am creating first file call README.md (I will call it help file, it has all the information regarding to my projects, scripts, howto etc.)
Write-OutPut "# vCloud-lab -Test Readme" >> README.md
As soon as I generate a new file, prompt color changes to Yellow with +1, means there is new file added. (first digit with + plus letter represents new files count added to the folder, second digit with ~ tilda character says this much of files are modified, and the last digit with - minus sign indicate deleted files count), Because I have added a single file it is showing +1 as a count.
The git add command adds a change in the working directory to the staging area. As below command I am only adding one file to the staging area, If you have multiple file you can use . (dot) or * (star) and keep checking status with git status. The git status command displays the state of the working directory and the staging area.
git add .\README.md
Next use Git Commit. The commit command is used to save your changes to the local repository, -m means message and it is mandatory.
git commit -m "first demo commit"
When I commit to the local repository, it immediately change the color of prompt with back to green color logs with 1 file changed, 1 insertion(+). As far all is good and everything is added to local repository nothing pushed to centralized github repo, now I can make as much as modifications to the files and keep adding and committing to local repo. All the code is stored locally, Before pushing the local changes to github, it requires to mention which online github.com repository you want to push data, use the same HTTPS git url shown before. This is a one time command.
git remote add origin https://github.com/username/vCloud-lab.git
Next start pushing data to online github repository.
git push -u origin master
When you add a remote url, try to push, for the first time it will try to authenticate to github.com and prompts you for to add credentials. Because I am using it on visual studio code, it is asking me for additional permissions. I am using Windows 10, credentials are stored under your accounts Credential Manager in encrypted form so you don't have to type user name and password again. Regarding this if you are facing error check my article.
Remote: Permission to UserName/repo.git denied to OtherUserName fatal: unable to access 'https://github.com/UserName/repo.git/': The requested URL returned error: 403
Once file is pushed online you can view it on github.com by refreshing it on browser. Now make few more tries, add, modify and delete files with git add, commit, push command and see the changes offline/online.
Next I will create a branch for better collaboration with my colleagues. A Git branch is essentially an independent line of development. It means me and my team will not make any direct modification/changes to the Master, in layman term instead I will copy the the Master folder, give the folder name, make changes on the copied folder, once work is done merge the changes with Master branch. if require delete the the branch I was working on to keep repository look cleaner. You can check the branches list with below command.
I have only one base branch - master and it is * starred means I am onto the branch, whatever changes I make, will be done under master branch, here also when I create new branch it will be created on top of selected * starred branch and data is cloned from the branch. Create a new branch using below command.
git branch vcloud-lab-projects
Now list the branches again. New branch is populated, all the changes are stored locally it is not yet pushed online on GitHub, the web screenshot here shown only for information purpose.
To change branch and start working on new branch, use below command.
git checkout vcloud-lab-projects
Once branch is changed notice the head of prompt, it switches to new branch. Whatever changes we make will reflect here only and master branch will be untouched.
Next I have copied 2 powershell ps1 script files on the folder (using explorer), when I just use ls command the prompt changes to yellow showing 2 new files added. There are 2 untracked files. Add the all files in git. You can use below command to see the status and add untracked files
git add .
Check the status again, run Commit with message to save them to git repository.
git commit -a 'Add scripts to branch'
Check the status again, prompt is green, This shows on which you are now, Nothing to commit, working tree is clean.
You can view the changes online now by refreshing github.com in browser, on the master branch there are no new files, click on the master button and choose the branch vcloud-lab-projects. All the newly added files are visible on the new branch now.
Next I will merge the changes made on from vcloud-lab-projects to master, I am still on the vcloud-lab-project, and to proceed further I will checkout to master branch.
git checkout master
Once switched to master branch you can verify no .ps1 files visible earlier added and there is still only sole README.md file exist.
Check the prompt I am on the master branch now. To start merging use below command.
git merge vcloud-lab-projects
list the files, all looks good, Check the prompt now, color changes to Magenta with up arrow, which means all the merging is done locally, To push it on online github repo, use below command.
Refresh github.com page and switch to master branch now you will see pushed files.
In the last, if you want to delete branch, once work is completed, below command helps you to perform the task on local git repository.
git branch -d vcloud-lab-projects
To delete branch on centralized github.com repository use as below command.
git push origin -d vcloud-lab-projects
Next refresh web page and verify by clicking master button, other branch no longer exists.
Resolved: Git warning LF will be replaced by CRLF in file
Creating an internal PowerShell module repository
Powershell Azure Inventory GUI Utility
This post first appeared on Tales From Real IT System Administrators World And Non-production Environment, please read the originial post: here