Chapter 6 GIT Commands
6.1 GIT Terminology
origin
: connection pointing to the remote repositorymaster
: name of your default branch. A branch in Git is simply a lightweight movable pointer to a commitworking directory
: local repository.git directory
: Git stores all of its repository data in the .git directory. This is created when a local repository is initialised using theinit
command.git.ignore
: Git uses this file to determine which files and directories to ignore, before you make a commithash
: the commit command creates a unique ID called a hash, which is an absolute pathHEAD
: pointer to the last commit of the branch you are currently on. If you are on the master branch, then HEAD and master will refer to the same commit. This is a relative path. To see the previous commit useHEAD~1
6.2 GIT Commands
Initialise
git init <local repository name>
initialises a new local repository
Remotes
git remote add <remote name> <url>
creates a new connection to a remote repositoryis the shortcut for the and is typically set to ‘origin’ git remote show <remote name>
shows which branch is automatically pushed to when you run git push while on certain branches. It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple local branches that are able to merge automatically with their remote-tracking branch when you run git pullgit remote rename <remote name> <new remote name>
rename a remotegit remote remove <remote name>
remove a remotegit remote -v
lists the remotes that are configured
Branches
git branch <new branch name>
adds a new branch, a structure with trees for saved states of filesgit checkout <branch name> <filename>
checks out (i.e. switches to another version) an old version of a filegit branch
lists all of the branches in a repository, with a * next to the branch you are currently ongit checkout <branch-name>
switches to another branch-namegit checkout -b branch-name>
creates the branch and switches you to itgit merge source destination
merges two branches
Status
git status
shows which files have changed/new in your repositorygit diff
shows the changes you made to the filegit diff --staged
shows the difference between the last committed change and what’s in the staging areagit diff directory
shows the changes to the files in the directorygit diff -r HEAD
-r flag refers to compare to a particular revisiongit log
view the log of the project’s historygit show <hash>
view the details of a specific commit, with the first few characters of the commit’s hashgit annotate <filename>
shows who made the last change to each line of a file and when
Clone
git clone <remote name>
to clone a repo and download a copy of a repo to a local folder This automatically creates the remote called origin
Add
git add
adds from your working directory to your staging area, ie specifies what will go in a snapshotgit add <filename>
stages a filegit add -A
stages all new, modified and deletedgit add <foldername>/*
adds folder and contents to your staging area
Remove
git clean -n
shows a list of files that are in the repository, but whose history Git is not currently trackinggit clean -f
will then delete those files
Undo
git reset
undo ALL changes that have been staged with git addgit reset HEAD <filename>
undo changes to a specific filename that have been staged on HEAD
Commit
git commit -m "<message>"
commits the file with the snapshot to thelocal repository
locally. The commit records the changes to the file ie actually takes the snapshot and makes a permanent record of it
Fetch
git fetch
gets any new work since last clone or fetch. Fetch does not however merge remote work with our work
Pull
git pull
automatically fetches and then merges that remote branch into your current branch
Push
git push
adds the files to your remote git
Note on adding files to the remote:
- When it is your first push from a repo, you will first have to make the link between the local and remote repository via:
git push --set-upstream origin master
, or shortergit push -u origin master
. As of then,git push
will refer to the upstream branch you’ve set: i.e. origin / master
6.2.1 references
- Happy Git and GitHub for the useR by Jennifer Bryan adapted under Creative Commons Attribution-NonCommercial 4.0 International License.
- Pro Git book, written by Scott Chacon and Ben Straub adapted under the Creative Commons Attribution Non Commercial Share Alike 3.0 license
- Version Control with Git by Software Carpentry adapted under the Attribution 4.0 International (CC BY 4.0 license