You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/02-git-basics/1-git-basics.asc
+70-54Lines changed: 70 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -874,7 +874,7 @@ In <<limit_options>> we’ll list these and a few other common options for your
874
874
| `--author` | Only show commits in which the author entry matches the specified string.
875
875
| `--committer` | Only show commits in which the committer entry matches the specified string.
876
876
| `--grep` | Only show commits with a commit message containing the string
877
-
| `-S ` | Only show commits adding or removing code matching the string
877
+
| `-S` | Only show commits adding or removing code matching the string
878
878
|================================
879
879
880
880
For example, if you want to see which commits modifying test files in the Git source code history were committed by Junio Hamano and were not merges in the month of October 2008, you can run something like this:
@@ -971,6 +971,13 @@ Changes not staged for commit:
971
971
The command is a bit strange, but it works.
972
972
The `benchmarks.rb` file is modified but once again unstaged.
973
973
974
+
[NOTE]
975
+
=====
976
+
While `git reset` _can_ be a dangerous command if you call it with `--hard`, in this instance the file in your working directory is not touched. Calling `git reset` without an option is not dangerous - it only touches your staging area.
977
+
=====
978
+
979
+
For now this magic invocation is all you need to know about the `git reset` command. We'll go into much more detail about what `reset` does and how to master it to do really interesting things in <<_git_tools_reset>>.
980
+
974
981
==== Unmodifying a Modified File
975
982
976
983
What if you realize that you don’t want to keep your changes to the `benchmarks.rb` file?
@@ -1003,26 +1010,27 @@ Changes to be committed:
1003
1010
----
1004
1011
1005
1012
You can see that the changes have been reverted.
1006
-
You should also realize that this is a dangerous command: any changes you made to that file are gone – you just copied another file over it.
1013
+
1014
+
[IMPORTANT]
1015
+
=====
1016
+
It's important to understand that `git checkout -- [file]` is a dangerous command. Any changes you made to that file are gone – you just copied another file over it.
1007
1017
Don’t ever use this command unless you absolutely know that you don’t want the file.
1008
-
If you just need to get it out of the way, we’ll go over stashing and branching in the next chapter; these are generally better ways to go.
1018
+
=====
1019
+
1020
+
If you would like to keep the changes you've made to that file but still need to get it out of the way for now, we’ll go over stashing and branching in the next chapter; these are generally better ways to go.
1009
1021
1010
-
Remember, anything that is committed in Git can almost always be recovered.
1022
+
Remember, anything that is __committed__ in Git can almost always be recovered.
1011
1023
Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see <<data_recovery>> for data recovery).
1012
1024
However, anything you lose that was never committed is likely never to be seen again.
1013
1025
1014
-
==== Undoing All Changes
1015
-
1016
-
**TODO**
1017
-
1018
1026
=== Working with Remotes
1019
1027
1020
1028
To be able to collaborate on any Git project, you need to know how to manage your remote repositories.
1021
1029
Remote repositories are versions of your project that are hosted on the Internet or network somewhere.
1022
1030
You can have several of them, each of which generally is either read-only or read/write for you.
1023
1031
Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.
1024
1032
Managing remote repositories includes knowing how to add remote repositories, remove remotes that are no longer valid, manage various remote branches and define them as being tracked or not, and more.
1025
-
In this section, we’ll cover these remote-management skills.
1033
+
In this section, we’ll cover some of these remote-management skills.
1026
1034
1027
1035
==== Showing Your Remotes
1028
1036
@@ -1044,29 +1052,36 @@ $ git remote
1044
1052
origin
1045
1053
----
1046
1054
1047
-
You can also specify `-v`, which shows you the URL that Git has stored for the shortname to be expanded to:
1055
+
You can also specify `-v`, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote:
1048
1056
1049
1057
[source,shell]
1050
1058
----
1051
1059
$ git remote -v
1052
-
origin https://github.com/schacon/ticgit.git
1060
+
origin https://github.com/schacon/ticgit (fetch)
1061
+
origin https://github.com/schacon/ticgit (push)
1053
1062
----
1054
1063
1055
1064
If you have more than one remote, the command lists them all.
1056
-
For example, my Grit repository looks something like this.
1065
+
For example, a repository with multiple remotes for working with several collaborators might look something like this.
This means we can pull contributions from any of these users pretty easily.
1083
+
This means we can pull contributions from any of these users pretty easily. We may additionally have permission to push to one or more of these, though we can't tell that here.
1084
+
1070
1085
Notice that these remotes use a variety of protocols; we’ll cover why more about this in <<_git_on_the_server>>.
1071
1086
1072
1087
==== Adding Remote Repositories
@@ -1078,12 +1093,12 @@ To add a new remote Git repository as a shortname you can reference easily, run
Now you can use the string `pb` on the command line in lieu of the whole URL.
@@ -1100,7 +1115,10 @@ From https://github.com/paulboone/ticgit
1100
1115
* [new branch] master -> pb/master
1101
1116
* [new branch] ticgit -> pb/ticgit
1102
1117
----
1103
-
Paul’s master branch is accessible locally as `pb/master` – you can merge it into one of your branches, or you can check out a local branch at that point if you want to inspect it.
1118
+
1119
+
Paul’s master branch is now accessible locally as `pb/master` – you can merge it into one of your branches, or you can check out a local branch at that point if you want to inspect it.
1120
+
(We’ll go over what branches are and how to use them in much more detail in <<_git_branching>>.)
1121
+
1104
1122
1105
1123
==== Fetching and Pulling from Your Remotes
1106
1124
@@ -1113,11 +1131,10 @@ $ git fetch [remote-name]
1113
1131
1114
1132
The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet.
1115
1133
After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.
1116
-
(We’ll go over what branches are and how to use them in much more detail in <<_git_branching>>.)
1117
1134
1118
1135
If you clone a repository, the command automatically adds that remote repository under the name origin.
1119
1136
So, `git fetch origin` fetches any new work that has been pushed to that server since you cloned (or last fetched from) it.
1120
-
It’s important to note that the fetch command pulls the data to your local repository – it doesn’t automatically merge it with any of your work or modify what you’re currently working on.
1137
+
It’s important to note that the `git fetch` command pulls the data to your local repository – it doesn’t automatically merge it with any of your work or modify what you’re currently working on.
1121
1138
You have to merge it manually into your work when you’re ready.
1122
1139
1123
1140
If you have a branch set up to track a remote branch (see the next section and <<_git_branching>> for more information), you can use the `git pull` command to automatically fetch and then merge a remote branch into your current branch.
@@ -1128,7 +1145,7 @@ Running `git pull` generally fetches data from the server you originally cloned
1128
1145
1129
1146
When you have your project at a point that you want to share, you have to push it upstream.
1130
1147
The command for this is simple: `git push [remote-name] [branch-name]`.
1131
-
If you want to push your master branch to your `origin` server (again, cloning generally sets up both of those names for you automatically), then you can run this to push your work back up to the server:
1148
+
If you want to push your master branch to your `origin` server (again, cloning generally sets up both of those names for you automatically), then you can run this to push any commits you've done back up to the server:
1132
1149
1133
1150
[source,shell]
1134
1151
----
@@ -1149,12 +1166,12 @@ If you run this command with a particular shortname, such as `origin`, you get s
1149
1166
----
1150
1167
$ git remote show origin
1151
1168
* remote origin
1152
-
Fetch URL: https://github.com/schacon/ticgit.git
1153
-
Push URL: https://github.com/schacon/ticgit.git
1169
+
Fetch URL: https://github.com/schacon/ticgit
1170
+
Push URL: https://github.com/schacon/ticgit
1154
1171
HEAD branch: master
1155
1172
Remote branches:
1156
-
master tracked
1157
-
ticgit tracked
1173
+
master tracked
1174
+
dev-branch tracked
1158
1175
Local branch configured for 'git pull':
1159
1176
master merges with remote master
1160
1177
Local ref configured for 'git push':
@@ -1172,33 +1189,32 @@ When you’re using Git more heavily, however, you may see much more information
issue-43 new (next fetch will store in remotes/origin)
1201
+
issue-45 new (next fetch will store in remotes/origin)
1202
+
refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove)
1203
+
Local branches configured for 'git pull':
1204
+
dev-branch merges with remote dev-branch
1205
+
master merges with remote master
1206
+
Local refs configured for 'git push':
1207
+
dev-branch pushes to dev-branch (up to date)
1208
+
markdown-strip pushes to markdown-strip (up to date)
1209
+
master pushes to master (up to date)
1210
+
----
1211
+
1212
+
This command shows which branch is automatically pushed to when you run `git push` while on certain branches.
1197
1213
It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple branches that are automatically merged when you run `git pull`.
1198
1214
1199
1215
==== Removing and Renaming Remotes
1200
1216
1201
-
If you want to rename a reference, in newer versions of Git you can run `git remote rename` to change a remote’s shortname.
1217
+
If you want to rename a reference you can run `git remote rename` to change a remote’s shortname.
1202
1218
For instance, if you want to rename `pb` to `paul`, you can do so with `git remote rename`:
0 commit comments