Last active
March 26, 2024 22:28
-
-
Save joe-oli/47d027acfce8980f580427ee0d63a94d to your computer and use it in GitHub Desktop.
Git notes #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>git push | |
Where does it push to? | |
==================== | |
The git push command pushes local repository content to a remote repository. | |
The default behavior is to push the current branch to the corresponding upstream branch, but as a safety measure, the push is aborted if the upstream branch does not have the same name as the local one. | |
For example, if you are on the main branch and you run git push, the command will push your local main branch to the remote main branch. | |
If the remote repository does not have a main branch, the push will fail. | |
You can also specify a different branch to push to by using the -u or --set-upstream option. For example, the following command will push the local develop branch to the remote staging branch: | |
> git push -u origin staging | |
The origin is the default name for the remote repository, but you can change it if you want. | |
After a git push, the remote repository will be updated with the latest commits from your local repository. | |
This means that other developers who have access to the remote repository will be able to see your changes. | |
Here is a summary of what happens after a git push: | |
1. The git push command compares the local branch to the remote branch. | |
2. Any commits that are present in the local branch but not in the remote branch are pushed to the remote branch. | |
3. The remote branch is updated with the latest commits from the local branch. | |
============================= | |
GIT INIT, HOW TO SET ORIGIN | |
I create a new repo with 'git init'; How is the remote origin set? | |
============================= | |
When you create a new repository with git init, the remote origin is not set by default. | |
You need to manually add the remote repository URL using the git remote add command. | |
For example, if you want to add a remote repository called origin that is hosted on GitHub, you would use the following command: | |
>git remote add origin https://github.com/<username>/<repository>.git | |
Once you have added the remote repository, you can push your local changes to the remote repository using the git push command. | |
Here is an example of how to set the remote origin and push your local changes to a remote repository: | |
# Create a new repository | |
git init | |
# Add the remote repository | |
git remote add origin https://github.com/<username>/<repository>.git | |
# Add some files to the repository | |
echo "This is a new file" > new_file.txt | |
git add new_file.txt | |
# Commit the changes | |
git commit -m "Added new file" | |
# Push the changes to the remote repository | |
git push origin master | |
Once you have pushed your changes to the remote repository, other developers will be able to see your changes by cloning the repository or pulling the changes from the remote repository. | |
==================== | |
SETTING AN ORIGIN | |
How do I view if a repo has an origin set or not? | |
==================== | |
There are two ways to view if a repo has an origin set or not: | |
1. Using the git remote -v command: This command will list all of the remotes that are configured for the current repository. If there is an origin remote, it will be listed in the output. | |
$ git remote -v | |
origin https://github.com/<username>/<repository>.git (fetch) | |
origin https://github.com/<username>/<repository>.git (push) | |
(-v for verify) | |
2. Using the git config command: This command can be used to view the configuration settings for the current repository. The remote.origin.url setting will contain the URL of the origin remote, if it is set. | |
$ git config --get remote.origin.url | |
https://github.com/<username>/<repository>.git | |
If the remote.origin.url setting is not set, then the repo does not have an origin set. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The command to create and checkout a branch with a single step is: | |
``` | |
git checkout -b <branch_name> | |
``` | |
Here's a breakdown of the command: | |
* `git checkout`: This is the command used for switching branches. | |
* `-b`: This flag tells `git checkout` to create a new branch if it doesn't already exist, **and** switch to the newly created or existing branch. | |
* `<branch_name>`: Replace this with the desired name for your new branch. | |
So, the entire command `git checkout -b feature/single_form` will: | |
1. Create a new branch named `feature/single_form` based on the current branch (which was `main` in your example). | |
2. **Immediately switch your working directory to the newly created `feature/single_form` branch.** | |
This is the most convenient way to create a new branch and start working on it right away. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment