Setting up a Local Repository in GitHub using Git

Notes

GitHub is a version control system that can be used to keep track of versions of codes for projects we work on in our lab. In addition, it can be used to collaborate with others on the same projects.

1. Create a GitHub Repository

  • Start by creating an account at https://github.com/ with your UTEP email.

  • Next, click on the “+” in the top right corner and choose “New repository.” It is important to make the repository Private to ensure these files cannot be viewed by the public.

  • Give this repository a name and description as needed. At this time, it is recommended that you not include a README , license, or gitignore file. These can all be added after the repository is initialized.

2. Download Git for Windows

  • To install Git Bash, visit https://git-scm.com/download/win, and download the latest 64-bit version of Git for Windows.

  • For our purposes you can choose all the default/recommended settings in the download wizard.

  • Once complete, open the Git Bash terminal and navigate to the working directory of the folder that contains your code files. The simplest way to do this is to right click in the folder you want to use and select “Show more options.” Next, choose “Git Bash Here” and the terminal should open with the directory automatically entered.

3. Initialize your Local Directory as a GitHub Repository

  • Please note, the “local repository” denotes the files contained on your personal device while the “remote repository” will store your files on the GitHub website.

  • Create the initial branch of the local repository by using the following command (Any name can be chosen for the branch but for our purposes it is common to name it “master”).

git init -b master
  • Add the files in your local repository using the command below. This action will prepare all the files to be committed to the remote repository you created in Step 1.
git add .
  • For future reference, individual files within your local repository can be added by using this command.
git add file_name.cpp
  • The command shown below will show the files staged for commit. It can also be regularly used to display the files that have been modified and not backed up to GitHub.
git status
  • Commit the files that have been staged in your local repository with this command. Note that this is the first time committing files which is why we are calling it “initial commit.” Feel free to amend this description to track the changes you make to your code.
git commit -m "initial commit"
  • Return to the repository you created on the GitHub website and copy the remote repository URL found at the top of the page.

  • Back in the command prompt, type the following command and include the URL to your remote repository in place of <REMOTE_URL>.

git remote add origin <REMOTE_URL>
  • Next, execute the following command to verify the new remote URL.
git remote -v
  • Finally, push the changes in your local repository to GitHub by using the following command. You will be prompted to sign in to your GitHub account from the terminal.
git push origin master

4. Done

  • The files in your repository should be viewable on GitHub. You may have to choose the right branch from a drop-down menu on the top right to view your files.

  • Please note, every time you want to back-up your files on to GitHub you will be required to add, commit, and push your code as shown above.

5. Commit and Push to GitHub using Visual Studio Code

  • If you use Visual Studio Code you can commit and push code without the use of the Git Bash terminal. This is done by adding the folder of your local repository to your workspace in VS Code.

  • Open up a file in this folder and make changes to it using the text editor (Save all changes).

  • Next, navigate to the “Source Control” tab using (Ctrl+Shift+G). Click on the drop-down menu and select “Commit & Push.”

  • A COMMIT_EDITMSG file will appear in VS Code. You will be prompted to enter the commit message for your changes. You will type the message inside of quotation marks and save the file using (Ctrl+S).

  • When you close the file where you entered the message, the changes should be pushed and viewable in your GitHub repository.

1 Like

In Step 3, we mentioned that changes to our code can be tracked using Git. The following will explain how to install and use Gitk. This is a graphical repository browser that allows users to visually keep track of the history of a repository.

  • Use the following command in the Git Bash terminal to install Gitk.
sudo apt-get install gitk
  • Next, simply type the command below to open the Gitk interface.
gitk