mirror of
https://github.com/k88hudson/git-flight-rules.git
synced 2025-06-16 21:01:45 -03:00
Replace master with main (issue/#308) (#309)
This commit is contained in:
208
README.md
208
README.md
@ -70,7 +70,7 @@ All commands should work for at least git version 2.13.0. See the [git website](
|
||||
- [Create a branch from a commit](#create-a-branch-from-a-commit)
|
||||
- [I pulled from/into the wrong branch](#i-pulled-frominto-the-wrong-branch)
|
||||
- [I want to discard local commits so my branch is the same as one on the server](#i-want-to-discard-local-commits-so-my-branch-is-the-same-as-one-on-the-server)
|
||||
- [I committed to master instead of a new branch](#i-committed-to-master-instead-of-a-new-branch)
|
||||
- [I committed to main instead of a new branch](#i-committed-to-main-instead-of-a-new-branch)
|
||||
- [I want to keep the whole file from another ref-ish](#i-want-to-keep-the-whole-file-from-another-ref-ish)
|
||||
- [I made several commits on a single branch that should be on different branches](#i-made-several-commits-on-a-single-branch-that-should-be-on-different-branches)
|
||||
- [I want to delete local branches that were deleted upstream](#i-want-to-delete-local-branches-that-were-deleted-upstream)
|
||||
@ -240,15 +240,15 @@ After all of this, do not forget to respond to any code review feedback.
|
||||
|
||||
#### Suggesting code via patches
|
||||
|
||||
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use `git format-patch`.
|
||||
Another approach to suggesting code changes that doesn't rely on third party sites such as Github is to use `git format-patch`.
|
||||
|
||||
`format-patch` creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
|
||||
`format-patch` creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.
|
||||
|
||||
A patch can be viewed and even edited by the recipient and applied using `git am`.
|
||||
|
||||
For example, to create a patch based on the previous commit you would run `git format-patch HEAD^` which would create a .patch file called something like 0001-My-Commit-Message.patch.
|
||||
|
||||
To apply this patch file to your repository you would run `gim am ./0001-My-Commit-Message.patch`.
|
||||
To apply this patch file to your repository you would run `gim am ./0001-My-Commit-Message.patch`.
|
||||
|
||||
Patches can also be sent via email using the `git send-email` command. For information on usage and configuration see: https://git-send-email.io
|
||||
|
||||
@ -259,18 +259,18 @@ After a while, the `upstream` repository may have been updated, and these update
|
||||
You probably have set up a remote that points to the original project. If not, do this now. Generally we use `upstream` as a remote name:
|
||||
|
||||
```sh
|
||||
$ (master) git remote add upstream <link-to-original-repository>
|
||||
# $ (master) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
|
||||
$ (main) git remote add upstream <link-to-original-repository>
|
||||
# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git
|
||||
```
|
||||
|
||||
Now you can fetch from upstream and get the latest updates.
|
||||
|
||||
```sh
|
||||
$ (master) git fetch upstream
|
||||
$ (master) git merge upstream/master
|
||||
$ (main) git fetch upstream
|
||||
$ (main) git merge upstream/main
|
||||
|
||||
# or using a single command
|
||||
$ (master) git pull upstream master
|
||||
$ (main) git pull upstream main
|
||||
```
|
||||
|
||||
## Editing Commits
|
||||
@ -281,7 +281,7 @@ $ (master) git pull upstream master
|
||||
Let's say that you just blindly committed changes with `git commit -a` and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
|
||||
|
||||
```sh
|
||||
(master)$ git show
|
||||
(main)$ git show
|
||||
```
|
||||
|
||||
Or
|
||||
@ -410,13 +410,13 @@ If you accidentally do `git reset --hard`, you can normally still get your commi
|
||||
Note: This is only valid if your work is backed up, i.e., either committed or stashed. `git reset --hard` _will remove_ uncommitted modifications, so use it with caution. (A safer option is `git reset --keep`.)
|
||||
|
||||
```sh
|
||||
(master)$ git reflog
|
||||
(main)$ git reflog
|
||||
```
|
||||
|
||||
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
|
||||
|
||||
```sh
|
||||
(master)$ git reset --hard SHA1234
|
||||
(main)$ git reset --hard SHA1234
|
||||
```
|
||||
|
||||
And you should be good to go.
|
||||
@ -482,17 +482,17 @@ Using bfg-repo-cleaner requires java. Download the bfg jar from the link [here](
|
||||
|
||||
To delete a specific file.
|
||||
```sh
|
||||
(master)$ git rm path/to/filetoremove
|
||||
(master)$ git commit -m "Commit removing filetoremove"
|
||||
(master)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
|
||||
(main)$ git rm path/to/filetoremove
|
||||
(main)$ git commit -m "Commit removing filetoremove"
|
||||
(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
|
||||
```
|
||||
Note that in bfg you must use the plain file name even if it is in a subdirectory.
|
||||
|
||||
You can also delete a file by pattern, e.g.:
|
||||
```sh
|
||||
(master)$ git rm *.jpg
|
||||
(master)$ git commit -m "Commit removing *.jpg"
|
||||
(master)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
|
||||
(main)$ git rm *.jpg
|
||||
(main)$ git commit -m "Commit removing *.jpg"
|
||||
(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
|
||||
```
|
||||
|
||||
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
|
||||
@ -506,7 +506,7 @@ Note, if you renamed a file as part of a commit, e.g. if it started as `LargeFil
|
||||
In the below, replace `filepattern` may be a specific name or pattern, e.g. `*.jpg`. This will remove files matching the pattern from all history and branches.
|
||||
|
||||
```sh
|
||||
(master)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
|
||||
(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
|
||||
```
|
||||
|
||||
Behind-the-scenes explanation:
|
||||
@ -520,19 +520,19 @@ Behind-the-scenes explanation:
|
||||
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over.
|
||||
To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
|
||||
```sh
|
||||
(master)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
|
||||
(master)$ git push origin --force --tags
|
||||
(main)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
|
||||
(main)$ git push origin --force --tags
|
||||
```
|
||||
|
||||
Since you just rewrote the entire git repo history, the `git push` operation may be too large, and return the error `“The remote end hung up unexpectedly”`. If this happens, you can try increasing the git post buffer:
|
||||
```sh
|
||||
(master)$ git config http.postBuffer 524288000
|
||||
(master)$ git push --force
|
||||
(main)$ git config http.postBuffer 524288000
|
||||
(main)$ git push --force
|
||||
```
|
||||
|
||||
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing `<number>` until the push operation succeeds.
|
||||
```sh
|
||||
(master)$ git push -u origin HEAD~<number>:refs/head/master --force
|
||||
(main)$ git push -u origin HEAD~<number>:refs/head/main --force
|
||||
```
|
||||
Once the push operation succeeds the first time, decrease `<number>` gradually until a conventional `git push` succeeds.
|
||||
|
||||
@ -681,7 +681,7 @@ If you want to discard all your local staged and unstaged changes, you can do th
|
||||
```sh
|
||||
(my-branch)$ git reset --hard
|
||||
# or
|
||||
(master)$ git checkout -f
|
||||
(main)$ git checkout -f
|
||||
```
|
||||
|
||||
This will unstage all files you might have staged with `git add`:
|
||||
@ -819,7 +819,7 @@ $ git checkout -b <branch> <SHA1_OF_COMMIT>
|
||||
This is another chance to use `git reflog` to see where your HEAD pointed before the bad pull.
|
||||
|
||||
```sh
|
||||
(master)$ git reflog
|
||||
(main)$ git reflog
|
||||
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
|
||||
c5bc55a HEAD@{1}: checkout: checkout message goes here
|
||||
```
|
||||
@ -850,41 +850,41 @@ Confirm that you haven't pushed your changes to the server.
|
||||
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
|
||||
|
||||
```sh
|
||||
(master)$ git reset --hard origin/my-branch
|
||||
(main)$ git reset --hard origin/my-branch
|
||||
```
|
||||
|
||||
<a name="commit-wrong-branch"></a>
|
||||
### I committed to master instead of a new branch
|
||||
### I committed to main instead of a new branch
|
||||
|
||||
Create the new branch while remaining on master:
|
||||
Create the new branch while remaining on main:
|
||||
|
||||
```sh
|
||||
(master)$ git branch my-branch
|
||||
(main)$ git branch my-branch
|
||||
```
|
||||
|
||||
Reset the branch master to the previous commit:
|
||||
Reset the branch main to the previous commit:
|
||||
|
||||
```sh
|
||||
(master)$ git reset --hard HEAD^
|
||||
(main)$ git reset --hard HEAD^
|
||||
```
|
||||
|
||||
`HEAD^` is short for `HEAD^1`. This stands for the first parent of `HEAD`, similarly `HEAD^2` stands for the second parent of the commit (merges can have 2 parents).
|
||||
|
||||
Note that `HEAD^2` is **not** the same as `HEAD~2` (see [this link](http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde) for more information).
|
||||
|
||||
Alternatively, if you don't want to use `HEAD^`, find out what the commit hash you want to set your master branch to (`git log` should do the trick). Then reset to that hash. `git push` will make sure that this change is reflected on your remote.
|
||||
Alternatively, if you don't want to use `HEAD^`, find out what the commit hash you want to set your main branch to (`git log` should do the trick). Then reset to that hash. `git push` will make sure that this change is reflected on your remote.
|
||||
|
||||
For example, if the hash of the commit that your master branch is supposed to be at is `a13b85e`:
|
||||
For example, if the hash of the commit that your main branch is supposed to be at is `a13b85e`:
|
||||
|
||||
```sh
|
||||
(master)$ git reset --hard a13b85e
|
||||
(main)$ git reset --hard a13b85e
|
||||
HEAD is now at a13b85e
|
||||
```
|
||||
|
||||
Checkout the new branch to continue working:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout my-branch
|
||||
(main)$ git checkout my-branch
|
||||
```
|
||||
|
||||
<a name="keep-whole-file"></a>
|
||||
@ -927,10 +927,10 @@ Note: Spike solutions are made to analyze or solve the problem. These solutions
|
||||
<a name="cherry-pick"></a>
|
||||
### I made several commits on a single branch that should be on different branches
|
||||
|
||||
Say you are on your master branch. Running `git log`, you see you have made two commits:
|
||||
Say you are on your main branch. Running `git log`, you see you have made two commits:
|
||||
|
||||
```sh
|
||||
(master)$ git log
|
||||
(main)$ git log
|
||||
|
||||
commit e3851e817c451cc36f2e6f3049db528415e3c114
|
||||
Author: Alex Lee <alexlee@example.com>
|
||||
@ -953,17 +953,17 @@ Date: Tue Jul 21 01:12:48 2014 -0400
|
||||
|
||||
Let's take note of our commit hashes for each bug (`e3851e8` for #21, `5ea5173` for #14).
|
||||
|
||||
First, let's reset our master branch to the correct commit (`a13b85e`):
|
||||
First, let's reset our main branch to the correct commit (`a13b85e`):
|
||||
|
||||
```sh
|
||||
(master)$ git reset --hard a13b85e
|
||||
(main)$ git reset --hard a13b85e
|
||||
HEAD is now at a13b85e
|
||||
```
|
||||
|
||||
Now, we can create a fresh branch for our bug #21:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout -b 21
|
||||
(main)$ git checkout -b 21
|
||||
(21)$
|
||||
```
|
||||
|
||||
@ -975,11 +975,11 @@ Now, let's *cherry-pick* the commit for bug #21 on top of our branch. That means
|
||||
|
||||
At this point, there is a possibility there might be conflicts. See the [**There were conflicts**](#merge-conflict) section in the [interactive rebasing section above](#interactive-rebase) for how to resolve conflicts.
|
||||
|
||||
Now let's create a new branch for bug #14, also based on master
|
||||
Now let's create a new branch for bug #14, also based on main
|
||||
|
||||
```sh
|
||||
(21)$ git checkout master
|
||||
(master)$ git checkout -b 14
|
||||
(21)$ git checkout main
|
||||
(main)$ git checkout -b 14
|
||||
(14)$
|
||||
```
|
||||
|
||||
@ -1005,7 +1005,7 @@ where, `upstream` is the remote you want to fetch from.
|
||||
If you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout -b my-branch
|
||||
(main)$ git checkout -b my-branch
|
||||
(my-branch)$ git branch
|
||||
(my-branch)$ touch foo.txt
|
||||
(my-branch)$ ls
|
||||
@ -1035,31 +1035,31 @@ Date: Tue Jul 29 13:14:46 2014 -0400
|
||||
Fixes #6: Force pushing after amending commits
|
||||
```
|
||||
|
||||
Now we're switching back to master and 'accidentally' removing our branch.
|
||||
Now we're switching back to main and 'accidentally' removing our branch.
|
||||
|
||||
```sh
|
||||
(my-branch)$ git checkout master
|
||||
Switched to branch 'master'
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
(master)$ git branch -D my-branch
|
||||
(my-branch)$ git checkout main
|
||||
Switched to branch 'main'
|
||||
Your branch is up-to-date with 'origin/main'.
|
||||
(main)$ git branch -D my-branch
|
||||
Deleted branch my-branch (was 4e3cd85).
|
||||
(master)$ echo oh noes, deleted my branch!
|
||||
(main)$ echo oh noes, deleted my branch!
|
||||
oh noes, deleted my branch!
|
||||
```
|
||||
|
||||
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
|
||||
|
||||
```
|
||||
(master)$ git reflog
|
||||
69204cd HEAD@{0}: checkout: moving from my-branch to master
|
||||
(main)$ git reflog
|
||||
69204cd HEAD@{0}: checkout: moving from my-branch to main
|
||||
4e3cd85 HEAD@{1}: commit: foo.txt added
|
||||
69204cd HEAD@{2}: checkout: moving from master to my-branch
|
||||
69204cd HEAD@{2}: checkout: moving from main to my-branch
|
||||
```
|
||||
|
||||
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
|
||||
|
||||
```sh
|
||||
(master)$ git checkout -b my-branch-help
|
||||
(main)$ git checkout -b my-branch-help
|
||||
Switched to a new branch 'my-branch-help'
|
||||
(my-branch-help)$ git reset --hard 4e3cd85
|
||||
HEAD is now at 4e3cd85 foo.txt added
|
||||
@ -1074,25 +1074,25 @@ Voila! We got our removed file back. `git reflog` is also useful when rebasing g
|
||||
To delete a remote branch:
|
||||
|
||||
```sh
|
||||
(master)$ git push origin --delete my-branch
|
||||
(main)$ git push origin --delete my-branch
|
||||
```
|
||||
|
||||
You can also do:
|
||||
|
||||
```sh
|
||||
(master)$ git push origin :my-branch
|
||||
(main)$ git push origin :my-branch
|
||||
```
|
||||
|
||||
To delete a local branch:
|
||||
|
||||
```sh
|
||||
(master)$ git branch -d my-branch
|
||||
(main)$ git branch -d my-branch
|
||||
```
|
||||
|
||||
To delete a local branch that *has not* been merged to the current branch or an upstream:
|
||||
|
||||
```sh
|
||||
(master)$ git branch -D my-branch
|
||||
(main)$ git branch -D my-branch
|
||||
```
|
||||
|
||||
### I want to delete multiple branches
|
||||
@ -1100,7 +1100,7 @@ To delete a local branch that *has not* been merged to the current branch or an
|
||||
Say you want to delete all branches that start with `fix/`:
|
||||
|
||||
```sh
|
||||
(master)$ git branch | grep 'fix/' | xargs git branch -d
|
||||
(main)$ git branch | grep 'fix/' | xargs git branch -d
|
||||
```
|
||||
|
||||
### I want to rename a branch
|
||||
@ -1108,18 +1108,18 @@ Say you want to delete all branches that start with `fix/`:
|
||||
To rename the current (local) branch:
|
||||
|
||||
```sh
|
||||
(master)$ git branch -m new-name
|
||||
(main)$ git branch -m new-name
|
||||
```
|
||||
|
||||
To rename a different (local) branch:
|
||||
|
||||
```sh
|
||||
(master)$ git branch -m old-name new-name
|
||||
(main)$ git branch -m old-name new-name
|
||||
```
|
||||
To delete the `old-name` remote branch and push the `new-name` local branch:
|
||||
|
||||
|
||||
```sh
|
||||
(master)$ git push origin :old_name new_name
|
||||
(main)$ git push origin :old_name new_name
|
||||
```
|
||||
|
||||
<a name="i-want-to-checkout-to-a-remote-branch-that-someone-else-is-working-on"></a>
|
||||
@ -1128,13 +1128,13 @@ To rename a different (local) branch:
|
||||
First, fetch all branches from remote:
|
||||
|
||||
```sh
|
||||
(master)$ git fetch --all
|
||||
(main)$ git fetch --all
|
||||
```
|
||||
|
||||
Say you want to checkout to `daves` from the remote.
|
||||
|
||||
```sh
|
||||
(master)$ git checkout --track origin/daves
|
||||
(main)$ git checkout --track origin/daves
|
||||
Branch daves set up to track remote branch daves from origin.
|
||||
Switched to a new branch 'daves'
|
||||
```
|
||||
@ -1187,14 +1187,14 @@ By checking your remote branches, you can see which remote branch your HEAD is t
|
||||
```sh
|
||||
$ git branch -r
|
||||
origin/HEAD -> origin/gh-pages
|
||||
origin/master
|
||||
origin/main
|
||||
```
|
||||
|
||||
To change `origin/HEAD` to track `origin/master`, you can run this command:
|
||||
To change `origin/HEAD` to track `origin/main`, you can run this command:
|
||||
|
||||
```sh
|
||||
$ git remote set-head origin --auto
|
||||
origin/HEAD set to master
|
||||
origin/HEAD set to main
|
||||
```
|
||||
|
||||
### I made changes on the wrong branch
|
||||
@ -1224,10 +1224,10 @@ You may have merged or rebased your current branch with a wrong branch, or you c
|
||||
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout my-branch
|
||||
(my-branch)$ git rebase -i master
|
||||
(my-branch)$ git checkout master
|
||||
(master)$ git merge --ff-only my-branch
|
||||
(main)$ git checkout my-branch
|
||||
(my-branch)$ git rebase -i main
|
||||
(my-branch)$ git checkout main
|
||||
(main)$ git merge --ff-only my-branch
|
||||
```
|
||||
|
||||
For more, see [this SO thread](https://stackoverflow.com/questions/11058312/how-can-i-use-git-rebase-without-requiring-a-forced-push).
|
||||
@ -1235,23 +1235,23 @@ For more, see [this SO thread](https://stackoverflow.com/questions/11058312/how-
|
||||
<a name="interactive-rebase"></a>
|
||||
### I need to combine commits
|
||||
|
||||
Let's suppose you are working in a branch that is/will become a pull-request against `master`. In the simplest case when all you want to do is to combine *all* commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the master branch is up to date and all your changes committed, then:
|
||||
Let's suppose you are working in a branch that is/will become a pull-request against `main`. In the simplest case when all you want to do is to combine *all* commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:
|
||||
|
||||
```sh
|
||||
(my-branch)$ git reset --soft master
|
||||
(my-branch)$ git reset --soft main
|
||||
(my-branch)$ git commit -am "New awesome feature"
|
||||
```
|
||||
|
||||
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
|
||||
|
||||
```sh
|
||||
(my-branch)$ git rebase -i master
|
||||
(my-branch)$ git rebase -i main
|
||||
```
|
||||
|
||||
If you aren't working against another branch you'll have to rebase relative to your `HEAD`. If you want to squash the last 2 commits, for example, you'll have to rebase against `HEAD~2`. For the last 3, `HEAD~3`, etc.
|
||||
|
||||
```sh
|
||||
(master)$ git rebase -i HEAD~2
|
||||
(main)$ git rebase -i HEAD~2
|
||||
```
|
||||
|
||||
After you run the interactive rebase command, you will see something like this in your text editor:
|
||||
@ -1311,7 +1311,7 @@ Newer, awesomer features
|
||||
# Please enter the commit message for your changes. Lines starting
|
||||
# with '#' will be ignored, and an empty message aborts the commit.
|
||||
# rebase in progress; onto 8074d12
|
||||
# You are currently editing a commit while rebasing branch 'master' on '8074d12'.
|
||||
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
|
||||
#
|
||||
# Changes to be committed:
|
||||
# modified: README.md
|
||||
@ -1322,20 +1322,20 @@ Newer, awesomer features
|
||||
If everything is successful, you should see something like this:
|
||||
|
||||
```sh
|
||||
(master)$ Successfully rebased and updated refs/heads/master.
|
||||
(main)$ Successfully rebased and updated refs/heads/main.
|
||||
```
|
||||
|
||||
#### Safe merging strategy
|
||||
`--no-commit` performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. `no-ff` maintains evidence that a feature branch once existed, keeping project history consistent.
|
||||
|
||||
```sh
|
||||
(master)$ git merge --no-ff --no-commit my-branch
|
||||
(main)$ git merge --no-ff --no-commit my-branch
|
||||
```
|
||||
|
||||
#### I need to merge a branch into a single commit
|
||||
|
||||
```sh
|
||||
(master)$ git merge --squash my-branch
|
||||
(main)$ git merge --squash my-branch
|
||||
```
|
||||
|
||||
<a name="rebase-unpushed-commits"></a>
|
||||
@ -1344,7 +1344,7 @@ If everything is successful, you should see something like this:
|
||||
Sometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.
|
||||
|
||||
```sh
|
||||
(master)$ git rebase -i @{u}
|
||||
(main)$ git rebase -i @{u}
|
||||
```
|
||||
|
||||
This will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.
|
||||
@ -1361,7 +1361,7 @@ This command is available since Git version >= 1.7.4
|
||||
|
||||
### I need to update the parent commit of my branch
|
||||
|
||||
Say I have a master branch, a feature-1 branch branched from master, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with `git rebase --onto`.
|
||||
Say I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with `git rebase --onto`.
|
||||
|
||||
```sh
|
||||
(feature-2)$ git rebase --onto feature-1 <the first commit in your feature-2 branch that you don't want to bring along> feature-2
|
||||
@ -1374,13 +1374,13 @@ This helps in sticky scenarios where you might have a feature built on another f
|
||||
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
|
||||
|
||||
```sh
|
||||
(master)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
|
||||
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
|
||||
```
|
||||
|
||||
This will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:
|
||||
|
||||
```sh
|
||||
(master)$ git log master ^feature/120-on-scroll --no-merges
|
||||
(main)$ git log main ^feature/120-on-scroll --no-merges
|
||||
```
|
||||
|
||||
### Possible issues with interactive rebases
|
||||
@ -1395,7 +1395,7 @@ noop
|
||||
|
||||
That means you are trying to rebase against a branch that is at an identical commit, or is *ahead* of your current branch. You can try:
|
||||
|
||||
* making sure your master branch is where it should be
|
||||
* making sure your main branch is where it should be
|
||||
* rebase against `HEAD~2` or earlier instead
|
||||
|
||||
<a name="merge-conflict"></a>
|
||||
@ -1430,7 +1430,7 @@ You will need to resolve the differences between the code that was added in your
|
||||
If you want to keep one branch's version of the code, you can use `--ours` or `--theirs`:
|
||||
|
||||
```sh
|
||||
(master*)$ git checkout --ours README.md
|
||||
(main*)$ git checkout --ours README.md
|
||||
```
|
||||
|
||||
- When *merging*, use `--ours` to keep changes from the local branch, or `--theirs` to keep changes from the other branch.
|
||||
@ -1439,7 +1439,7 @@ If you want to keep one branch's version of the code, you can use `--ours` or `-
|
||||
If the merges are more complicated, you can use a visual diff editor:
|
||||
|
||||
```sh
|
||||
(master*)$ git mergetool -t opendiff
|
||||
(main*)$ git mergetool -t opendiff
|
||||
```
|
||||
|
||||
After you have resolved all conflicts and tested your code, `git add` the files you have changed, and then continue the rebase with `git rebase --continue`
|
||||
@ -1689,7 +1689,7 @@ From github.com:foo/bar
|
||||
### Exporting a repository as a Zip file
|
||||
|
||||
```sh
|
||||
$ git archive --format zip --output /full/path/to/zipfile.zip master
|
||||
$ git archive --format zip --output /full/path/to/zipfile.zip main
|
||||
```
|
||||
### Push a branch and a tag that have the same name
|
||||
|
||||
@ -1719,21 +1719,21 @@ $ git push origin refs/tags/<tag-name>
|
||||
### I want to change a file name's capitalization, without changing the contents of the file
|
||||
|
||||
```sh
|
||||
(master)$ git mv --force myfile MyFile
|
||||
(main)$ git mv --force myfile MyFile
|
||||
```
|
||||
|
||||
### I want to overwrite local files when doing a git pull
|
||||
|
||||
```sh
|
||||
(master)$ git fetch --all
|
||||
(master)$ git reset --hard origin/master
|
||||
(main)$ git fetch --all
|
||||
(main)$ git reset --hard origin/main
|
||||
```
|
||||
|
||||
<a href="remove-from-git"></a>
|
||||
### I want to remove a file from Git but keep the file
|
||||
|
||||
```sh
|
||||
(master)$ git rm --cached log.txt
|
||||
(main)$ git rm --cached log.txt
|
||||
```
|
||||
|
||||
### I want to revert a file to a specific revision
|
||||
@ -1741,13 +1741,13 @@ $ git push origin refs/tags/<tag-name>
|
||||
Assuming the hash of the commit you want is c5f567:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout c5f567 -- file1/to/restore file2/to/restore
|
||||
(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore
|
||||
```
|
||||
|
||||
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
|
||||
|
||||
```sh
|
||||
(master)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
|
||||
(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
|
||||
```
|
||||
|
||||
### I want to list changes of a specific file between commits or branches
|
||||
@ -1761,7 +1761,7 @@ $ git diff HEAD:path_to_file/file c5f567:path_to_file/file
|
||||
Same goes for branches:
|
||||
|
||||
```sh
|
||||
$ git diff master:path_to_file/file staging:path_to_file/file
|
||||
$ git diff main:path_to_file/file staging:path_to_file/file
|
||||
```
|
||||
|
||||
### I want Git to ignore changes to a specific file
|
||||
@ -1782,7 +1782,7 @@ $ git update-index --no-assume-unchanged file-to-stop-ignoring
|
||||
|
||||
The [git-bisect](https://git-scm.com/docs/git-bisect) command uses a binary search to find which commit in your Git history introduced a bug.
|
||||
|
||||
Suppose you're on the `master` branch, and you want to find the commit that broke some feature. You start bisect:
|
||||
Suppose you're on the `main` branch, and you want to find the commit that broke some feature. You start bisect:
|
||||
|
||||
```sh
|
||||
$ git bisect start
|
||||
@ -1841,7 +1841,7 @@ On OS X and Linux, your git configuration file is stored in ```~/.gitconfig```.
|
||||
wip = rebase -i @{u}
|
||||
zap = fetch -p
|
||||
day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\"$(git config --get user.name)\"
|
||||
delete-merged-branches = "!f() { git checkout --quiet master && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
|
||||
delete-merged-branches = "!f() { git checkout --quiet main && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"
|
||||
```
|
||||
|
||||
### I want to add an empty directory to my repository
|
||||
@ -1944,21 +1944,21 @@ So, you're screwed - you `reset` something, or you merged the wrong branch, or y
|
||||
This is what `git reflog` is for. `reflog` keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).
|
||||
|
||||
```sh
|
||||
(master)$ git reflog
|
||||
(main)$ git reflog
|
||||
0a2e358 HEAD@{0}: reset: moving to HEAD~2
|
||||
0254ea7 HEAD@{1}: checkout: moving from 2.2 to master
|
||||
c10f740 HEAD@{2}: checkout: moving from master to 2.2
|
||||
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
|
||||
c10f740 HEAD@{2}: checkout: moving from main to 2.2
|
||||
```
|
||||
|
||||
The reflog above shows a checkout from master to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled `HEAD@{0}`.
|
||||
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled `HEAD@{0}`.
|
||||
|
||||
If it turns out that you accidentally moved back, the reflog will contain the commit master pointed to (0254ea7) before you accidentally dropped 2 commits.
|
||||
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.
|
||||
|
||||
```sh
|
||||
$ git reset --hard 0254ea7
|
||||
```
|
||||
|
||||
Using `git reset` it is then possible to change master back to the commit it was before. This provides a safety net in case history was accidentally changed.
|
||||
Using `git reset` it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.
|
||||
|
||||
(copied and edited from [Source](https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog)).
|
||||
|
||||
|
Reference in New Issue
Block a user