How to fork your own repo on BitBucket? - fork

How to fork your own repo on BitBucket ?
I know how to fork another user repo from web interface, and I know how to clone my repo.
But how to fork your own repo on BitBucket and ease a future pull request workflow?

Go to your repository, and then go to Actions -> Fork.
If you have the new navigation enabled, then go to your repository, click on the + on the left navigation bar and then Get to work -> Fork this repository.
Also, make sure that forking is enabled in repository settings (for the existing repository).

First, create a new repository 'bar'.
Next, clone the existing project 'foo':
$ git clone git#bitbucket.org:YOURNAME/foo.git bar
Next, edit your Git config file and replace the origin URL with your new URL:
$ cd bar
$ vim .git/config
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git#bitbucket.org:YOURNAME/bar.git #replace foo with bar
Optionally add your original repo as an upstream source:
$ git remote add upstream git#bitbucket.org:YOURNAME/foo.git
Finally, push your new repository up to Bitbucket:
$ git push -u origin master
Now you can push/pull from your new repo (bar) as expected. You should also be able to merge upstream changes using the following command:
$ git fetch upstream
$ git merge upstream/master
credit: bitdrift

On bitbucket server, it does not seem to be an option to fork from your own personal repository. I ran into this trying to move a personal repository to a public location. Possible options:
Get someone else to fork it for you or use a different login if available.
Or
From the project settings, "Move" the repository to the public location, then fork the moved repository back to the original location.

Related

git push to create new GitHub repo not working

I am trying to create a script that prompts for a name to give to a new GitHub repo, and then create the GitHub repo by pushing to the remote URL which the new GitHub repo will have. When I use the script I get these errors:
remote: Repository not found. fatal: repository 'https://github.com/JT-style/great.git/' not found
read -p"Enter the name of remote repository: " name
mkdir ~/rep/$name
cd ~/rep/$name
echo "#$name" >> README.md
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/JT-style/$name.git
git push -u origin main
echo "Done"
Why doesn't this work? What can I do to achieve my goal?
p.s. This is my first script.
You can't push to a repository to github if it doesn't already exist. Usually, you'd create one through a web browser by going on their site, but they released a cli called gh, which allows creating repositories (you have to authenticate somehow before though).

Make a script for automatically updating the links from external server to our internal gitlab server

I am working with a open source repo that I have cloned in my development environment. When I want to update the internal git repo, I enter the folder /home/pranav/Desktop/ and do:
git pull upstream master
Make edits, save, git add , and git commit all in your local repo
Push changes from local repo to your fork on gitlab.com ( git push origin master )
Update the central repo from your fork ( Pull Request )
But then, every morning I have to do the same thing for the updating the repo. This is quite troublesome.
Can I put this in a Bash script? I mean, if I write these git commands in the shell script, and run it, will I be able to update the repo automatically whenever there are any commits in the original repo?
other way: 1. git fetch- check for any commits
2. if commits found, then git pull
3. Go with stashing for any local changes.
I was thinking of writing something like this...

How to push a new code to an existing git repository in github

I need to push my modified new java code to my old git repository in github but I do not have old code in my pc. How to do that?
I had push a code before my github account before. Now I don't have that old code in my pc. How do I pull the project into my pc and after making changes, push again to the same repository?
I do not have much experience in github, so please help me to improve skills on github.
Check your remote first to see where it is pointing to by
$ git remote -v
origin ssh://git#<old-git-url>/<project>.git (fetch)
origin ssh://git#g<old-git-url>/<project>.git (push)
Change the pointing to GitHub
$ git remote set-url origin ssh://git#<github-url>/<project>.git
Now your repo is pointing to Github
Now you can make your changes and then add them and do a commit and finally push to remote branch; say you are on master.
git add <file>
git commit -m <commit message>
git push origin master
I had to do just that and achieved it like this.
Setup. I assume you have a new remote repository with files that may not cause a conflict with the directory you currently have locally.
Git clone from the git repository you need to push to. Just make sure you create a new directory for the cloned code.
Copy the contents of the cloned repository into the local directory that has your current code. Make sure to copy the .git (hidden) file.
cd into your local directory and run git remote -v. You should see the remote repository git address.
git add -A to add whatever change you require and commit it.
Finally git push
You need to make sure that your local repository (the one that is on your computer) is connected to the remote repository (the one that is on the GitHub servers).
After this, you need to add the modified file to the staging area. Say, you have a file test.txt that you have modified, you would add it to the staging area by typing
git add test.txt
After that you would need to commit those changes. You can do that by
git commit -m "commit message"
And that's it, you have now saved those changes and recorded them in the version control. But the changes that you made have only been recorded in your local repository and you would need to push these changes to the remote repository (the GitHub servers). You can do this by
git push origin master
It would take a few seconds (depending on your internet speed and the project file size) to push these changes to the remote servers. Once it's done, you can open that repository on GitHub and see the changes for yourself.
Just to amend this a bit. I believe "master" is now "main" in some instances or platforms. If this answer isn't working for you, try swapping that out. It worked for me.

fatal: remote <Repository> already exists

As part of the development of a CI & CD flow for the company I work at, I am building a command line program (Bash script on OSX) that
creates a new local Git repo
adds some default branches to this repo
Then adds a new repo to Bitbucket using the next code:
gitUserName = Joris <-- provided by the user, this is an example
projectName = TestProject <-- provided by the user, this is an example
git remote add $projectName "https://bitbucket.org/$gitUserName/$projectName.git"
After running this command, I don't see the repository on my Bitbucket account on the website. When I try to re-run this command, it says the repository already exists.
Also, when I run git push $projectName master it says fatal: repository 'https://bitbucket.org/Joris/TestProject.git/' not found
This behavior seems inconsistent, and I have followed the Atlassian guide to set this up so I don't really understand why it doesn't add the repository as expected. I do realize that I can also just go on the BitBucket website and add the repository manually, but the purpose of my program is that it generates a fully set-up repository for a user based on as little commands as possible.
The git remote add documentation says that the command adds a remote to the local repo. This terminology is, IMO, a bit off; it would better to say it adds a remote configuration to the local repository (i.e. configures the repo to access a remote). This does not actually create the remote repo; that must be done separately.
In the case of bitbucket, the "normal" thing to do is to go to the website and create the repo through their UI. Because you're trying to automate things, you don't want to do that; so in that case, you would need to use the BitBucket REST API, which is documented here: https://developer.atlassian.com/server/bitbucket/reference/rest-api/
The "Core API" section talks about repositories and permissions, so you should be able to script out requests to (if necessary) check if the repo exists and set it up if it doesn't. You'll just need a way for your script to send HTTP requests and receive the responses.
In your machine:
Create repo:
git init
Add branches:
git checkout -b branchX
git checkout -b branchY
git checkout -b branchZ
In Bitbucket website:
Create new repository named TestProject, allow write permissions to user Joris in settings and save. Finally copy the url of the repository, this must be something like bitbucket.mydomain:port/nameofproject/testproject.git (Notice this is all in lowcase)
In your machine:
git remote add origin theURL
git push origin *:*
git push origin --tags
The last is the command to push all your local repo, this will overwrite the history and tags in your remote repo, but since is a new repo it doesn't matter.

Git sync with GitHub repository

I have a desktop PC and a laptop, I use both daily to commit to the same repository on GitHub (via Git). I have just moved over and started using Git, which is great. However, what command do I use when, for example, I have made changes on my desktop PC and committed them to GitHub, and then I want to move all of the changes currently on GitHub onto my laptop files to resume my work on my laptop where I left off on my desktop PC / GitHub?
Thanks in advance.
Clone a repository into a new directory ;
git clone uri_repository.git
Incorporates changes from a remote repository into the current branch.
git pull
Show the working tree status.
git status
Update the index using the current content found in the working tree.
git add .
Record changes to the repository.
git commit -m 'message'
Update remote references (refs) along with associated objects.
git push
To fetch your code from your remote repository from GitHub you use the command git pull. This fetches the latest changes and merges them with your local code so you have the most recent code. You can find a Git cheat sheet with most of the commands you will need here
You can use the following command to syn your remote repository to your local repository
/* origin points to your remote repository, master is the branch in which your working */
git pull origin master
Incase you dont have remote ie origin setup then
/* Here your aliasing your remote repository location to origin, you can name it anything */
git remote add origin your_git_repo_url
In case if you dont know which branch your currently working then
/* This will list all the branches with * preceded which is active one */
git branch
Once you do your local changes then you may want to push the code to remote repository which can be done as follows
git add .
OR
git add file1.txt file2.txt
git commit -m "You commit description"
git push origin master

Resources