Git
From PHASTA Wiki
Introduction
Git is a distributed revision control system (VCS), designed to work well with highly non-linear workflows. It was initially conceived to support development of the Linux kernel, and has since become the most widely-used VCS for software development.
This page is essentially a cheat-sheet for git commands frequently used by our group.
Basic Git
Basic information to get started with Git can be found at http://rogerdudler.github.io/git-guide/.
- Git repository (repo)
- Is a directory that contains files of the current branch and a hidden
.gitdirectory - Contains an Index in
.gitthat acts as a staging area for all your branches - Contains a HEAD in
.gitthat points to the last commit you made - Your PHASTA repository should always be at
~/git-phasta/phasta
- Create a new repository
-
git init
- Checkout a repository
-
git clone /path/to/repo -
git clone user@host:/path/to/repo
- Add files to be tracked in the git index
-
git add <filename1> <filename2> ... -
git add * -
git add -u(all files updated since last commit)
- Commit changes to HEAD
-
git commit -m "my_username: my comments about this commit"
- Push changes to remote repo
-
git push origin master(pushes themasterbranch to the original/main repo)
- Connect to a remote server
-
git remote add origin <server>(connects<server>to your main, ororiginrepository) -
git remote add myrepo <server>(connects<server>to a named branch calledmybranch) - Note:
<server>could beuser@host:/path/to/repo
- Create and delete branch
-
git checkout -b branch_name(creates and checkout branch) -
git checkout master(checkout master branch) -
git branch -d branch_name(delete branch we just created)
- Push branch to a remote
-
git push origin branch(pushes from your mainoriginrepo)
- Update and merge
-
git pull(so all branches are up-to-date for the merge) -
git merge origin/master(merges current branch withorigin'smasterbranch) - If a merge has conflicts, you need to edit the conflict files provided by Git, and then mark them as merged with
git add
- Compare code in different branches
-
git diff <branch_a> <branch_b>
- View history of commits
-
git log -
git log --author=foo -
git log --pretty=oneline(show each commit as one line) -
git log --graph --oneline --decorate --all(ASCII tree of branches, commits, merges) -
git log --name-status(show which files changed in each commit) -
git log --help(more information)
Advanced Git
Git Prompt Statement (PS1)
- Acquire
git-prompt.shfrom https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh, and save it somewhere like~/.git-prompt.sh - Edit your
~/.bashrcto saysource ~/.git-prompt.shbefore the lines that set yourPS1 - Modify your
PS1string to include$(__git_ps1 "(%s)")where you want the current branch to appear in your prompt