Creating a github repository from command line - windows

I am creating a new app and I want to be able to create a repository from the command line to be able to add commits without having to go to github and create a repo. I am totally noobian. is it possible? and if so how? Thank you!

for github you could try hub - is a command line tool that wraps git in order to extend it with extra features and commands that make working with GitHub easier.
Examples from doc:
$ git create
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git#github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git#github.com:sinatra/recipes.git

If you want to use git, then it is fairly straightforward, but you won't go far without going through some tutorial. I highly recommend Git Book by Scott Chacon. At least go through chapter 2 and perhaps chapter 3. To answer your questions though: Git is a distributed versioning system so you definitely do not need a repo in GitHub - you can create a repo on your own harddrive and then push it to any other repos (i.e. you could create a GitHub repository later and publish your repository there - it will be an exact clone!).
To create a repo you invoke a command: git init or git init repoName - the former creates the repo in current folder, the latter creates a new one named "repoName".
When you are ready to create a first commit invoke git add . to add all files to index (think of it as an area where you prepare what will go into the next commit) and the git commit -m "Commit message". nstead of adding all files you could also choose files to commit individualy by git add path/to/file
You will need at least some basics with git covered though before you can start using it comfortably.

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).

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.

Migrating forked project from Heroku to Github

Some time ago I found some code in one Github repo. I downloaded it (did not fork it), started upgrading it and when I was happy with the result, I used Heroku as a host. So now the code lives on my computer and Heroku. How could I push it to my Github account, but also give the original author of the project some credit for it (showing on my Github that I actually forked it)?
Okay, so I actually figured it out already!
First, create a new repository on github, let's name it github-project.
git clone git#heroku.com:<heroku-project>.git
cd <heroku-project>
git remote rm origin
git remote add github https://github.com/<github-username>/<github-project>
git pull github master
Now you'll probably see some conflicts. If you want to preserve all your changes, just add them all.
git add .
git commit -m "some message"
git push github master
This is quite simple:
Create an empty repository on GitHub, let's call it github-project
Clone from Heroku, let's call it heroku-project
Add a remote for github
Push to GitHub
The commands to perform these steps:
git clone git#heroku.com:heroku-project.git
cd heroku-project
git remote add github https://github.com/github-username/github-project
git push -u github master
That's it!
Note: if you already created the GitHub project with a README file in it, then it is NOT empty anymore, and the last push will be refused. In that case you can force the push, effectively overwriting the project on GitHub, by using the --force flag, for example:
git push -u github master --force

How to pull and check-out remote Git repository

OK, total noob question, for msysgit on Windows 7, but I have a remote repository (on unfuddle), create on one PC, and now I want to pull it down to another PC. I tried 'Fetch' using Git Gui, but the folder still only has a .git subfolder. It took a while pulling it down, so I assume there is something in the repo, but how do I check it out to a working copy. Nothing I have tried seems to work.
SOLUTION:
It's not directly in his answer, but VonC below pointed me to the unfuddle help documentation for Git, which is as terse as the Git man-pages are dense. The follwing single command got me the working copy and local repository I needed:
$ git clone git#subdomain.unfuddle.com:subdomain/abbreviation.git
Following the Git documentation on Unfuddle, did you declare your unfunddle repo as a remote?
$ cd /path/to/repository
$ git remote add unfuddle git#subdomain.unfuddle.com:subdomain/abbreviation.git
Try also gitk --all: if the fetch has succeded, you should the remote tracking branches (like unfuddle/master). You can then merge it to your master branch in order to finally see files in your (still empty) working tree.
You could also have done a git pull to combine the two steps together (fetch+merge). See this blog post for illustration.
Actually, the OP ProfK reports a cloning issue:
I did already have the unfuddle remote added
git clone git#subdomain.unfuddle.com:subdomain/abbreviation.git
is more suited to get a local repo with the right remote already added to it.

Resources