29

Can you explain me how to use git in case, when I have access to repository:

  • How to download repository
  • How to push changes in selected branch
  • How to select branch to push

I tried these steps

git init
git clone git.repository
git pull develop  (where develop is branch)
git add .
git commit -m "m"
git push origin develop

Result is:

* branch            develop    -> FETCH_HEAD
fatal: refusing to merge unrelated histories

What I do wrong?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Darama
  • 3,130
  • 7
  • 25
  • 34

3 Answers3

38

3 Steps to Commit your changes

Suppose you have created a new branch on GitHub with the name feature-branch.

enter image description here

FETCH

    git pull --all         Pull all remote branches
    git branch -a          List all branches now

Checkout and switch to the feature-branch directory. You can simply copy the branch name from the output of branch -a command above

git checkout -b feature-branch

VALIDATE

Next use the git branch command to see the current branch. It will show feature-branch with * In front of it

git branch   check current branch
git status   check the state of your codebase       

COMMIT

git add .   add all untracked files
git commit -m "Rafactore code or use your message"

Take update and the push changes on the origin server

 git pull origin feature-branch
 git push origin feature-branch

OR you can rebase with the master before commit

git fetch
git rebase origin/master
git push origin feature-branch
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
13

How to download repository

# download a repository
git clone <url>

How to push changes in selected branch

# push changes to remote branch
# assuming you are on the master branch right now
git push origin master

How to select branch to push

 # push any desired branch to remote
 git push -u origin local_branch_name:remote_branch_name
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
3

First i would like to suggest you to read this nice documentation.

Prerequirements:
- Installed git.
- Have access to the repository.

  1. You can download repository following these commands:
    git clone <depositry https or ssh link>

  2. First you need to add changes, create commit, add remote branch and then push.
    git remote add <repo name> <repo url>
    Example if you use BitBucket: git remote add origin ssh://git@bitbucket.org//.git
    git status // to see all changes (they should be printed with red color)
    git add <file name> // add the file as git staged
    git commit or git commit -m 'some message'
    git push <remote name> <branch name>

MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • git remote add whee repo name is? – Darama Mar 15 '17 at 21:27
  • When I did clone it downloaded files, but onlt git directory – Darama Mar 15 '17 at 21:29
  • Here is a example if you use bitbucket: git remote add origin ssh://git@bitbucket.org//.git – MilanNz Mar 15 '17 at 21:29
  • Ok, in that case your link will look like this: git remote add origin https://github.com//.git – MilanNz Mar 15 '17 at 21:48
  • bwm,when you create new repository on GitHub, BitBucket, GitLab and etc. you will get instructions how to init git, what is the remote branch url and etc. – MilanNz Mar 15 '17 at 21:50
  • In that case you already have added remote branch if you have cloned repository. You can check it using command: git remote -v – MilanNz Mar 15 '17 at 21:58
  • it returns: develop git@github.com:Trading.git (push) – Darama Mar 15 '17 at 21:59
  • Ok then you have remote branch. So next step would be to push your local changes (commit) to the remote repository, but you can push commit only if you have rights to do so. Or you can fork project and then clone it and push – MilanNz Mar 15 '17 at 22:02
  • I tried: `e:\abs\test>git push origin develop error: src refspec develop does not match any.` – Darama Mar 15 '17 at 22:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138157/discussion-between-darama-and-milannz). – Darama Mar 15 '17 at 22:07