Or: more than you wanted to know, but just touching the surface
Issues and Pull Requests
Ignoring Files with .gitignore
bare repositories
git checkout
)git reset
and git revert
git rebase
git rebase -i
git reflog
git config --list
user.email=amueller@nyu.edu user.name=Andreas Mueller core.editor=vim core.pager= push.default=simple pager.grep=false color.ui=auto
mkdir myrepo
cd myrepo
git init
Initialized empty Git repository in /home/andy/advanced_git_tmp/myrepo/.git/
ls -a
. .. .git
git status
On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
echo "first line in file myfile" > myfile.txt
git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
myfile.txt
nothing added to commit but untracked files present (use "git add" to track)
git add myfile.txt
git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: myfile.txt
git commit -m "added a first line to my file"
git status
[master (root-commit) 7e4d620] added a first line to my file 1 file changed, 1 insertion(+) create mode 100644 myfile.txt On branch master nothing to commit, working directory clean
git log
commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d
Author: Andreas Mueller <amueller@nyu.edu>
Date: Tue Mar 29 11:32:47 2016 -0400
added a first line to my file
echo "second line in myfile is not much more interesting" >> myfile.txt
git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.txt
no changes added to commit (use "git add" and/or "git commit -a")
git add myfile.txt
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: myfile.txt
git commit -m "I added a second line"
git log
[master 344a292] I added a second line 1 file changed, 1 insertion(+) commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
echo "omg a third line" >> myfile.txt
cat myfile.txt
fist line in file myfile second line in myfile is not much more interesting omg a third line
Reverting to the last commit:
git checkout myfile.txt
cat myfile.txt
fist line in file myfile second line in myfile is not much more interesting
Reverting to a previous commit:
git checkout HEAD~1 myfile.txt
cat myfile.txt
fist line in file myfile
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: myfile.txt
git commit -m "checked out myfile from first commit"
git log
[master 59d540c] checked out myfile from first commit 1 file changed, 1 deletion(-) commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
Compare all files to the previous commit (HEAD "minus" one).
git diff HEAD~1
diff --git a/myfile.txt b/myfile.txt index d6cb53f..dad84b7 100644 --- a/myfile.txt +++ b/myfile.txt @@ -1,2 +1 @@ fist line in file myfile -second line in myfile is not much more interesting
Compare only myfile.txt
to the previous commit.
git diff HEAD~1 myfile.txt
diff --git a/myfile.txt b/myfile.txt index d6cb53f..dad84b7 100644 --- a/myfile.txt +++ b/myfile.txt @@ -1,2 +1 @@ fist line in file myfile -second line in myfile is not much more interesting
GitHub is a way to host and share repos, but also a social network. GitHub makes it easy to contribute to existing repositories.
To work on a project with a project partner you:
pull
from your repopush
their changesTo work on someone else's project you:
pull
your changes into the main project - the pull requestAKA "the simplified workflow"
touch stupidtempfile.swp
touch whycompiling.pyc
git status
On branch master Untracked files: (use "git add <file>..." to include in what will be committed) stupidtempfile.swp whycompiling.pyc nothing added to commit but untracked files present (use "git add" to track)
Each line in .gitignore
specifies one (kind of) file to ignore:
echo "*.swp" >> .gitignore
echo "*.pyc" >> .gitignore
ls -a
. .. .git .gitignore myfile.txt stupidtempfile.swp whycompiling.pyc
Ignored files will not show up in git status
and will not be added when doing git add --all
git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
awesome_content.txt
you want to commit.local_notes.txt
that you don't want to track.git status
local_notes.txt
to .gitignore
git status
local_notes.txt
should not show up in the status.awesome_content.txt
.local_notes.txt
..gitignore
?pwd
/home/andy/advanced_git_tmp/myrepo
cd ..
mkdir cloned_repo
cd cloned_repo
I can clone a local directory!
git clone ../myrepo
Cloning into 'myrepo'... done.
tree ..
.. ├── cloned_repo │  └── myrepo │  └── myfile.txt └── myrepo ├── myfile.txt ├── stupidtempfile.swp └── whycompiling.pyc 3 directories, 4 files
cd myrepo
ls
myfile.txt
git remote -v
origin /home/andy/advanced_git_tmp/cloned_repo/../myrepo (fetch) origin /home/andy/advanced_git_tmp/cloned_repo/../myrepo (push)
git log
commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
Change something in the original repo:
cd ../../myrepo
cat myfile.txt
fist line in file myfile
echo "another line committed to the original repo" >> myfile.txt
git add myfile.txt
git commit -m "new commit in the original repo"
[master 18a3a33] new commit in the original repo 1 file changed, 1 insertion(+)
git log
commit 18a3a33467da956c792681a283effcd7c72d1864 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:54 2016 -0400 new commit in the original repo commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
Update the cloned repo ...
cd ../cloned_repo/myrepo
git pull origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/andy/advanced_git_tmp/cloned_repo/../myrepo
59d540c..18a3a33 master -> origin/master
Updating 59d540c..18a3a33
Fast-forward
myfile.txt | 1 +
1 file changed, 1 insertion(+)
git log
commit 18a3a33467da956c792681a283effcd7c72d1864 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:54 2016 -0400 new commit in the original repo commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
Change something in the cloned repo:
echo "new line in cloned repo" >> myfile.txt
git add myfile.txt
git commit -m "new line in cloned repo"
[master b84d28d] new line in cloned repo 1 file changed, 1 insertion(+)
But I can't push to the original repo. (Someone might have edited the files but not checked them in)
git push origin
true
Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable to remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into remote: error: its current branch; however, this is not recommended unless you remote: error: arranged to update its work tree to match what you pushed in some remote: error: other way. remote: error: remote: error: To squelch this message and still keep the default behaviour, set remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To /home/andy/advanced_git_tmp/cloned_repo/../myrepo ! [remote rejected] master -> master (branch is currently checked out) error: failed to push some refs to '/home/andy/advanced_git_tmp/cloned_repo/../myrepo'
bare
repositories¶Allow pushing to them, but not directly editing them. They are used only as remotes.
cd ../..
ls
cloned_repo myrepo
mkdir bare_repo
cd bare_repo
git init --bare
Initialized empty Git repository in /home/andy/advanced_git_tmp/bare_repo/
Bare repos don't look like the content of the repository:
ls
branches config description HEAD hooks info objects refs
Adding the bare
repository as a remote to the existing repository myrepo
:
cd ../myrepo
git remote add new_bare ../bare_repo
git remote -v
new_bare ../bare_repo (fetch) new_bare ../bare_repo (push)
Pushing master of myrepo
to the bare_repo
:
git push new_bare master
Counting objects: 10, done. Delta compression using up to 4 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (10/10), 907 bytes | 0 bytes/s, done. Total 10 (delta 1), reused 0 (delta 0) To ../bare_repo * [new branch] master -> master
Add bare_repo
as a remote in the cloned repo:
cd ../cloned_repo/myrepo
git remote add new_bare ../../bare_repo
Push the new changes we made in the clone to the bare repo:
git push new_bare master
Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To ../../bare_repo 18a3a33..b84d28d master -> master
Pull the changes from the clone to the original repo via the bare repo:
cd ../../myrepo
git pull new_bare master
From ../bare_repo
* branch master -> FETCH_HEAD
18a3a33..b84d28d master -> new_bare/master
Updating 18a3a33..b84d28d
Fast-forward
myfile.txt | 1 +
1 file changed, 1 insertion(+)
git log
commit b84d28dff6036a3f958752576dac5e9ea209598d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:55 2016 -0400 new line in cloned repo commit 18a3a33467da956c792681a283effcd7c72d1864 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:54 2016 -0400 new commit in the original repo commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
clone
the bare repo.Remove all files:
rm *
ls
Get them back!
From the last commit:
git reset --hard
HEAD is now at b84d28d new line in cloned repo
From any commit or remote:
git reset --hard new_bare/master
HEAD is now at b84d28d new line in cloned repo
The last line rescues you after screwing up your local copy.
git rm
, not git add
to stage that)git reset --hard
to your remote repository.Allow parallel lines of development.
Create new branches with git checkout -b
git checkout -b new_branch_with_descriptive_name
Switched to a new branch 'new_branch_with_descriptive_name'
echo "new stuff that is reeeeaaally cool and maybe even works" > myfile.txt
git add myfile.txt
git commit -m "hurray, new stuff."
[new_branch_with_descriptive_name 3a2169a] hurray, new stuff. 1 file changed, 1 insertion(+), 3 deletions(-)
git log
commit 3a2169aab7f3cf48b2926b35f19e03fbf5cfe3fb Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:59 2016 -0400 hurray, new stuff. commit b84d28dff6036a3f958752576dac5e9ea209598d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:55 2016 -0400 new line in cloned repo commit 18a3a33467da956c792681a283effcd7c72d1864 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:54 2016 -0400 new commit in the original repo commit 59d540c6d105d3d9b11ec7f0b2e662fc73c43532 Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:49 2016 -0400 checked out myfile from first commit commit 344a292cc5a57060e0767f31aa020a16c8b5600a Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:48 2016 -0400 I added a second line commit 7e4d6208c11e3daa867eff0a9d494e90a7510a1d Author: Andreas Mueller <amueller@nyu.edu> Date: Tue Mar 29 11:32:47 2016 -0400 added a first line to my file
git log --graph --decorate --all --oneline
* 3a2169a (HEAD -> new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (new_bare/master, master) new line in cloned repo * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
master
is at "new line in cloned repo", new_branch_with_descriptive_name
is at "hurray, new stuff"
HEAD
points to new_branch_with_descriptive_name
.
alias nicelog="git log --graph --decorate --all --oneline"
Move to another branch with git checkout
git checkout master
Switched to branch 'master'
git log --graph --decorate --all --oneline
* 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (HEAD -> master, new_bare/master) new line in cloned repo * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
HEAD
points at master
.
The content of myfile.txt
is the one at "new line in cloned repo", not "hurray, new stuff":
cat myfile.txt
fist line in file myfile another line commited to the original repo new line in cloned repo
We add another commit to master
:
echo "add to the old stuff " >> another_file.txt
git add another_file.txt
git commit -m "working on another file on the master"
[master 35e6fa4] working on another file on the master 1 file changed, 1 insertion(+) create mode 100644 another_file.txt
An now we see why it's called branch!
git log --graph --decorate --all --oneline
* 35e6fa4 (HEAD -> master) working on another file on the master | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
ls
another_file.txt myfile.txt
Check out the "new_branch" again:
git checkout new_branch_with_descriptive_name
Switched to branch 'new_branch_with_descriptive_name'
another_file.txt
has disappeared:
ls
myfile.txt
homer_is_in.txt
to "australia" and commit it.Or reuniting the lines of development.
git checkout master
Switched to branch 'master'
Merge branches with git merge
. Provide a message with -m
.
git merge new_branch_with_descriptive_name -m "merge descriptive branch with cool feature"
Merge made by the 'recursive' strategy. myfile.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
git merge --abort
is your friend if there are many conflicts and you start to panic.
After a successful merge, all is clean:
git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
Changes from both branches are there:
cat myfile.txt
new stuff that is reeeeaaally cool and maybe even works
The merge created a new commit:
git log --graph --decorate --all --oneline
* 266bd72 (HEAD -> master) merge descriptive branch with cool feature |\ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * | 35e6fa4 working on another file on the master |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
homer_is_in.txt
to "doughnut plant" and commit it.america
into master
and resolve the conflict so that the file contains "america".git checkout -b branch_started_further_back HEAD~3
Switched to a new branch 'branch_started_further_back'
git log --graph --decorate --all --oneline
* 266bd72 (master) merge descriptive branch with cool feature |\ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * | 35e6fa4 working on another file on the master |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 (HEAD -> branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
git checkout -b another_new_branch
Switched to a new branch 'another_new_branch'
git log --graph --decorate --all --oneline
* 266bd72 (master) merge descriptive branch with cool feature |\ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * | 35e6fa4 working on another file on the master |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 (HEAD -> another_new_branch, branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
We can't delete the branch we're on so we go back to master:
git checkout master
Already on 'master'
git branch -d another_new_branch
Deleted branch another_new_branch (was 18a3a33).
america
australia
So far we have:
git commit
), branched (git checkout -b
) and merged branches (git merge
).git checkout
)git checkout -b <branchname> <commit>
)Most commands impact the DAG, and some of the Index, staging area and working tree.
git revert
and git reset
git checkout master
git log --graph --decorate --all --oneline
Already on 'master' * 266bd72 (HEAD -> master) merge descriptive branch with cool feature |\ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * | 35e6fa4 working on another file on the master |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
ls
another_file.txt myfile.txt
git revert HEAD~1 --no-edit
[master 64c8100] Revert "working on another file on the master" 1 file changed, 1 deletion(-) delete mode 100644 another_file.txt
git log --graph --decorate --all --oneline
* 64c8100 (HEAD -> master) Revert "working on another file on the master" * 266bd72 merge descriptive branch with cool feature |\ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * | 35e6fa4 working on another file on the master |/ * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
ls
myfile.txt
america
into master
.git reset
¶Moving head when on a branch (not headless) also moves where the branch is pointing!
git reset branch_started_further_back
Unstaged changes after reset: M myfile.txt
git log --graph --decorate --all --oneline
* 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (new_bare/master) new line in cloned repo * 18a3a33 (HEAD -> master, branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
git status
On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: myfile.txt Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore no changes added to commit (use "git add" and/or "git commit -a")
cat myfile.txt
new stuff that is reeeeaaally cool and maybe even works
git add myfile.txt
git commit -m "new stuff from the feature branch that I did stuff with"
[master 00cf774] new stuff from the feature branch that I did stuff with 1 file changed, 1 insertion(+), 2 deletions(-)
git log --graph --decorate --all --oneline
* 00cf774 (HEAD -> master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
--hard
fixes (aka destroys) everything!
git checkout -b branch_well_move_back
Switched to a new branch 'branch_well_move_back'
git log --graph --decorate --all --oneline
* 00cf774 (HEAD -> branch_well_move_back, master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 I added a second line * 7e4d620 added a first line to my file
git reset --hard HEAD~3
HEAD is now at 344a292 I added a second line
git status
On branch branch_well_move_back
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
git log --graph --decorate --all --oneline
* 00cf774 (master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (HEAD -> branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
way_back_machine
.rebase only works on the DAG (and moves HEAD)
rebase basically does every possible thing with the DAG
changing the tree means changing hashes!
"simple" form:
git rebase basebranch otherbranch
equivalently
git checkout otherbranch
git rebase basebranch
Put everything in otherbranch that is not in basebranch on top of otherbranch. (compute common root, cut of there)
git log --graph --decorate --all --oneline
* 00cf774 (master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 (branch_started_further_back) new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (HEAD -> branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
git checkout branch_started_further_back
Switched to branch 'branch_started_further_back'
echo "Change in back branch" > new_file_in_back_branch.txt
git add new_file_in_back_branch.txt
git commit -m "added a file in the branch_started_further_back"
echo "more lines" >> new_file_in_back_branch.txt
git add new_file_in_back_branch.txt
git commit -m "second commit in the branch_started_further_back"
[branch_started_further_back 50d5233] added a file in the branch_started_further_back 1 file changed, 1 insertion(+) create mode 100644 new_file_in_back_branch.txt [branch_started_further_back 12be701] second commit in the branch_started_further_back 1 file changed, 1 insertion(+)
git log --graph --decorate --all --oneline
* 12be701 (HEAD -> branch_started_further_back) second commit in the branch_started_further_back * 50d5233 added a file in the branch_started_further_back | * 00cf774 (master) new stuff from the feature branch that I did stuff with |/ | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
git rebase master
First, rewinding head to replay your work on top of it... Applying: added a file in the branch_started_further_back Applying: second commit in the branch_started_further_back
git log --graph --decorate --all --oneline
* 59d2f69 (HEAD -> branch_started_further_back) second commit in the branch_started_further_back * 0258c9e added a file in the branch_started_further_back * 00cf774 (master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
using --onto
Three parts in rebase:
For the "simple" version 2 and 3 are the same.
For the --onto
version, you can specify 3 separately.
git log --graph --decorate --all --oneline
* 59d2f69 (HEAD -> branch_started_further_back) second commit in the branch_started_further_back * 0258c9e added a file in the branch_started_further_back * 00cf774 (master) new stuff from the feature branch that I did stuff with | * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. | * b84d28d (new_bare/master) new line in cloned repo |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
git rebase master --onto new_branch_with_descriptive_name
First, rewinding head to replay your work on top of it... Applying: added a file in the branch_started_further_back Applying: second commit in the branch_started_further_back
git log --graph --decorate --all --oneline
* 157de4a (HEAD -> branch_started_further_back) second commit in the branch_started_further_back * 08211a4 added a file in the branch_started_further_back * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (new_bare/master) new line in cloned repo | * 00cf774 (master) new stuff from the feature branch that I did stuff with |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
way_back_machine
(from the last exercise) adding a new file.(or you could apply a rusty nail to your retina)
We'll only discuss squashing and skipping. Needs to be done on the command line!
# git rebase -i new_branch_with_descriptive_name
false
git log --graph --decorate --all --oneline
* 4ec5822 (HEAD -> branch_started_further_back) all the changes I ever wanted! * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (new_bare/master) new line in cloned repo | * 00cf774 (master) new stuff from the feature branch that I did stuff with |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
Seek and ye shall find
git reflog -n 10
4ec5822 HEAD@{0}: rebase -i (finish): returning to refs/heads/branch_started_further_back 4ec5822 HEAD@{1}: rebase -i (squash): all the changes I ever wanted! 08211a4 HEAD@{2}: rebase -i (start): checkout new_branch_with_descriptive_name 157de4a HEAD@{3}: rebase finished: returning to refs/heads/branch_started_further_back 157de4a HEAD@{4}: rebase: second commit in the branch_started_further_back 08211a4 HEAD@{5}: rebase: added a file in the branch_started_further_back 3a2169a HEAD@{6}: rebase: checkout new_branch_with_descriptive_name 59d2f69 HEAD@{7}: rebase finished: returning to refs/heads/branch_started_further_back 59d2f69 HEAD@{8}: rebase: second commit in the branch_started_further_back 0258c9e HEAD@{9}: rebase: added a file in the branch_started_further_back
git checkout -b before_interactive_rebase HEAD@{3}
Switched to a new branch 'before_interactive_rebase'
git log --graph --decorate --all --oneline
* 4ec5822 (branch_started_further_back) all the changes I ever wanted! | * 157de4a (HEAD -> before_interactive_rebase) second commit in the branch_started_further_back | * 08211a4 added a file in the branch_started_further_back |/ * 3a2169a (new_branch_with_descriptive_name) hurray, new stuff. * b84d28d (new_bare/master) new line in cloned repo | * 00cf774 (master) new stuff from the feature branch that I did stuff with |/ * 18a3a33 new commit in the original repo * 59d540c checked out myfile from first commit * 344a292 (branch_well_move_back) I added a second line * 7e4d620 added a first line to my file
Remember that first commit in the way_back_machine
branch? It's not in the graph any more, is it?
Get it back with reflog!
(to make the project maintainer love you)
Do it!
git@github.com:amueller/repo_to_fork.git
as a remote to your cloned repo
check out local master. Pull upstream master. This is why you don't want to change master. Ever!