Version Control
11.13 Github
3 Parts of Version Control
- Working Directory: This is the folder where you store your files (e.g.,
C:\Home\hw1) - Staging Area: Every time you make changes to your files, you need to stage the files. This tells git that these files are ready
- Repository: Every working directory that uses git for version control will have a
.gitfolder, which records info about each version of the working directory.
Basic git commands:
git --version
- Check version of git
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"
- Configure git to recognize you
git config --global user.name
git config --global user.email
- Confirm that user name/e-mail are configured
git init
touch readme.md
git add readme.md
git commit -m "added readme file"
- Initialize new repository
- If you are at the file path
C:\Home\hw1,git initwill initialize the repository in thehw1folder - An invisible
.gitfolder will be created in thehw1folder to track files. If you are at the file path
C:\Homeand have not created thehw1folder, then you can typegit init hw1, which will create thehw1folder and initialize the repo.- The code above did the following:
git init: initialize repositorytouch readme.md: added a blank readme Markdown filegit add readme.md: staged readme.md (this tells git that you are ready to commit this file to the repo)git commit -m "added readme file": committed changed to the repo (-mallows us to add a message)
- If you are at the file path
git diff
- Check differences between in the staging area and working directory.
- Output: difference between each file that has changed since you last staged files (
git add) - If you have staged all files, this command will output nothing, since nothing is different between your working directory and staging area.
git diff --staged
- Check the differences between staging area and repository.
git log
- See list of commits.
git reset file1.md
- Suppose
file1.mdwas staged, but I want to remove it from the staging area.- Now,
file1.mdwill be untracked.
- Now,
git remote add origin https://github.com/<username>/<repo-name>.git
git push -u origin master
- Create a new repository in Github. Link your local working directory with the Github repo with these commands.
git remote add: sends commits to the locationorigin- We associated
originas a shortcut for the Github repohttps://github.com/<username>/<repo-name>.git. git push -u origin master- Send
masterbranch to remote repo calledorigin - Option
-uallows us to usegit pushas a shortcut forgit push origin masterin the future
- Send