Git basics: Get started with Git and GitHub
Many organizations treat documentation as code, so technical writers need to know how to use Git and GitHub for version control. This article is a user guide for the basic workflow to work with a repository. You’ll learn how to create, clone, and commit changes to a repository.
This article is written as a beginners guide for Git and GitHub. The repository is set up for one person to learn the command syntax. Some steps verify that a command worked as intended so that you can see the results and understand the concepts.
Prerequisites
Use the default settings for these installations unless your computing environment requires specific configurations.
- Install Microsoft Visual Studio Code (VS Code).
- Install Git. After the install, use
git config
to set up your editor, name, and email address. For more information, see First time Git setup. - Install PowerShell. PowerShell integrates with VS code and you can run PowerShell commands from a VS Code terminal.
- Create a GitHub account. Go to GitHub and select Sign Up.
Create a repository
To begin, you’ll create a GitHub repository.
- Sign in to GitHub.
- On the top, right of the screen, select the plus sign (
+
). - Select New Repository.
- Enter the repository information:
- Enter a Repository name such as testrepo.
- Add a Description such as My test repository.
- Select Public.
- Under Initialize the repository with select Add a README file.
- Select Create repository.
The repository is created and your browser opens to the repository’s Code tab. The README.md file is displayed and includes the repository’s name and description.
Clone a repository
A clone is a copy of a repository that you download to your computer. You use a cloned repository to add, change, or delete files.
- Launch VS Code and open a terminal: Terminal > New Terminal.
-
Use PowerShell commands to create a directory for your repository and switch to that directory.
New-Item -Path "C:\github\clonedemo" -ItemType Directory Set-Location -Path "C:\github\clonedemo"
New-Item
creates the directory C:\github\clonedemo andSet-Location
switches to the directory. - From GitHub, go to your repository’s Code tab and select the Code button.
- Select HTTPS and use the clipboard icon to copy the URL.
-
From the VS Code terminal, paste the link to clone the repository.
git clone https://github.com/<GitHub account>/testrepo.git
The output shows the progress.
Cloning into 'testrepo'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (3/3), done.
-
After the clone is finished, switch to the repository’s directory.
Set-Location -Path "C:\github\clonedemo\testrepo"
Verify the remote
When you clone a repository, the origin
remote is created with fetch
and push
. A remote is a
shortcut name for the repository’s GitHub URL. The fetch
downloads repository changes from GitHub
to your computer and push
uploads changes from your cloned repository to GitHub. The commands are
used to keep your repository in sync.
To view the cloned repository’s remote name and URL, use the git remote
command.
git remote -v
The output displays your repository’s remote name and URL.
origin https://github.com/<GitHub account>/testrepo.git (fetch)
origin https://github.com/<GitHub account>/testrepo.git (push)
For a single-user repository only
origin
is necessary. When you work with a shared
repository, you'll use additional remote names. For more information about how to manage remotes,
see About
remote repositories.
Change a file
Git is version control software that tracks changes to files. To see changes that aren’t committed,
use the git status
command. In this example, you’ll add some text to the README.md file and save
the change.
-
In the VS Code terminal, display the cloned repository’s status.
git status
The output shows nothing to commit, working tree clean.
On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
-
To open README.md in VS Code, use the
code
command. Then update and save the file.code README.md
-
Run
git status
and verify the output shows there are Changes not staged for commit.On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
-
Update Git tracking so that the changes can be committed.
git add README.md
-
Run
git status
and the output shows Changes to be committed.On branch main Your branch is up to date with 'origin/main'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: README.md
Commit a change
After a file is changed in your cloned repository, you commit the change and then push the commit to
your GitHub repository. When you commit changes, include a brief message that describes the commit
and is included in the commit history. The push
syncs your clone with GitHub and the commit
history is the same in both places.
-
Commit changes to an updated file in the cloned repository.
git commit -m "updates README.md"
Each commit has a 40-character unique hash and
git
commands reference the initial seven characters. The hash in this message isb897559
.[main b897559] updates README.md 1 file changed, 2 insertions(+)
-
Run
git status
. The cloned repository is one commit ahead of the GitHub repository.On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
-
To upload the commit to GitHub run the
push
command.git push origin main
The output shows the status as the commit is uploaded.
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 328 bytes | 328.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/<GitHub account>/testrepo.git d3e4f6a..b897559 main -> main
After a successful push
your cloned repository and GitHub repository are in sync. Your GitHub
repository contains the updated README.md file.
An optional step is to confirm the commit history is identical between your local clone and GitHub. In a VS Code terminal, run
git log
to display the commits. On GitHub, go to your
repository's Code tab and select Commits.
Clean up
When you’re done and no longer need the repository, delete your repository from GitHub and the local clone.
- From GitHub, there’s a drop-down menu at the top, right of the page. Select Your repositories
- Select testrepo.
- Select Settings and scroll down to Danger Zone.
- Select Delete this repository and follow the prompts.
- To delete your local clone, remove C:\github\clonedemo\testrepo.
Conclusion
In this article you set up a GitHub repository, cloned the repository, and committed a change to GitHub. To learn more about how to manage your repository, continue to Git basics: How to branch and merge.