Git push upstream with dynamic branch name inside an npm script - bash

Is it possible to run an npm script containing a git push command with the upstream option based on the current branch ?
As an example, I would like to be able to run a command npm run push.
This command will do something like git push -u origin ${current-branch} where $current-branch will be replaced by the local current git branch.
I know that this is possible to achieve it by creating a script, but I would like to know if there is already something provided by npm or git to achieve this with the minimal code requirement.
Thanks for the help !
Solution:
Vlad274's solution works.
Steps:
Add a new file .gitconfig on the root of your repository
Apply the configuration with git config --local include.path ../.gitconfig
You will be able to use a new git command which will push with the origin by using the default local branch name

I do this with a git alias, but I assume the same commands would work via npm.
In ~/.gitconfig:
[alias]
branch-name = "!git rev-parse --abbrev-ref HEAD"
pub = "!f(){ git push -u origin $(git branch-name); };f"

Related

commit and push generically with Jenkins in a Freestyle project

I'm trying to commit and push the changes to Bitbucket using a Jenkins Job
The job is a Freestsyle Project with "Execute Shell" as a build step.
What I currently did inside the build step of "Execute Shell" is the following:
python run.py
git config --global user.email "name#gmail.com"
git config --global user.name "name"
git add .
git commit -m 'jenkins.job : update...'
Then I push the changes with Git Publisher.
This works as expected in my local computer but is there a way to retrieve the credentials that I used in Source Code Management with Jenkins variables instead of using git config (because I'm not allowed to use git config error: could not lock config file //.gitconfig: Permission denied)
I fixed it using git -c user.name=".." -c user.email="..#.." commit -m ".."
This way I can set temporary configuration data.

How can i upload a Spring Java project to GitHub so i can show my work?

I've tried through various ways, directly uploading the files wont work. I've tried through bash and nothing.
Do you have git installed?
# Create a new repository on the command line
#Go to Project Directory on your Computer, Open git Bash here
# run the following 1 by 1
git init
git add .
git commit -m "Comment here"
git remote add origin https://github.com/YourRepoPrefixHere
git push -u origin master
If you have a git profile and a repository you can take these simple steps to get it online:
Open a terminal and go inside the folder of the project you want to push to your git repository
Check if you are on the right git branch: git checkout
git add .
git commit -m "your messagge to commit"
git push origin -u "yourbranchName
If you have never set up your origin branch, you should add it before using it:
git remote add origin "github link"
To see if everything went well you can do a simple: git status.
I hope it will be useful to you my friend!

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

How to publish Github repository

I need to publish a Github repository from a directory on my computer.
I added, committed, and pushed the origin master of the files I wanted to publish from the root directory of my project. By that, I mean I clicked the "create a new repository" button and typed all the git commands in my terminal while in the project directory I want to publish as my repository. For a list of the commands I used, see the below section labeled "Code."
I saw no errors during the terminal commands. When I finished, all I saw in my repository was the README.md file and nothing else.
I tried to check at my terminal to see if my login name matched the Github username of the target repository. But I didn't see any commands for checking the login name at the terminal.
OS/config: Using OS X 10.10.5 (Yosemite) on MacBook Air
Code
I tried this...
git init
git add README.md # This is the problem. (See my answer below.) Should be "git add ."
git commit -m "first commit"
git remote add origin https://github.com/"username"/"repository".git
git push -u origin master
Heres a basic push to GitHub, What kind of errors are you getting?
create a new repository on the command line
git init
git add . //This will add everything in the directory
git commit -m "first commit"
git remote add origin https://github.com/"username"/"repository".git
git push -u origin master
…or push an existing repository from the command line
git remote add origin https://github.com/"username"/"repository".git
git push -u origin master
Here is what solved the problem for me...
That second git command line should be git add . not git add README.md. The . adds all the files in the directory, as mentioned by the accepted answer.
Code
Solution
git init
git add . # This was the key line to change from what was tried in the question.
git commit -m "first commit"
git remote add origin https://github.com/username/repository
git push -u origin master

converting a local git folder as a remote repository

sorry if my question is too naive. Here is what i am trying to do:
I want to create a local git repo (git init )-> i did that
I want to access the same form other machines using ssh -> stuck here
I have tried installing git bash , but i don't know how to configure ssh which is built in the git bash.
I did add a ssh config file using git bash and gave as my computername along with domain, since this machine is the server here.
In the git bash guide, they are adding the public key to github, i dont want that
In short i have created a git repository by 'git init .'
I want to access the same using other machines in the same domain using ssh
I don't know how to proceed further, Would anyone be able to help in this case ?
I'd create a --bare repository somewhere accessible via SSH and push, fetch, merge, pull to and from that repository by adding a remote origin to the ones you created.
Something like
/home/git $ git init --bare your_repository.git
/your/current/repository $ git remote add origin /home/git/your_repository.git
And from another machine
/client/path $ git init
/client/path $ git remote add origin user#server:/home/git/your_repository.git
/client/path $ git pull origin
Then
/your/current/repository $ git fetch origin
/your/current/repository $ git merge origin/master
/your/current/repository $ git push origin master
/client/path $ git fetch origin
/client/path $ git merge origin/master
/client/path $ git push origin master
That's just an example, the --bare repository can be anywhere.
Have a look here for pros and cons http://git-scm.com/book/ch4-1.html

Resources