How to do push for new repository? - macos

I created new repository (git#github.com:derkode/ForvoClient.git) and did SSH Key, then:
git config --global user.email "my_email#mail.com"
git config --global user.name "my_nickname"
git config --global push.default simple
git init
git add *
git commit -m "First commit"
git remote add origin git#github.com:derkode/ForvoClient.git
But after: git push -u origin master
! [rejected] master -> master (non-fast-forward) error: failed
to push some refs to 'git#github.com:derkode/ForvoClient.git' hint:
Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git
pull') hint: before pushing again. hint: See the 'Note about
fast-forwards' in 'git push --help' for details.
What is it?

Your repo on GitHub already has a commit.
https://github.com/derkode/ForvoClient
This is normal when you create a repo with a README file.
You can fix this either by force pushing your local repo to GitHub, but you will lose the README file this way:
git push -u origin master -f
Or, you can merge the version on GitHub into yours and then push it back:
git pull origin master
git push -u origin master
Or, as #xbonez suggested, rebase your version on top of GitHub's version:
git fetch origin
git rebase origin/master
git push -u origin master

If you want to get rid of the commit that Github created for you with the README file, follow janos's answer. If you want to keep that commit, and push yours over it, simply pull down those changes and then push:
git fetch origin && git rebase origin/master && git push origin master

Related

Laravel - bitbucket pull / push

I'm totally confused how I should add my laravel project to my bitbucket profile and repository. That project that I am adding should be used by me and my college colleagues and they all have to push their changes to the project. How it works ? And how can I pull those changes to my local project on my PC ?
Please help. Thanks!
This can be achieved by creating a repository on bitbucket. Then navigating to the project on your local environment and running the following commands:
cd my-laravel-project
git init
git remote add origin git#bitbucket.org:yourusername/my-laravel-app.git
git add -A
git commit -m "Initial commit"
git push -u origin master
Colleagues can then clone the repository using the following command:
git clone git#bitbucket.org:yourusername/my-laravel-app.git
cd my-laravel-app
after making changes they can be committed using:
git status
git add -A
git commit -m "Fix database connection"
git push
The changes made by colleagues can be retrieved using:
git pull
Merging can be achieved by first pulling the target branch into the current branch then comparing and committing the changes:
git pull origin master
// review the changes
git add -A
git commit -m "Merge changes"
git push

Unable to Git push to a remote branch (not master) from azure pipeline

I have been trying to do a Git Push from a bash task on Azure DevOps. I was already pushing the code to the master branch without any issues.
But now I have to push it to the dev branch, and for some reason, Git Pull works but not Git Push. These are the steps I followed. I tried various versions of this actually, but nothing worked.
git config --global user.email "xxx#gmail.com"
git config --global user.name "abc"
git pull https://PAT#github.com/repo.git HEAD:dev
git add $PROXYNAME
git commit -m 'Auto-checkin of $PROXYNAME proxy to Git'
git show-ref
git push https://PAT#github.com/repo.git HEAD:dev
The above worked when the push / pull had HEAD:master at the end.
This is the error I have been getting each time.
To https://github.com/repo.git
! [rejected] HEAD -> dev (non-fast-forward)
error: failed to push some refs to 'https://***#github.com/repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Edited to add:
I have also tried with a checkout command - to checkout that dev branch, and no luck!
Result of git remote -v:
origin https://github.com/repo (fetch)
origin https://github.com/repo (push)
The latest error am getting is below:
error: src refspec dev does not match any
error: failed to push some refs to 'https://***#github.com/repo.git'
Could anyone please let me know what I need to do to get this working. This seems to be a recurring issue for me now :(
I believe you need to configure git remote to your correct repository.
The result of git remote -v should refer to your current directory remote association.
Start with git remote --help
Especially:
git remote set-url [--push] [] git remote
set-url --add [--push]
Here is a good tutorial on git remote.

Automatically copy correct CLI command to clipboard after failure

Suppose I run:
git push
And I get the error message:
fatal: The current branch cdt-rd has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin cdt-rd
Is there a way to conjigger my git or bash so that it automatically copies the correct command (git push --set-upstream origin cdt-rd) to my clipboard?
Would probably be easier to create a smart alias, I use this one:
git config --global alias.pto '!bash -c "git push --set-upstream ${1-origin} $(git symbolic-ref --short HEAD)" -'
Pushes to the origin by default git pto or to another remote git pto remote
It automatically resolves the current branch name, then pushes the branch to the remote and sets the upstream. Also works if you use this command instead of push if the upstream is already set.
git config --global push.default current
You should only have to do this once. Then,
git push

Unable to update my GitHub from my Mac

Why am I unable to update my GitHub from my Mac?
git status produces the following:
On branch master Changes to be committed: (use "git reset HEAD
<file>..." to unstage)
new file: file.test
Untracked files: (use "git add <file>..." to include in what will be
committed)
.DS_Store
git push produces the following;
fatal: The current branch master has no upstream branch. To push the
current branch and set the remote as upstream, use
git push --set-upstream origin master
You need to git add ., then git commit -m "My commit message", then git push -u origin master.
That will set the upstream to the origin/master. Moving forward, just git push shall suffice
Git works through commits - which are basically snapshots of what your repository looked like at a given point in time. You need to commit the files to your repository before you can actually push them to GitHub.
So your first step is to add the files to the "Staging Area" with
git add file.text # you can do git add . but this will add all files which you may not always want
Now you can check the current status of the "Staging Area" with
git status
This will let you make sure that you are only committing the changes that you want added.
Now you can commit the changes. Committing will only 'save' the files that are in the Staging Area.
git commit -m "A useful description of what you did since your last commit"
Ok so now you're ready to push. Assuming you cloned from GitHub you can just run
git push origin master
But if you created this repository with git init you will need to tell git that you have a remote repository somewhere. Do this by running
git remote add https://github.com/<usernane>/<repo_name> origin
This origin is the name you would like to associate with the remote repository. 'Origin" is the most common but you may have other remotes like "backup" or "code_review" for different use cases.
Once you have added the remote repo, you can actually push to it with
git push origin master
Again, origin is the name for your remote repo and 'master' is a 'branch' name.
You can add the -u flag which will make it so that git assumes you want to push to origin. So in the future you would only need to run
git push
Listen to the error messages. :)
You currently have one untracked file, .DS_Store which I happen to know is a system file, so you may want to add that to your .gitignore
As for trying to push upstream, you need to set your upstream for each branch, so you simply need to type the command
$ git push --set-upstream origin master
then do a simple
$ git push
to send your changes to github.
If the branch you are pushing to online is ahead of you, then you may need to do a git pull to get grab the changes, before you do a git push.

Migrating all branches of GitHub repo to new location sends just Master as each origin/<branch>

I was very excited to get multiple repos migrated, but then come to find out my branches are all of master. Any thoughts on my mistake? Here is my process to migrating, specifically from a GHE to a GH account.
git clone <GHE repository>
cd <repository name>
for remote in `git branch -r | grep -v '\->'`; do git branch --track $remote; done
git remote set-url origin <public GH repository>.git
git push -u --all origin
I am obviously attempting to track and migrate all branches, and if I am asking too much we can do one at a time if this is an issue fixing.
Attempting to simplify the steps, I used...
git push --all origin
and...
git push --mirror origin
With only master pushing to GitHub. --mirror did actually clean up the bad branches that duplicated master.
So after some investigation with the team, this looks to be a clean migration process if all agree...
git clone <orig repository>
cd <repository name>
for i in `git branch -a | grep remote`; do git branch --track ${i#remotes/origin/} $i; done
3) Change your local copies origin to new location
git remote set-url origin <new repository>
4) Push it to new repository (as mirror of old)
git push --mirror
Before I accept, I would consider modifications and a better practice if exists.

Resources