Automatically copy correct CLI command to clipboard after failure - bash

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

Related

Git push error - failed to push some refs to <path>

I am trying to upload my code onto a repository I created on github but I get an error.
Assuming the URL of the repository is https://github.com/a, how do I go about uploading my code to my remote repository?
I tried the following commands on git bash after navigating INSIDE the folder I want to upload -
git init
(output - Reinitializing existing Git repository in C://(path)/.git/
git add.
(blank output)
git commit -m "Initial commit"
(output- On branch main. nothing to commit, working tree clean)
git remote add origin https://github.com/(path).git
(output - error: remote origin already exists)
git push origin master
(output - error:src refspec master does not match any
error failed to push some refs to https://github.com/(path).git)
Why is this happening and what could I do to push my code to my remote git repo? (I want to learn how to use git via cmd)
Thanks for reading my query!
note that you already have an remote repository configured as origin you must remove this configuration or rename then add the correctly repo.
the git command to remove remotes repositories by name is git remote remove ${repo_name}, in your case:
git remote remove origin
the git command to rename remotes repositories is:
git remote rename ${old_repo_name} ${new_repo_name}
then you can add an new remote with the name origin
git remote add origin https://github.com/(path).git
you can assume that the git command git remote add origin https://github.com/(path).git says to git configure an new remote repository into the current local repository, this remote config will "point" to the <URL> and the "alias" of this <URL> will be "origin" (or the name you specify between add and <URL>).
in this case <URL>=https://github.com/(path).git
With this you can have various remotes and specify then when you execute git push ${repo_name}
You can set an "default" upstream for git pull/status with git push -u ${repo_name}
You wrote:
git commit -m "Initial commit"
(output- On branch main. nothing to commit, working tree clean)
git push origin master
(output - error:src refspec master does not match any
Your init created a branch called main but you tried to push master, which doesn't exist. Try pushing main instead.

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.

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.

How to do push for new repository?

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

Resources