Section | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...
It is best to keep your master branch clean and up-to-date with the project repo. This will provide you with a clean space to compare your "working" code with, as well as a stable location to push changes back up to the project repo.
Code Block |
---|
# list all branches, including remotes
git branch -a
# change branches, in this case switch to master
git checkout master
# make a branch of master, and immediately switch to the branch
# Note: it's best to name your branches after the jira you are working on (e.g. FLUID-xxxx)
git checkout -b FLUID-XXXX
|
...
When you have a branch that is ready to go into the project repo you'll need to merge it into your clean, up-to-date master.
Code Block |
---|
# get the latest changes, where upstream is the project repo
git fetch upstream
# view the changes that are in the project repo that aren't currently in your master
git log upstream/master ^master
# view the changes that are in your master, that aren't in the project repo (should be none)
git log ^upstream/master master
# update your master by merging in the changes from the project repo
# assuming you are already in the master branch
git merge upstream/master
# view the changes that are in master that aren't currently in your branch
git log master ^FLUID-xxxx
# view the changes that are in your branch, that aren't in the master
git log ^master FLUID-xxxx
# merge the branch into master.
# Note: --no-ff forces a merge commit
# Note: --log lists all the commits that are part of the merge (if there are lots of commits you may need to increase the max number --log=n)
git merge FLUID-xxxx --no-ff --log
# push to your public repo, origin, first to make sure things worked
git push origin master
# push to the project repo
git push upstream master
|
...
In git, commit logs are structured a lot like e-mails. The first line is a summary, 50 characters or less. The remainder of the commit log should contain the detailed description of what is being committed.
Code Block |
---|
#summary, it should start with a jira number.
FLUID-xxxx: Adding in feature x to component y
#This is just a mock example, a real commit log would have proper details about the changes in a commit
Feature x adds does such and such a function, which is necessary for component y.
|
...
Lists commits in branch x that aren't in branch y
Code Block |
---|
git log x ^y
|
Lists commits and also shows the diff of the changes
Code Block |
---|
git log -p
|
Diff
(see: git-diff)
Diff of unstaged changes
Code Block |
---|
git diff
|
Diff of staged changes
Code Block |
---|
git diff --cached
|
Diff between two commits
Code Block |
---|
git diff commitHash1 commitHash2
|
Diff between branch x and y
Code Block |
---|
git diff x y |
Diff between a path at a revision and a path in the working tree
Code Block |
---|
git diff rev:path1 path2 |
Resetting working copy
(see: git-reset)
(For a detailed description of all the types of reset available see: http://git-scm.com/2011/07/11/reset.html);
Unstage changes
Code Block |
---|
git reset HEAD
|
Undoes all changes, staged or not, so that the working copy is back to the state of the last commit
Code Block |
---|
git reset HEAD --hard
|
Sets the working copy back to the state of the specified commit
Code Block |
---|
git reset commitHash --hard
|
...
commitHash can be replaced by either a commit hash, tag, or branch name.
You can also use the "~1" at the end of a commit hash to use the 1 before.
This is good when you know the commit with the error.
(See the "Reset with a path" and "Check it out" sections: http://git-scm.com/2011/07/11/reset.html);
Code Block |
---|
git checkout commitHash path/to/file/
|
Another method, which preserves the working directory, is to use git reset
Code Block |
---|
git reset commitHash path/to/file/
|
...
Deletes local branch x
Code Block |
---|
git branch -d x
#to force a branch to delete even when it has unmerged changes
git branch -D x
|
Deletes branch x from remote repo
Code Block |
---|
#note the ":"
git push remoteRepo :x
|
Remove stale branches fetched from the remote at remoteName
Code Block |
---|
git remote prune remoteName
|
...
Useful to temporarily save changes that aren't ready to be committed
Code Block |
---|
git stash
|
Re-apply stashed changes. If more than one set of stashed changes, you can specify which stash to apply.
Code Block |
---|
git stash apply <stash name>
|
List stashed changes
Code Block |
---|
git stash list
|
Clear stash
Code Block |
---|
git stash clear
|