Editing
Git Cookbook
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Overview== Git cheatsheet. ==Staging files== ===Stage a file or files=== <syntaxhighlight lang="bash"> $ git add [path] </syntaxhighlight> ===Stage all files=== What this won't do is stage deletes. <syntaxhighlight lang="bash"> $ git add ./ </syntaxhighlight> To stage everything including deleted files: <syntaxhighlight lang="bash"> $ git add -A </syntaxhighlight> ==Unstaging and reverting== ===Unstage a file or files=== To remove any local changes and revert a repo to its previous state: <ref>[https://stackoverflow.com/a/1146981 How do I revert all local changed] - StackOverflow</ref> <syntaxhighlight lang="bash"> $ git restore # or, older option $ git checkout </syntaxhighlight> This will unstage the file, but edits that have been made to the file will remain unchanged. <syntaxhighlight lang="bash"> $ git reset [path] </syntaxhighlight> Note: <code>git reset</code> as described above will throw this error prior to the initial commit: <pre> fatal: Failed to resolve 'HEAD' as a valid ref. </pre> If files have been staged prior to the initial commit, then <syntaxhighlight lang="bash"> $ git rm --cached [path] </syntaxhighlight> To remove untracked files and/or directories, e.g. new files: <syntaxhighlight lang="bash"> $ git clean -fd </syntaxhighlight> ===Reverting file edits=== <syntaxhighlight lang="bash"> $ git checkout HEAD [path] </syntaxhighlight> ==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: <syntaxhighlight lang="bash"> $ git status -s </syntaxhighlight> * View (unstaged) edits for a specific file: <syntaxhighlight lang="bash"> $ git diff -- [path] </syntaxhighlight> * Switch from the local branch to 'master'. <syntaxhighlight lang="bash"> $ git co master </syntaxhighlight> * Merge the updated local 'master' with the local branch, resolving any conflicts: <syntaxhighlight lang="bash"> $ git branch * master mybranch $ git co mybranch $ git merge master </syntaxhighlight> * See also <code>[http://git-scm.com/book/en/Git-Branching-Rebasing git rebase]</code><br />Which does the same thing as <code>git merge</code> but in a slightly different way that is helpful to maintain a linear set of changes when merging two branches together. == Commmiting changes == <syntaxhighlight lang="bash"> $ 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 </syntaxhighlight> 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'. === Maintain different `.gitignore` files for development/production === '''Goal:''' Push a set of files to production that doesn't include non-public things like sass files, etc. Maintain a "development" branch that has a different version of `.gitignore`, or even a different set of files.<ref>[http://stackoverflow.com/questions/10475273/git-have-different-gitignore-file-for-each-remote Git Have Different .gitignore File for Each Remote] (Stackoverflow)</ref> <p class="alert-warning">I haven't attempted to work out the details of how this could work, but it seems promising.</p> == Reports and diffs == === View all branches and commit messages: === <syntaxhighlight lang="bash"> $ git log --oneline </syntaxhighlight> === View commit messages for a single branch: === <syntaxhighlight lang="bash"> $ git log --oneline [branchname] </syntaxhighlight> === 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"> $ git diff --name-only SHA1 SHA2 </syntaxhighlight> === View all files modified by the commits in a single branch === <syntaxhighlight lang="bash"> $ git diff --name-status master..<branch> </syntaxhighlight> === List all the files in a single commit === One way (preferred): <syntaxhighlight lang="bash"> $ git diff-tree --no-commit-id --name-only -r bd61ad98 index.html javascript/application.js javascript/ie6.js </syntaxhighlight> Another way: <syntaxhighlight lang="bash"> $ git show --pretty="format:" --name-only bd61ad98 index.html javascript/application.js javascript/ie6.js </syntaxhighlight> The `--no-commit-id` suppresses the commit ID output. The `--pretty` argument specifies an empty format string to avoid the cruft at the beginning. The `--name-only` argument shows only the file names that were affected (Thanks Hank). The `-r` argument is to recurse into sub-trees === View all commits for a single file === <syntaxhighlight lang="bash"> $ git log --oneline <path> </syntaxhighlight> === View changes in a single file between two branches === <syntaxhighlight lang="bash"> $ git diff <branch1>...<branch2> -- <path/to/file> </syntaxhighlight> === Create a patch file for work done in a git branch === To get all the changes after a specific commit: <syntaxhighlight lang="bash"> $ git diff -p <commit_string> > <path/to/patchfile.patch> </syntaxhighlight> Or between two specific commits (the newer one goes first): <syntaxhighlight lang="bash"> $ git diff -p <newer_commit_string> <older_commit_string> > <path/to/patchfile.patch> </syntaxhighlight> (To narrow it down to a specific file, see the 'git diff` example above.) <syntaxhighlight lang="bash"> $ git diff -p <branch1>...<branch2> > <path/to/patchfile.patch> </syntaxhighlight> === Applying a patch created with "git diff" === <syntaxhighlight lang="bash"> $ patch -p1 < <path/to/patchfile.patch> </syntaxhighlight> If the directory structure is different, say if you are editing `myapp2/path/to/file_to_edit.py` using changes made to `myapp1/path/to/file_to_edit.py` then enter the new path when prompted by the `patch` program. == Managing files == === Rename a file === <syntaxhighlight lang="bash"> $ git mv <src_name> <dst_name> </syntaxhighlight> == Stashing files == === Stash changes during a commit or checkout === <syntaxhighlight lang="bash"> $ git stash </syntaxhighlight> === Get a list of stashed files === <syntaxhighlight lang="bash"> $ git stash list </syntaxhighlight> === Apply the latest stash === <syntaxhighlight lang="bash"> $ git stash apply </syntaxhighlight> === Apply the stashes farther down on the stack === <syntaxhighlight lang="bash"> $ git stash apply --<index> </syntaxhighlight> == Copying local changes == To copy local edits that haven't been committed to another computer: On the computer where edits have been made: <pre> $ git diff > my-patch-file.patch </pre> Copy those changes to the remote computer, and on the remote computer run: <pre> $ git apply ./my-patch-file.patch </pre> [[Category:Git]] [[Category:Web Development]]
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information