Git Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Tag: wikieditor
 
(13 intermediate revisions by 2 users not shown)
Line 22: Line 22:


===Unstage a file or files===
===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.
This will unstage the file, but edits that have been made to the file will remain unchanged.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git reset [path]
$ git reset [path]
Line 35: Line 47:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git rm --cached [path]
$ git rm --cached [path]
</syntaxhighlight>
To remove untracked files and/or directories, e.g. new files:
<syntaxhighlight lang="bash">
$ git clean -fd
</syntaxhighlight>
</syntaxhighlight>


Line 71: Line 89:
* 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.
* 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==
== Commmiting changes ==
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git co myBranch
$ git co myBranch
Line 82: Line 101:


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'.
=== 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 ==
== 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
Line 91: Line 119:


=== 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]
Line 96: Line 125:


=== View the files touched between two commits ===  
=== View the files touched between two commits ===  
Where you include just enough of SHA1 and SHA2 to identify the commits (usually about 4 characters).
Where you include just enough of SHA1 and SHA2 to identify the commits (usually about 4 characters).
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 101: Line 131:
</syntaxhighlight>
</syntaxhighlight>


=== View all files touched by all the commits in a single branch ===  
=== 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">
<syntaxhighlight lang="bash">
$ git checkout <branch>
$ git show --pretty="format:" --name-only bd61ad98
$ git log --name-only master
index.html
javascript/application.js
javascript/ie6.js
</syntaxhighlight>
</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 ===  
=== View all commits for a single file ===  
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git log --oneline <path>
$ git log --oneline <path>
Line 113: Line 169:


=== 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>
Line 118: Line 175:


=== Create a patch file for work done in a git branch ===  
=== 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.)
 
To get all the changes after a specific commit:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git diff -p <branch1>...<branch2>  
$ git diff -p <commit_string> > <path/to/patchfile.patch>
</syntaxhighlight>
</syntaxhighlight>


==Managing files==
Or between two specific commits (the newer one goes first):
* Rename a file:
 
<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">
<syntaxhighlight lang="bash">
$ git mv <src_name> <dst_name>
$ git mv <src_name> <dst_name>
Line 131: Line 210:
== Stashing files ==
== Stashing files ==


* Stash changes during a commit or checkout:
=== Stash changes during a commit or checkout ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash
$ git stash
</syntaxhighlight>
</syntaxhighlight>


* Get a list of stashed files
=== Get a list of stashed files ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash list
$ git stash list
</syntaxhighlight>
</syntaxhighlight>


* Apply the latest stash:
=== Apply the latest stash ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash apply
$ git stash apply
</syntaxhighlight>
</syntaxhighlight>


* Apply the stashes farther down on the stack:
=== Apply the stashes farther down on the stack ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ git stash apply --<index>
$ git stash apply --<index>
</syntaxhighlight>
</syntaxhighlight>


[[Category:GIT]]
== Copying local changes ==
[[Category:Web Development]]
 
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]]

Latest revision as of 17:20, 21 April 2024

Overview[edit]

Git cheatsheet.

Staging files[edit]

Stage a file or files[edit]

$ git add [path]

Stage all files[edit]

What this won't do is stage deletes.

$ git add ./

To stage everything including deleted files:

$ git add -A

Unstaging and reverting[edit]

Unstage a file or files[edit]

To remove any local changes and revert a repo to its previous state: [1]

$ git restore

# or, older option

$ git checkout

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]

To remove untracked files and/or directories, e.g. new files:

$ git clean -fd

Reverting file edits[edit]

$ git checkout HEAD [path]

Syncing a repo with subsequent changes to the master[edit]

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[edit]

$ 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'.

Maintain different .gitignore files for development/production[edit]

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.[2]

I haven't attempted to work out the details of how this could work, but it seems promising.

Reports and diffs[edit]

View all branches and commit messages:[edit]

$ git log --oneline

View commit messages for a single branch:[edit]

$ git log --oneline [branchname]

View the files touched between two commits[edit]

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 modified by the commits in a single branch[edit]

$ git diff --name-status master..<branch>

List all the files in a single commit[edit]

One way (preferred):

$ git diff-tree --no-commit-id --name-only -r bd61ad98
index.html
javascript/application.js
javascript/ie6.js

Another way:

$ git show --pretty="format:" --name-only bd61ad98
index.html
javascript/application.js
javascript/ie6.js

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[edit]

$ git log --oneline <path>

View changes in a single file between two branches[edit]

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

Create a patch file for work done in a git branch[edit]

To get all the changes after a specific commit:

$ git diff -p <commit_string> > <path/to/patchfile.patch>

Or between two specific commits (the newer one goes first):

$ git diff -p <newer_commit_string> <older_commit_string> > <path/to/patchfile.patch>

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

$ git diff -p <branch1>...<branch2> > <path/to/patchfile.patch>

Applying a patch created with "git diff"[edit]

$ patch -p1 < <path/to/patchfile.patch>

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[edit]

Rename a file[edit]

$ git mv <src_name> <dst_name>

Stashing files[edit]

Stash changes during a commit or checkout[edit]

$ git stash

Get a list of stashed files[edit]

$ git stash list

Apply the latest stash[edit]

$ git stash apply

Apply the stashes farther down on the stack[edit]

$ git stash apply --<index>

Copying local changes[edit]

To copy local edits that haven't been committed to another computer:

On the computer where edits have been made:

$ git diff > my-patch-file.patch

Copy those changes to the remote computer, and on the remote computer run:

$ git apply ./my-patch-file.patch