Where to initialise git in Go project - go

As a complete beginner to Go I'm not sure where to init Git.
The docs here https://golang.org/doc/code.html appear to suggest outside the hello directory early on and then later tell me to run git init inside the hello directory.
Any advice on this would be useful.

The example is clear:
$ cd $GOPATH/src/github.com/user/hello
$ git init
You do initialize the repo within your project 'hello'.
That way:
you can push it to your GitHub repo (that you need to create first on GitHub, empty):
git remote add origin https://<user>#github.com/<user>/hello
git push -u origin master
your go project is "go gettable"
go get github.com/<user>/hello
# that would clone and compile the project in `$GOPATH/src/github.com/<user>/hello`.
The .git you see outside hello (on the same page) is for another project:
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
Here, the project is 'example' and include several packages, including the hello one.

You'd want to initialize it at the same level as it would have been if you'd run go get github.com/user/hello - i.e. in the hello directory.
Basically, if you initialize git, push your repo up an go get again, nothing should change.

Related

Github how to push Xcode project without old files and folders?

I finished an Xcode project and pushed it to github from Xcode 'Source Control' Menu. Then I changed the project name CountryBook to Countries. I Built project and ran. Everything was okay. Then I coiped project folder to desktop as a backup. Then pushed project again. Everything has messed up. Some old named folders and files still exist in repo. Then I deleted every directory and file from github repository. Now, backup version of project is working. But when I try to push it to repo, old files are still exist. I deleted 'origin' from 'Remotes' and created a new repo named 'Countries'. I pushed project again but it was same. A mixed version of old files and new files. When I clone the github version of project, of course it is not runnable. What sould I do and how can I push clean version of my project? I don't want to lose project.
This is Countries repo now:
This is my working project folder with correct content:
I would fix it via command line, lets assume you start from scratch:
Step 1 - prepare the working branch:
Clone the project
Navigate to root folder of the project
Checkout the main branch ("main", "master", or whatever it is)
Create a new branch you will be working with
git clone https://github.com/yourorg/yourrepo
cd yourrepo
git checkout main # or master
git checkout -b fixprojectstructure # branch name can be anything
Step 2 - clean project locally
Delete old project, old workspace, ensure the names in Podfile and Podfile.lock are fine
Build the project and ensure it's working
Step 3 - commit your changes:
# assuming you are in the root of the project
git add .
git commit -m "Some explanation"
Step 4 - push your changes:
I usually do it the lazy way: just run git push, which will show you the proper syntax to push remotely, something like
> git push
fatal: The current branch fixprojectstructure has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin fixprojectstructure
> git push --set-upstream origin fixprojectstructure
Enumerating objects: ...
...
remote: Create a pull request for 'fixprojectstructure' on GitHub by visiting:
remote: https://github.com/.../pull/new/fixprojectstructure
remote:
...
Branch 'fixprojectstructure' set up to track remote branch 'fixprojectstructure' from 'origin'.
Step 5 - merge your changes:
Basically just do what the line above is saying:
Navigate to https://github.com/.../pull/new/fixprojectstructure
Create pull request
Merge pull request to main (or whatever the initial branch was)
Note on the side: configure the gitignore file properly for your repo as well. For starters, follow the gitignore template to create a proper gitignore file, and then change it the way you need to.
For instance:
Usually, if you use cocoapods, you do not store .workspace folder and its contents in the repo. Instead it's generated using pod install command on each machine that needs it.
It's also common to exclude Pods directory from storing in the repo, although there are pro / cons arguments both ways.

How to start new Git from scratch when old Git still there

I want to create a new empty repository with git. I cannot find a simple solution online that explains this step.
This documentation states "git-init - Create an empty Git repository or reinitialize an existing one" but then doesn't say which options are needed to make it empty:
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-init.html
Is a --bare repository the same as an empty repository?
Problem:
I have two different projects with different repo's on github, but when I try to use Git (in Bash) after changing the directory to the new project, it keeps pushing with files from the old project.
I think the problem is that git is using the old repo files and thinks the new folder is just additional files perhaps? Basically I want to start from fresh. Can I just start from scratch on my new project with a new repo?
I have tried $ git init in a new directory, but then it just says: "Reinitialized existing Git repository in /home/user/new_project/.git/"
I tried: $ git remote set-url origin git#github.com:User/New_Project.git
but that just updates where it pushes my new project to, and then includes old projects files.
Please help a noob trying to figure things out the hard way 🙏🏼
Delete .git folder:
rm -rf .git
Then create new git repository:
git init
git remote add origin <remote-URL>
git add .
git commit -m "new clean repo"
git push --force origin master
create 1st project
make git init
create a remote repo for 1st project
link remote repo to 1st project
create 2nd project
make git init
create a remote repo for 2nd project
link remote repo to 2nd project
Git will push everything to its proper remote repo
Small hint from my side
git init : create empty repository and reinitialize an existing one
following link will give more information about different options can be used during git init ($GIT_DIR)
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-init.html

Upload Go project to GitHub Repository

How can i successfully upload my Go project to existing github repository? Here is how my $GOPATH looks like: /home/user/go.
There I have created the following directories: src/github.com/StefanCepa/ . And inside that directory I have 2x directories which represent two different projects. I would like each of those projects to be posted on seperate github repository.
Any ideas how can I do that? Commiting stuff written in Go on github is kinda confusing to me.
This is done the same way that you would initialize and commit a new repository regardless of the language.
Go just makes it a little easier to find your other Go projects thanks to the $GOPATH and the standards of the language. Simple example below.
Within project 1:
$ cd ~/go/src/github.com/StefanCepa/projectOne/
$ git init
$ git add .
$ git commit -m 'init'
$ git remote add origin https://github.com/StefanCepa/projectOne.git
$ git push -u origin master
Within project 2:
*same process as above

What to do if I want to make changes to a forked repository only to find that I can not because the forked package import itself

Like this github repository, I forked the repository and made some changes to the code. When I run it, my changes are not in effect because the original code imports itself. I could simply just change the import library to mine like "import github.com/brucewangno1/ytdl". But is there any other clean way to avoid this?
Another easy way is to use go get instead:
$ go get github.com/original/a_library
$ cd $GOPATH/src/github.com/original/a_library
$ git remote add my_origin https://github.com/myaccount/a_library
In your case it will be
$ go get github.com/rylio/ytdl
$ cd $GOPATH/src/github.com/rylio/ytdl
$ git remote add my_origin https://github.com/brucewangno1/ytdl
Once you are done with changes, commit them and push using below:
$ git push my_origin my_branch
When working with golang forks of a repo, try and work in the import path of the original repo, and update the tooling to use your forked repo.
What I mean is, if you have forked the project github.com/rylio/ytdl/ to your own github account github.com/brucewangno1/ytdl
Then you might have a gopath such as:
$GOPATH/src/github.com
├── rylio
│   └── ytdl
└── brucewangno1
└── ytdl
You should make changes in the directory $GOPATH/src/github.com/rylio/ytdl.
To add the changes to your forked repo, you can set another origin in git, inside your local clone of rylio/ytdl
git remote add fork git#github.brucewangno1/tydl.git
If you do not plan on merging changes back into the upstream repo, you can use tools such as dep to ensure that you pull your own fork of that project instead of the upstream one. Something like:
[[constraint]]
name = "github.com/c/d
source = "https://github.com/myusername/d.git"
This answer assumes that you forked repository
https://github.com/rylio/ytdl/ to https://github.com/brucewangno1/ytdl
You need to create directory named rylio in your $GOPATH and clone your fork into that directory.
mkdir $GOPATH/src/github.com/rylio
cd $GOPATH/src/github.com/rylio
git clone https://github.com/brucewangno1/ytdl
Now, you will not face any issues related to the imports.

uploading code to github

I have created a repository named appengine-testers on github. It is a public repository. Though I easily managed to create a repository but I do not know how to store code there. Do I need to upload the code/folder ?
There are multiple options to do that, i'll just briefly tell the simple one.
git clone ssh-path-to-project
It creates .git folder in the project which is used for references.
cd project
copy the entire project code from any location and paste it in this folder.
Now
Add all the untracked files.
git add .
git commit -am <"commit message">
or
git commit -a
Which automatically takes the changes.Lastly
git push
It pushed the entire code to the repository
One more simple option is do
git init
in the project folder and then change the remote url in the .git folder created inside the project folder
Once you create the repo on github, it'll give you a URL (that looks kinda like git#github.com:yourusername/appengine-testers.git) with push access. From there, you just push from your local repo to that URL.
It's probably easier if you add that URL as a remote. Typically you'd call it 'origin'.

Resources