diff --git a/git-branches.Rmd b/git-branches.Rmd index 690d004..cdc4bc2 100644 --- a/git-branches.Rmd +++ b/git-branches.Rmd @@ -1,19 +1,20 @@ # Branches {#git-branches} Branching means that you take a detour from the main stream of development and -do work without changing the main stream. It allows one or many people to work -in parallel without overwriting each other's work. +do work without changing the main stream. +It allows one or many people to work in parallel without overwriting each other's work. +It allows a someone working solo to work incrementally on an experimental idea, without jeopardizing the state of the main product. -Branching in git is very lightweight, which means creating a branch and -switching between branches is nearly instantaneous. This means git encourages -workflows which create small branches for exploration or new features, often -merging them back together quickly. +Branching in Git is very lightweight, which means creating a branch and +switching between branches is nearly instantaneous. +This means Git encourages workflows which create small branches for exploration or new features, often merging them back together quickly. ## Create a new branch -You can create a new branch with `git branch`, then checkout the branch with `git checkout`. To distinguish it from the main stream of development, presumably on `master`, we'll call this a "feature branch". +You can create a new branch with `git branch`, then checkout the branch with `git checkout`. +To distinguish it from the main stream of development, presumably on `main`, we'll call this a "feature branch". -```shell +```console git branch issue-5 git checkout issue-5 ``` @@ -27,57 +28,61 @@ Once you have switched to a branch, you can commit to it as usual. You use `git checkout` to switch between branches. But what do you do if you are working on a branch and need to switch, -but the work on the current branch is not complete? One option is the [Git -stash](https://git-scm.com/book/en/v2/ch00/_git_stashing), but generally a -better option is to safeguard the current state with a temporary commit. Here I -use "WIP" as the commit message to indicate work in progress. +but the work on the current branch is not complete? +One option is the [Git stash](https://git-scm.com/book/en/v2/ch00/_git_stashing), but generally a better option is to safeguard the current state with a temporary commit. +Here I use "WIP" as the commit message to indicate work in progress. -```shell +```console git commit --all -m "WIP" -git checkout master +git checkout main ``` Then when you come back to the branch and continue your work, you -need to undo the temporary commit by [resetting](#reset) your state. Specifically, we want a mixed reset. This is "working directory safe", i.e. it does not affect the state of any files. But it does peel off the temporary WIP commit. Below, the reference `HEAD^` says to roll the commit state back to the parent of the current commit (`HEAD`). +need to undo the temporary commit by [resetting](#reset) your state. +Specifically, we want a mixed reset. +This is "working directory safe", i.e. it does not affect the state of any files. +But it does peel off the temporary WIP commit. +Below, the reference `HEAD^` says to roll the commit state back to the parent of the current commit (`HEAD`). -```shell +```console git checkout issue-5 git reset HEAD^ ``` If this is difficult to remember, or to roll the commit state back to a different previous state, the reference can also be given as the SHA of a specific commit, which you can see via `git log`. +This is where I think a graphical Git client can be invaluable, as you can generally right click on the target commit, then select the desired type of reset (e.g., soft, mixed, or hard). +This is exactly the type of intermediate-to-advanced Git usage that often feels more approachable in a graphical client. ## Merging a branch -Once you have done your work and committed it to the feature branch, you can switch back to `master` and merge the feature branch. +Once you have done your work and committed it to the feature branch, you can switch back to `main` and merge the feature branch. -```shell -git checkout master +```console +git checkout main git merge issue-5 ``` ## Dealing with conflicts -Most of the time, the merge will go smoothly. However if both the branches you -are merging changed the same part of the same file you will get a merge -conflict. +Most of the time, the merge will go smoothly. +However if both the branches you are merging changed the same part of the same file you will get a merge conflict. -```shell +```console git merge issue-5 # Auto-merging index.html # CONFLICT (content): Merge conflict in index.html # Automatic merge failed; fix conflicts and then commit the result. ``` -The first thing to do is **NOT PANIC**. Merge conflicts are not the end of the -world and most are relatively small and straightforward to resolve. +The first thing to do is **NOT PANIC**. +Merge conflicts are not the end of the world and most are relatively small and straightforward to resolve. The first step to solving a merge conflict is determining which files are in conflict, which you can do with `git status`: ```shell git status -# On branch master +# On branch main # You have unmerged paths. # (fix conflicts and run "git commit") # @@ -89,8 +94,8 @@ git status # no changes added to commit (use "git add" and/or "git commit -a") ``` -So this shows only `index.html` is unmerged and needs to be resolved. We can -then open the file to see what lines are in conflict. +So this shows only `index.html` is unmerged and needs to be resolved. +We can then open the file to see what lines are in conflict. ```html <<<<<<< HEAD:index.html @@ -103,10 +108,12 @@ then open the file to see what lines are in conflict. ``` In this conflict, the lines between `<<<<<< HEAD:index.html` and `======` are -the content from the branch you are currently on. The lines between `=======` -and `>>>>>>> issue-5:index.html` are from the feature branch we are merging. +the content from the branch you are currently on. +The lines between `=======` and `>>>>>>> issue-5:index.html` are from the feature branch we are merging. -To resolve the conflict, edit this section until it reflects the state you want in the merged result. Pick one version or the other or create a hybrid. Also remove the conflict markers `<<<<<<`, `======` and `>>>>>>`. +To resolve the conflict, edit this section until it reflects the state you want in the merged result. +Pick one version or the other or create a hybrid. +Also remove the conflict markers `<<<<<<`, `======` and `>>>>>>`. ```html
``` -Now run `git add index.html` and `git commit` to finalize the merge. CONFLICTS RESOLVED. +Now run `git add index.html` and `git commit` to finalize the merge. +CONFLICTS RESOLVED. ### Bailing out If, during the merge, you get confused about the state of things or make a mistake, use `git merge --abort` to abort the merge and go back to the state -prior to running `git merge`. Then you can try to complete the merge again. +prior to running `git merge`. +Then you can try to complete the merge again. Git Basic Branching and Merging: diff --git a/git-remotes.Rmd b/git-remotes.Rmd index ea0057f..5e2c2aa 100644 --- a/git-remotes.Rmd +++ b/git-remotes.Rmd @@ -1,8 +1,9 @@ # Remotes {#git-remotes} Remote repositories are versions of your project that are hosted on the -Internet or another network. A single project can have 1, 2 or even hundreds of -remotes. You pull others changes from remotes and push your changes to remotes. +Internet or another network. +A single project can have 1, 2, or even hundreds of remotes. +You pull others' changes from remotes and push your changes to remotes. ```{r setup, include = FALSE} has_bash <- Sys.which('bash') != '' && .Platform$OS.type != 'windows' @@ -20,40 +21,48 @@ git remote -v ## Adding a new remote `git clone` automatically adds a new remote, so often you do not need to do -this manually initially. However, after the initial clone, it is often useful to -add additional remotes. +this manually initially. +However, after the initial clone, it is often useful to add additional remotes. -Use `git remote add` to add a new remote +Use `git remote add` to add a new remote: -```shell +```console git remote add happygit https://github.com/jennybc/happy-git-with-r.git ``` Note: when you add a remote you give it a nickname (here `happygit`), which you can use in git commands in place of the entire URL. -```shell +```console git fetch happygit ``` -Sidebar on nicknames: there is a strong convention to use `origin` as the nickname of your main remote. At this point, it is common for the main remote of a repo to be hosted on GitHub (or GitLab or Bitbucket). It is tempting to use a more descriptive nickname (such as `github`), but you might find that following convention is worth it. It makes your setup easier for others to understand and for you to transfer information that you read in documentation, on Stack Overflow, or in blogs. +Sidebar on nicknames: there is a strong convention to use `origin` as the nickname of your main remote. +At this point, it is common for the main remote of a repo to be hosted on GitHub (or GitLab or Bitbucket). +It is tempting to use a more descriptive nickname (such as `github`), but you might find that following convention is worth it. +It makes your setup easier for others to understand and for you to transfer information that you read in documentation, on Stack Overflow, or in blogs. -A common reason to add a second remote is when you have done a "fork and clone" of a repo and your personal copy is set up as the `origin` remote. Eventually you will want to pull changes from the original repository. It is common to use `upstream` as the nickname for this remote. +A common reason to add a second remote is when you have done a "fork and clone" of a repo and your personal copy (your fork) is set up as the `origin` remote. +Eventually you will want to pull changes from the original repository. It is common to use `upstream` as the nickname for this remote. + +```console +git remote add upstream https://github.com/TRUE_OWNER/REPO.git +``` ## Fetching data from remotes -To get new data from a remote use `git fetch