Quick git notes
For using git to fork/clone/submit assignments, see the
git student submission notes.
The git executable is located at /usr/bin/git
To try out the basics of version control:
- Create the base directory and initial contents for your project
- In the directory you just created:
(i) initialize a git project (git init)
(ii) add the current directory to it (git add .)
(iii) save your changes to the git project (git commit)
Add appropriate commentary in the log opened by the commit
command
- If you haven't already, create a makefile for the project
and add it to your git repository
(git add makefile) and commit your changes
(git commit), providing a suitable comment
during the commit.
- Create a new branch for a commented version
of the project (git branch my_new_version)
and switch to work on that new branch
(git checkout my_new_version)
You can switch between branches at any point using checkout,
but it is strongly recommended you add/commit any changes before
switching branches.
- Renaming main: "main" is now the preferred choice for naming
your core branch (formerly was "main"). After you have done an
initial commit in a repository you can change the name from main to
main using
git branch -M main
Most of the documentation below assumes you have renamed master to main.
- Plow along on the cycle of edits and commits .
If you need to move, rename, or remove files or directories
be sure to use the git mv or git rm commands
rather than the standard linux mv, rm (to ensure the changes are
accurately included in the repository updates).
- Run gitk while in the git repository and see how it lets you
explore your project history.
Note that if you have a git repository as a subdirectory of another
git repository the "parent" one will not track the files in the
subrepository unless you force it to with the -A option in add (not generally
a recommended approach).
Quick list of git commands
info
- git log
- get commit log
- git blame filename
- see who has done what to the file
- git log -p filename
- see the changes made to a file over time
- git diff
- see what you've changed in the file
- git diff HEAD^..HEAD
- see what changed in the last commit
- git help command
- get the help entry for a git command
- git help tutorial
- get the git tutorial
- gitk
- gives you a visual representation of the repo branch/commit history
- git log --graph --pretty --branches --date=relative --abbrev=commit
- gives similar information but in ascii format (when you're working
through putty or ssh without the ability to open a graphics window like gitk)
basics
- git init
initialize a repository
- git clone url
- create a local repository that is a clone of the (main branch of) the specified one
- to clone a different branch use
git clone url --branch thebranchname
- git add filename
- include the specified file (or directory) for the next commit
- git commit -m "...some message..."
- commit the changes made thus far, labelling them with the given message
- git commit --amend
- lets you change your most recent commit message
(not a good idea if you've already pushed the commit)
- git checkout -- filename
restores the file to what it looked like in the previous commit,
note there is a space between the -- and the filename
git checkout SOMECOMMITNUMBER -- filename
restores the file to what it looked like in the specified commit (use git log
to look up the id for the commit you want)
- git revert commitnumber
- reverses any changes made in the specified commit
(not a good idea if you've already pushed the commit)
- git revert HEAD
- reverts the most recent commit
(not a good idea if you've already pushed the commit)
- git tag -a tagname -m "tag message"
associates a tag with the current commit, allowing you to refer to it
by name rather than the commit number when using git checkout, push, fetch, show, etc
git tag with no options lists the existing tags
- git mv oldname newname
- rename a file or directory (-f option to force)
- git rm filename
- delete the file or directory (-r for recursive, -f to force)
branches and remotes
- git branch
- shows list of existing branches, marks current branch with *
- git branch branchname
- creates the new branch
- git checkout branchname
- switches to the specified branch
(make sure your adds/commits are up to date before switching)
- git merge targetbranch
- merges the target branch into the current branch
(make sure your adds/commits are up to date before merging)
- git branch -D branchname
- deletes the branch
- git branch -m oldname newname
- renames a branch
- git remote rename oldname newname
- rename a remote repository
- git remote remove reponame
- remove a remote repository
- git remote add reponame repourl
- add an upstream repository with the specified url, using the given name
- git fetch remotename
- fetch updates from the named upstream repository
- their contents can be seen in the branch named remotename/main, which you can also
use for diffs, merges, etc., e.g.
git fetch instructor
git diff main instructor/main
git merge instructor/main
- git pull
- pull will grab changes from the upstream repository
and merge them with your current repo
- git pull remotename branchname
- lets you pull from a specific remote and branch, merging any
changes from that repository with your current repo
(make sure your adds/commits are up to date before pulling)
- git push
- push your main to the upstream repository
git push origin branchname if you want to push a different branch
Stashes
- git stash
- temporarily store all modified tracked files (so they won't be seen/applied
as part of the next commit), placing them on a stack of such stashes
- git stash pop
- restore the top stash
- git stash drop
- discard the top stash
- git stash list
- list the collection of stashes (all the stashes currently on the stack)
Misc. notes
You can run a git command from a different directory using the --git-dir option,
e.g.
git --git-dir someRepo/.git status
Note that if you want to find an old commit and go back to it,
first we have to find the right commit #: use git log, which will
show your commit numbers (big ugly numbers) and associated messages.
Pick the command number, create a new branch for that commit's code,
and check it out:
git checkout -b CallMyNewBranchSomething 123456789
(replacing the 123456789 with your chosen commit's number)
You can create a list of files and file types you specifically want git to ignore
when tracking: simply list them in the .gitignore file in the top level of the
repository.