Command-Line Git
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.
Note: This information is not a requirement for E 115.
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) on your current branch
- It will also tell you which files have been modified but not added
- Using this command on the master branch will also report if you are on track with the remote repository
- git diff
- This command allows you to view the differences between your local file and the file most recently committed to the branch you are on.
- 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 branch [-d]
- This command is used to create a new branch on your project
- Branches are useful so that you are not directly modifying the master branch (They can also be used for organization)
- The -d option is to remove an existing branch
- You should not delete branches unless you have pushed all your changes and merged or you do not want to keep the changes on that branch
- git checkout [-b]
- This command is used to switch branches
- You can use the -b option to create that branch and switch to it (if it does not already exist)
- If you switch to master, this command will tell you if you are ahead or behind the remote repository
- git push origin
- This command pushes your commits to the remote repository
- Pushing a commit makes that commit the active set of files
- You must specify the branch name you are pushing to
- git pull
- This command allows you to pull from the remote repository
- This is mostly useful if your local branch is behind the remote branch 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
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.