Difference between revisions of "Working with git"
(→Undo a commit that hasn't propagated yet, keeping the working tree the same:) |
(→Revert a commit (i.e. create another commit), dropping all changes that it made:) |
||
Line 90: | Line 90: | ||
==Revert a commit (i.e. create another commit), dropping all changes that it made:== | ==Revert a commit (i.e. create another commit), dropping all changes that it made:== | ||
− | + | ||
+ | git revert <commit> | ||
=Making local changes= | =Making local changes= |
Revision as of 06:12, 24 November 2011
Contents
- 1 Interacting with a remote repository
- 1.1 Check out a repository:
- 1.2 Find out what changes were made to a remote repository since I last pulled:
- 1.3 Update from a repository, if there are no local uncommitted changes:
- 1.4 Update from a repository, if there are local changes:
- 1.5 Push to a repository:
- 1.6 Create a branch containing the current snapshop of the local repository, including all uncommitted changes, and switch to this branch
- 1.7 List all branches
- 1.8 Find out on which branch I am:
- 1.9 Switch to a branch, if there are no uncommitted changes
- 2 Committing
- 2.1 Commit a change:
- 2.2 Modify a commit that hasn't propagated yet, keeping the working tree the same:
- 2.3 Undo a commit that hasn't propagated yet, keeping the working tree the same:
- 2.4 Revert a commit (i.e. create another commit) that may have propagated already, keeping all the changes as local modifications:
- 2.5 Revert a commit (i.e. create another commit), dropping all changes that it made:
- 3 Making local changes
- 3.1 Look at local modifications:
- 3.2 Revert an uncommitted change to a file:
- 3.3 Add a file to the repository:
- 3.4 Remove a file from the repository:
- 3.5 Undo adding a file to the repository:
- 3.6 Undo removing a file from the repository, recreating the file manually:
- 3.7 Undo removing a file from the repository, resurrecting the file in the repository:
Interacting with a remote repository
This page describes solution to typical needs with git. If you are new to git, please read any of the fine tutorials to learn about the concepts that git offers, and how one would typically work with git.
Later, if you get stuck, or if you maneuvered yourself into a corner, you can come back here to find solutions to particular problems. That is, this page is not organised by "how do I do thing that are natural for git", but rather "how do I map things I need to do onto actions that are natural for git".
This page is split into three parts:
- Interacting with a remote repository (pull, push, merge)
- Local commits
- Working (adding/removing files, undoing changes)
Check out a repository:
git clone <remote-repo-url> <local-repo-name> git clone carpetgit@carpetcode.org:McLachlan McLachlan
Find out what changes were made to a remote repository since I last pulled:
git fetch git log HEAD..origin
Update from a repository, if there are no local uncommitted changes:
git pull --rebase
Update from a repository, if there are local changes:
Save all local changes:
git stash
Then update as described above:
git pull --rebase
Re-apply all local changes:
git stash pop
If there are no conflicts, you are done. If there are conflicts, handle them, then:
git stash drop
since "stash pop" doesn't pop the stash if there are conflicts. "git stash list" shows all stashes. View the content of a stash with "git stash show <stash>".
Push to a repository:
Ensure all remote changes have been pulled (see above), then:
git push
Create a branch containing the current snapshop of the local repository, including all uncommitted changes, and switch to this branch
Create a new branch pointing to the current commit:
git branch <newbranchname>
Check out the new branch:
git checkout <newbranchname>
List all branches
git branch -a
??? How to find out whether there are accidental local branches without remote counterpart?
Find out on which branch I am:
git branch
The branch marked with a star "*" is the current branch.
Switch to a branch, if there are no uncommitted changes
git checkout <branch-name>
??? Does this require a better syntax so that branch names and directory names are not confused?
Committing
Commit a change:
Commit certain files:
git commit <list-of-files-to-commit>
Commit all modified files which are known to git (i.e. have been added with "git add"):
git commit -a
Modify a commit that hasn't propagated yet, keeping the working tree the same:
git commit --amend <files>
This is the same as "git commit", except it rewrites the last commit instead of creating a new one. <files> lists any files with new changes that you want to include. You can also say "-a" to mean all registered files.
Undo a commit that hasn't propagated yet, keeping the working tree the same:
git reset HEAD~1
This deletes the last commit but leaves the changes from the current commit in the working tree. HEAD~1 means one commit before the latest.
Revert a commit (i.e. create another commit) that may have propagated already, keeping all the changes as local modifications:
???
Revert a commit (i.e. create another commit), dropping all changes that it made:
git revert <commit>
Making local changes
Look at local modifications:
List modified files:
git status
Get a diff between the local repo and the working tree ("which changes did I make since the last commit?"):
git diff <filename> ??? need more options
Get a diff between the local repo and the staging area ("which changes did I mark for committing?"):
git diff <filename> ??? need more options
Get a diff between the staging area and the working tree ("which of my changes would not be committed?"):
git diff <filename> ??? need more options
Get a diff between the remote repository and working tree ("what would a 'git pull' do?"):
???
Revert an uncommitted change to a file:
git checkout -- <file>
Add a file to the repository:
First create file locally, then:
git add <list-of-files-to-add>
Note: This does not mark the file for adding! Instead, this adds file at the time of "git add"; later modifications are ignored. Then commit.
Remove a file from the repository:
Delete file locally
git rm <list-of-files-that-were-removed>
(??? Is there a way to let git determine automatically which files were removed?) (??? is this correct? Doesn't "git rm" only remove it from the staging area? Are there other options needed?) Then commit.
Undo adding a file to the repository:
Delete file locally again, then
git reset HEAD <file>
Undo removing a file from the repository, recreating the file manually:
Recreate file locally, then:
git reset HEAD <file>
Undo removing a file from the repository, resurrecting the file in the repository:
??? (probably a combination of "checkout" and "reset")