Home | Applications | Documentation | Transport | Personal |

Documentation

Home | Cloud | C# | Database | DevOps | Revision | Web |

DevOps | Repos | Pipelines | Boards |

Code Repositories

This is for git and not any repository in general. This works for GitHub, Azure DevOps, VS Code, VS2019 and the command line.

Introduction

Need a detailed understanding of git repositories for Home and Work. My current code repository is with github. Login is with my email address and the password hint is NM301. This is my current understanding of how git works.

Git Basics

Git is there to control source code and shows current state, work in progress and history. Projects are downloaded, worked on and the stored in the git repository. Many different people can work on the same code at the same time if they create separate branches and can then merge the changes to the master branch. Conflicts are shown if 2 branches merged are changing the same line of code.

Git Location

When a git repository is created for a project, the git control directories are created in the main directory of the project. These are there to control and record the changes. In the git setup, you have the option of telling git which files you wish to exclude for the source control. You can still store files that are not in the source control in the directory.

Command Line Commands

Git can be run from interfaces such as DevOps and GitHub. The command line gives you more control if you don’t want to use the UI in the application you are using.

Is it installed on your PC
From the command prompt, type git version

Is there a git already setup for the directory
From the command prompt, type git status
Note that if a git repository is installed, you will also see a .git directory.

Creation
If you have a directory that has source code and you want to add git to it then
From the command prompt, type git init
This will create the .git directory which controls the source code repository.

Adding a file to git
You first need to move the file to the repository manually from the source location to the source code directory.
From the command prompt, type git add {filename}
This will add the control to the repository
Also
To add all files not staged, git add .
To add a file, git add file.txt
To add an entire directory, git add css

How to start making a change
You should always create a branch so that your code is separate from anyone else’s code
From the command prompt, type git branch {branch name}

How to switch to use the new branch
From the command prompt, type git checkout {branch name}

How to save a change
To save the change from your editor to git
From the command prompt, type git commit {filename} -m {commit message}

How to merge the change with the master code
From the command prompt, type git merge master
This merges your current branch to the master

How do I check that everything is ok
From the command prompt, type git status
This will show you the current branch you are using and any changes that you have not saved to the repository (commits)

Remote Repositories
Above are the commands for a local repository. Normally, you would be using a remote repository like DevOps or GitHub. For these there are additional commands that are required. After updating the local repository, you will need to move it to the remote repository.

How do I save changes to a remote repository
If you have a remote repository, then once you have committed your code to the local repository, then you need to push the changes to the remote repository.
From the command prompt, type git push {remote repository – origin} {branch}
You can push all changes using git push -–all
How to force a push to a repository. Use git push --force-with-lease origin master

How do I get changes from a remote repository
If I need to get the latest version of source code from a repository, then you need the pull request..
From the command prompt, Type git pull {remote repository – origin} {branch}

How do I download a repository to my machine
If you want a copy of a project onto your PC, then you will need a copy of the git repository.
From the command prompt, Type git clone {remote repository – origin}

Examples
Create a new repository on the command line
echo "# myWebPages" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/The-David-Skinner/myWebPages.git
git push -u origin main

Push an existing repository from the command line git remote add origin https://github.com/The-David-Skinner/myWebPages.git
git branch -M main
git push -u origin main

Using GitHub

To login go to www.github.com.
Type in email address and password (hint = )
My account is https://github.com/The-David-Skinner

To find the Repositories, click on The-David-Skinner and select Repositories.

To create a new Repository, Press New and ensure you make the Repository Private.

To copy data across from my pc to GitHub, find the code that you wish to copy across. Add a local git repository by using ‘git init’. Add all of the files to the repository by running ‘git add .’. Now we need to connect to the remote repository. Type ‘git remote add origin https://github.com/The-David-Skinner/myWebPages.git’. Create a master branch using ‘git branch -M master’. Commit the files using ‘git commit -m "Copy to GitHub"’. Finally push the code using ‘git push -u origin master’.

Using Visual Code Editor

To find out which repository you are connected to, use terminal and type git config –list. Press enter until you get to (END) and the press q.
Basically, if you open the source from a cloned source, then use the features or command line from terminal.

Using Visual Studio 2019

Go to GitHub and create a new repository for CountdownNumbers. Launch VS2019 and find the solution to launch. Found in C:\Users\David_Admin\source\repos\CountdownNumbers. I can also see that it already has a git repository. Team Explorer is showing that it is currently looking at The-David-Skinner/VSCode: General code for my projects (github.com). This is showing the local repository. We need to create the remote repository as we did for the “Using GitHub” example above. Once it is created, we need to amend the remote repository location. Use ‘git remote set-url origin https://github.com/The-David-Skinner/CountdownNumbers.git’. Restart VS2019. Run a commit and push from the command prompt. VS2019 now shows that all Pushes have been done. Now commits and pushes work with no issues.