-
Make sure git is installed
- SSH keys for those who want them
-
Git config
-
git config --global user.name "Name" -
git config --global user.email "user@email.com" -
Editor
$EDITORor$VISUAL[System Wide]- e.g.
export EDITOR=vimin~/.bashrc
- e.g.
git config --global core.editor vim/emacs/etc[Git only]
-
git status-- shows current status, including which files are tracked/untracked and which are stagedgit log-- Shows a log of all commits.- By default, this is hard to read. I personally have
alias lg="git log --all --decorate --oneline --graph"specified in my.bashrc, so all I need to type islgfor a more readable git log
- By default, this is hard to read. I personally have
git add-- add (stages) filegit commit-- Commits all staged filesgit push-- pushes all commits to remote repositorygit pull-- pulls commits from remote repository
- Starting from command line:
git init-- Sets up git repository in current folder
- Add remote repository
git remote add <name> <url><name>can be any name, typically the main remote branch is calledorigin<url>is the URL of the git repo. This looks likehttps://github.com/<username>/<repo_name>if using HTTPS andgit@github.com:<username>/<repo_name>.gitif using SSH.
- Starting from web
- Create repository on GitHub.com (or equivalent)
- Clone repository
git clone https://github.com/<username>/<repo_name>-- HTTPS/no SSHgit clone git@github.com:<username>/<repo_name>.git-- SSH
- Remote repository will automatically be added with this method, with the default name
origin.
-
Check the status of your repository with
git status- Will list untracked files, tracked files with unstaged changes, and staged files
- Staging = telling git to add the file to the next commit
- Will list untracked files, tracked files with unstaged changes, and staged files
-
git add <file>-- Stages file to be committedgit add .-- Stages all files (even those in folders)git add *.py-- Only stage.pyfiles- Did you accidentally stage a file that you didn't want to?
git restore --staged <filename>
- If the file has been updated since the last
git addbut before a commit, you will need togit addthe file again.- I generally only stage files immediately before committing so that I know they're all at the latest version
Note: Git will continue to stage the file, even if it's deleted. If a staged file is deleted, you will need to git add <filename> it again to make git aware of its deletion
- Commit the files
git commit-- Opens a text editor (set viagit config) to write commit message on the top line. Save the file and quit the editor to continue the commit process.- Alternatively,
git commit -m "<message>"bypasses the editor entirely
- Alternatively,
This section may be incomplete in time for the Git workshop on Feb 25th.
Branches are very powerful, but may not be necessary if you're just using Git to keep your code backed up. A good usecase for branches are
- if you're making a change that would potentially break
master, and you want to keepmaster"clean" as the working version or - if you're contributing to someone elses repository
git checkout -b <branch_name>- This is short for
git branch <branch_name>andgit checkout <branch_name>, so it creates and checks out a new branch- By default, this branch is identical to wherever
HEADis (HEADis git-speak for what is currently checked out. This could be an old commit, a branch, etc). Unless you have specified otherwise,HEADis at the latest commit of the current branch.
- By default, this branch is identical to wherever
- This is short for
- Now there are two branches, one
master(by default) and the second<branch_name>.- Switch between the two with
git checkout master,git checkout <branch_name>
- Switch between the two with
- The two branches are completely isolated from each other. Changes to one branch do not affect the other
Often, you're going to eventually want to merge the contents of one branch into the other.
git merge <branch_name>will merge all changes from<branch_name>into the current branch.- Git always merges the specified branch into the current checked-out branch.
- Git will automatically detect any conflicts between the two versions. Often, it's able to reconcile the differences on its own but sometimes it requires some manual intervention.
Much like the command diff, git diff can provide information on the differences between files in different commits.
- By default,
git diffalone will show the changes between all unstaged and the last commit.git diff --stagedshows changes between staged and last commit
- You can also see the differences between two versions of a file, using the commit hash found by running
git log- e.g.
git diff db821bc 5104f19 my_file.txtwhere the two commit hashes are the shorter versions given bygit log --oneline
- e.g.
git diff <branch1> <branch2>see diff between branches
This section may be incomplete in time for the Git workshop on Feb 25th.
Adding and committing files works even if you're not syncing with a remote repository, allowing for a local "track changes" of your code (which can be useful!). However, the power of Git comes from being able to publish your code online and work collaboratively.
- If you don't have a remote repository set up yet, make sure you go back to that section and do that first
- Once you have made commit, you can push it online. This can be done with
git push origin master. This pushes the current commit to themasterbranch of theoriginremote repository.- You can specify
-u(git push -u origin master) to let git know that this is the "upstream" repository, meaning in the future you only need togit pushwithout specifying the branch or repository name.
- You can specify
- Similarly, if there was a change in the remote that you want to pull into your current repository, you can so
git pull origin master(orgit pulliforigin masteris set to be the upstream branch).
- Git Online Ebook -- Great resource!
- Cheat Sheet
- Group Folder