Git Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Line 83: Line 83:
See the [http://wiki.mediabistro.net/index.php?title=Git#Merging_branch_to_preview Mediabistro wiki] for instructions on how to put the changes on 'preview'.
See the [http://wiki.mediabistro.net/index.php?title=Git#Merging_branch_to_preview Mediabistro wiki] for instructions on how to put the changes on 'preview'.


==Reports and diffs==
== Reports and diffs ==
* View all branches and commit messages:
 
=== View all branches and commit messages: ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git log --oneline
$ git log --oneline
</syntaxhighlight>
</syntaxhighlight>
* View commit messages for a single branch:
 
=== View commit messages for a single branch: ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git log --oneline [branchname]
$ git log --oneline [branchname]
</syntaxhighlight>
</syntaxhighlight>
* View the files touched between two commits<br />''Where you include just enough of SHA1 and SHA2 to identify the commits (usually about 4 characters).''
 
=== View the files touched between two commits ===
Where you include just enough of SHA1 and SHA2 to identify the commits (usually about 4 characters).
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git diff --name-only SHA1 SHA2
$ git diff --name-only SHA1 SHA2
</syntaxhighlight>
</syntaxhighlight>
* View all files touched by all the commits in a single branch
 
=== View all files touched by all the commits in a single branch ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git checkout <branch>
$ git checkout <branch>
$ git log --name-only master
$ git log --name-only master
</syntaxhighlight>
</syntaxhighlight>
* View all commits for a single file
 
=== View all commits for a single file ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git log --oneline <path>
$ git log --oneline <path>
</syntaxhighlight>
</syntaxhighlight>
* View changes in a single file between two branches
 
=== View changes in a single file between two branches ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git diff <branch1>...<branch2> -- <path/to/file>
$ git diff <branch1>...<branch2> -- <path/to/file>
</syntaxhighlight>
</syntaxhighlight>
* Create a patch file for work done in a git branch<br />''(To narrow it down to a specific file, see the `git diff` example above.)''
 
=== Create a patch file for work done in a git branch ===
(To narrow it down to a specific file, see the `git diff` example above.)
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git diff -p <branch1>...<branch2>  
$ git diff -p <branch1>...<branch2>  

Revision as of 19:41, 3 April 2013

Overview

Git cheatsheet.

Staging files

Stage a file or files

$ git add [path]

Stage all files

What this won't do is stage deletes.

$ git add ./

To stage everything including deleted files:

$ git add -A

Unstaging and reverting

Unstage a file or files

This will unstage the file, but edits that have been made to the file will remain unchanged.

$ git reset [path]

Note: git reset as described above will throw this error prior to the initial commit:

fatal: Failed to resolve 'HEAD' as a valid ref.

If files have been staged prior to the initial commit, then

$ git rm --cached [path]

Reverting file edits

$ git checkout HEAD [path]

Syncing a repo with subsequent changes to the master

Scenario: Create a branch, make edits. In the meantime other work is being done by other members of the team. The time comics to push your changes out. The goal is to merge their changes into yours locally then push it all out.

  • See what files have been touched:
$ git status -s
  • View (unstaged) edits for a specific file:
$ git diff -- [path]
  • Switch from the local branch to 'master'.
$ git co master
  • Merge the updated local 'master' with the local branch, resolving any conflicts:
$ git branch
* master
  mybranch
$ git co mybranch
$ git merge master
  • See also git rebase
    Which does the same thing as git merge but in a slightly different way that is helpful to maintain a linear set of changes when merging two branches together.

Commmiting changes

$ git co myBranch
$ git status -s
  # add any files that need to be added to the commit
$ git commit -m 'commit message'
  # the "commit message" is required
$ git push origin myBranch

See the Mediabistro wiki for instructions on how to put the changes on 'preview'.

Reports and diffs

View all branches and commit messages:

$ git log --oneline

View commit messages for a single branch:

$ git log --oneline [branchname]

View the files touched between two commits

Where you include just enough of SHA1 and SHA2 to identify the commits (usually about 4 characters).

$ git diff --name-only SHA1 SHA2

View all files touched by all the commits in a single branch

$ git checkout <branch>
$ git log --name-only master

View all commits for a single file

$ git log --oneline <path>

View changes in a single file between two branches

$ git diff <branch1>...<branch2> -- <path/to/file>

Create a patch file for work done in a git branch

(To narrow it down to a specific file, see the git diff example above.)

$ git diff -p <branch1>...<branch2>

Managing files

  • Rename a file:
$ git mv <src_name> <dst_name>

Stashing files

  • Stash changes during a commit or checkout:
$ git stash
  • Get a list of stashed files
$ git stash list
  • Apply the latest stash:
$ git stash apply
  • Apply the stashes farther down on the stack:
$ git stash apply --<index>