Command-Line Git
Note: You can just upload files to GitHub directly through the web browser. You can do this in a pinch, if you are unable to upload files through git directly. However, using Git/Git Bash may make things much quicker once you get it set up!
If you like using GitHub to host your files and projects, you may be interested in using git on the command line. Commands that are associated with the git command can be used to interface with your GitHub repositories. This page is a quick guide to getting started with git commands on your personal computer.
- Standard one: http://git-scm.com/downloads
- You can use any text editor to edit your actual files, as is allowed in E 115.
- VS Code is allowed and can be convenient since it integrates into GitHub directly: https://code.visualstudio.com/docs/sourcecontrol/github
- Reference book: https://git-scm.com/book/en/v2
More Help:
If you would like a more interactive help session, NC State offers a free account with LinkedIn Learning, which hosts a variety of tutorial videos. Once you have set up your LinkedIn Learning account, you can watch the broad git introduction, Learning Git and GitHub. If you want a more in-depth lesson, check out the Git Essential Training.
Introduce yourself to Git
Once you’ve installed Git, you can use it through GitBash on Windows. It looks basically like the terminal (it functions pretty similarly to one, but doesn’t have quite everything working!). You can use the Terminal on a Mac/Linux machine



Using your Repositories
Once you’ve set up an account at github.ncsu.edu, and have created the webpages
repository as described in GitHub account preparation, you can clone the repository.
- Get the files from your repository before starting.
- Make a local repository as a clone of main
git clone <blah>
(e.g.,git clone https://github.ncsu.edu/unityID/webpages.git
)- If you are having trouble, clone with
ssh
instead. But you may need to set up an SSH key. Follow these directions. - See all the contents of the folder
ls -a
to see the .git folder.
- Make a local repository as a clone of main
- Make changes
- Put changes back up into repository
- Commit your staged changes in your repository
git commit -m "the reason for the change"
- Update the respository:
git push
- Commit your staged changes in your repository
- At this point, you should be able to see the changes in the browser and your files should match what you did locally.
Typical Workflow
git pull
- Get changes from a remote repository and merge them into your own repository
- This is only after you’ve cloned the directory the first time.
git status
- See what Git thinks is going on
- Use this frequently!
- Work on your files
git add –-all
(or just changes)git commit –m “What I did”
git push
- Pushes the changes to the remote repository. You can go online to see that the changes are available.
Exercises1
- You can clone a repo if it’s available online, but you can also just use git locally.
- Let’s say you want a new folder called
heaps
that is a git repository, so you type in the command-line:git init heaps
- if you do
ls -a
insideheaps
afterwards, what do you see? What aboutgit status
?
- Let’s say you want a new folder called
- If you’re working on an older version of Git, you may see an output from
git status
suggesting you can take files out of the staging area usinggit rm --cached
. Try this out:- Create a new file in an initialized Git repository called
example.txt
. - Use
git add example.txt
to add this file. - Use
git status
to check that Git has noticed it. - Use
git rm --cached example.txt
to remove it from the list of things to be saved. - What does
git status
now show? What (if anything) has happened to the file?
- Create a new file in an initialized Git repository called
- Create a new Git repository on your computer called
bio
.- Write a three-line biography for yourself in a file called
me.txt
and commit your changes. - Modify one line and add a fourth line.
- Display the differences between the file’s original state and its updated state. Try using
git diff
. Does that show the differences?
- Write a three-line biography for yourself in a file called
References
- Exercises from Research Software Engineering with Python ↩︎
Generating an SSH Key
In order to use git on the command line, you must first generate an SSH Key. The SSH Key is what allows your computer to remotely connect to a repository. Github has more information on adding SSH Keys.
List of Commands
There are many git commands that can be used (more than are possible to list here concisely), but there are some that you will use all the time or that may be helpful from time to time. This is a list of those commands.
- git clone <“URL”>
- Once you create a repository on GitHub, you can use this command to create the git folder in your file system
- The URL is the one provided by the repository in GitHub when you click the clone button (must be in quotes)
- Make sure you are in the directory that you wish the git folder to be created in before running this command
- git add
- Using this command “stages” the files and directories for commit
- Staging a file for commit is basically telling git that those files are to be included in the next commit
- It is only recommended to “git add .” on your initial commit. Otherwise, you should only add specific files you want to commit
- git status
- This command is used to check which files have been added (staged to commit)
- It will also tell you which files have been modified but not added
- git diff
- This command allows you to view the differences between your local file and the file most recently committed
- Additions are shown with a green + at the beginning of the line and deletions are shown with a red –
- git commit -m “Commit message”
- This command is to take the files that were added and commit them to the git folder
- Commits on the command line are stored locally until they are pushed
- All commits require a commit message, they should be descriptive for what you are commiting
- git pull
- This command allows you to pull from the remote repository
- This is mostly useful you did something directly online (or from a different computer) and you need to catch up.
- Pulling may overwrite any modified files that are not staged
- git log
- This command allows you to see a list of recent commits
- It will show you information like the name of the commit, the author, and the commit message
Exercises
- Create a new Git repository on your computer called
biography
.- Write a three-line biography for yourself in a file called
me.txt
and commit your changes. - Modify one line and add a fourth line.
- Display the differences between the file’s original state and its updated state.
- Create another new file
employment.txt
and add the details of your most recent job. - Add the new changes to both
me.txt
andemployment.txt
to the staging area and commit those changes.
- Write a three-line biography for yourself in a file called