A simplified overview of git.
Lesson Level: Beginner
Last Updated: 2021-02-12
Created By: Theo Acker
Table of Contents
- Standard Workflow
- Command Reference
- Extra Topics
I recommend regularly using the command
Sample branch is named practice.
Creating a new branch
- Create a new branch named practice:
git checkout -b practice
- Push to GitHub for the first time:
git push -u origin practice
- Check your current branch using
- If you are not on the branch you want to be on, switch:
git checkout practice
- Make sure you are up to date with GitHub:
- Check what files you have changed:
- Stage files to be commited:
git add <filename>or
git add *
- Make sure you have staged the correct files:
- Unstage files if necessary:
- Commit staged files:
git commit -m "commit message"
When you are ready to add the changes from your branch (sample branch is named practice) to the master branch on GitHub:
- Go to the GitHub repository.
- Make a new pull request.
- Click Pull requests from the top bar.
- Click New pull request.
- Select the branch to merge.
- Click Create pull request.
- Optionally add a comment, then click Create pull request.
- Merge the pull request - you may want to wait for your collaborator's input before mergin.
- Assuming GitHub says there are no conflicts, click Confirm merge.
- If merge was successful, click Delete branch.
- Carry the changes over to your local repository.
- Switch to the master branch:
git checkout master
- Add the changes from GitHub:
- Delete the old branch:
git branch -d practice
- Switch to the master branch:
- Create a new branch to make more changes.
Most Common Commands
git status: An extremely useful command for information - what branch you are on, what branch it is connected to on GitHub, whether or not the branch is up to date, and staged or unstaged files.
git add <filename>: Stages file(s) to commit. If you adding all unstaged files, can replace
git commit -m "<message>": Commits all staged files.
git pull: Checks the remote (GitHub) branch for any changes and updates the local branch.
git push: Pushes your changes in the local branch to the remote branch.
git branch: Lists all branches and indicates which one you are on.
git checkout <branch-name>: Switches to the branch specified if it exists.
git checkout -b <branch-name>: Creates a new branch and switches to it.
git branch -d <branch-name>: Removes the specified branch (make sure you are on a different one first).
git push -u origin <branch-name>: For first push from a new branch, sets up tracking with the specified branch from Git
Other Useful Commands
git branch <branch-name>: Creates a new branch, but does not switch to it.
git reset: Unstages all staged files. Useful if you accidentally added the wrong files.
git restore --staged <filename>: Unstages the specified files. Useful if you accidentally added a file you didn't want committed yet.
git init: Initializes a new git repository in the current directory.
git clone <repository>: Clones an existing git repository.
Check if git is installed:
If you need to install git, check the Software Carpentry instructions for installing git.
Taken from Software Carpentry Setting Up Git.
--global flag will ensure that you won't have to do this again on this computer - git will be configured for all projects.
Your git username will be associated with your activity, so you probably want to use your GitHub username. To configure with the username Vlad Dracula:
git config --global user.name "Vlad Dracula"
Make sure to use the email address associated with your GitHub account. To configure with the email email@example.com:
git config --global user.email "firstname.lastname@example.org"
Different operating systems deal with line endings in different ways. To prevent the problems this can create, run the correct command for your operating system (OS).
- For MacOS or Linux:
git config --global core.autocrlf input
- For Windows:
git config --global core.autocrlf true
Starting with GitHub
If you already have an existing GitHub repository, skip step one.
- Create a new GitHub repository and initialize with a README.
- Get the identifier for the GitHub repository. You can find this identifier by clicking the Code button and copying the "https..." string provided under Clone, or you can copy the URL of the website and add ".git" to the end. It will look something like this:
- Clone the repository on your computer:
git clone https://github.com/username/repository-name.git
- Enter the new repository:
Starting with a Local Repository
If you do not already have a local repository, you can create one in your current folder with the command
git init. Warning: always make sure you are not already in an existing repository by typing
git status first. If you receive an error message that says something similar to: "fatal: not a git repository" then you are good to go!
Create a new GitHub repository. Do not initialize it with a README, a .gitignore file, or a license. On the creation of a blank repository, GitHub will provide a set of instructions. Follow the ones to push an existing repository from the command line.
git remote add origin https://github.com/username/repository-name.git git branch -M master git push -u origin master
Some git commands (like
git add <file>) accept regular expressions (as do some Bash commands). If you are not already familiar with regular expressions, I do not recommend putting a lot of time into them, but there are a few aspects that are particularly useful and that you may recognize from searching databases in the course of your research.
file.txt is a regular expression (or regex) that matches only files named exactly
The most useful character to know is the asterisk
*. This takes the place of any number of characters, which makes it a good shorthand to use when you want to list multiple files.
*.txtmatches all files that end with
.txt. This could include file.txt, notes.txt, and abc.txt, but it would not include code.py or text.md.
myFolder/*matches all files contained in myFolder, including any folders contained in myFolder and so on. It would include myFolder/file.txt, myFolder/code.py, and myFolder/folder2/folderX/notes.txt.
myFolder/*.txt, then, matches all files that end with
.txtthat are in myFolder or one of its subfolders.
Bash commands are referenced throughout this overview. For more information, see Software Carpentry's Unix Shell lesson. These are commands like
cd that help you work in your terminal.
Much of this information originally came from Software Carpentry's git lesson.
This is a short but useful list of git commands.
If there is a branch (sample branch is named practice2) on GitHub that you would like to have on your local repository:
git branch practice2 origin/practice2
If you make a commit and your terminal screen starts looking very strange, you probably forgot to add a commit message. When
git commit is entered with no
-m "message", the terminal will open a default text editor, typically in your command line. Typing
:q should allow you to exit. This will abort the commit, so you will have to redo it.
git pull may respond with an error because the remote contains work that you do not have locally or
git push may respond with: "Automatic merge failed; fix conflicts and then commit the result."
If possible, open up the file with the conflict and look for
<<<<<<< HEAD, some content,
=======, conflicting content, and
>>>>>>>. Remove the markers and manually reconcile the conflicts - that is, decide how you want the file to look. Then add, commit, and push.
If this is not possible/does not work and you're not sure what to do, find someone to help you troubleshoot.