0

I joined a new team recently. My machine was already setup with the local server. and also there's a copy of the codes that I can run in my local (browser). When I did a

git branch -a

I saw a list of all the repositories. and my copy of the codes were checked out from a branch-staging. it means, it's like a clean copy. Now that I'm almost about to finish my own codes, I want my codes to be inside my own branch, and when I commit and push, it should land in my own remote branch. Can you help check if these are correct?

1)

git branch -a myname

2) is my step two correct?, or should it be git push origin myname ?

git push myname myname 

3) if the step two is correct, the next thing is to add my new file codes?

git add file.php

4) before even going to do the step one, when I ran git status ,I already saw a lot of modified files and untracked files even if I didn't even touched those things. Now my question is, in my new own branch, will they exist also?, I wonder how troublesome this is, there's a lot of modified files that I never touched at all, I only want to commit and push my own files in my own branch. so how?

sasori
  • 5,249
  • 16
  • 86
  • 138
  • Does this answer your question? [How do I push a new local branch to a remote Git repository and track it too?](https://stackoverflow.com/questions/2765421/how-do-i-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too) – Henke Dec 05 '20 at 08:01

1 Answers1

1

Without knowing exactly what you checked out and the structure of the project it is difficult to guess. However here go my two cents:

git branch -a shows the list of all branches, both in local and remote repositories. Be careful that it is not the list of repositories as you seem to say in your message.

git branch myNewBranch creates a new branch. Be careful that it just creates the branch, it does not changes into it. To change into your new branch you must check out: git checkout myNewBranch.

Alternately, you can both create and change to your new branch with git checkout -b myNewBranch

git push origin myname uploads your branch myname to the remote origin repository. It does not create the branch, just uploads it to the remote repo.

git push myname myname would upload the myname branch to the myname remote repo (probably would cause error due to using same name for both?)

For commiting changes into your local repo: git add changedFile (stages changes in changedFile) git commit (commits staged changes)

Once commited your changes, you can push them into the remote repository: git push origin branchName (pushes the branchName branch to the origin remote repo)

Remote repositories are added with: git remote add aliasForRemoteRepo urlOfRemoteRepo When using just one remote repo it is usually called origin.

The Pro Git book is an excellent reference. See:

More info on the git reference documentation:

airos
  • 742
  • 5
  • 14
  • As I've said, before even proceeding to step 1, I already saw that the files from which I am currently checked-out is branch-staging, and there's a lot of modified files that I never touched at all. I did my codes in this copy, all new files. Now if I create my own branch. and then do a checkout to my own branch, does that mean, all I have to do is add my new files ? before commiting/pushing my own branch?..will the other modified files that I never touch exist also in my own branch ? – sasori Apr 05 '17 at 15:55
  • There were modified files in your local copy just after checking it out? That sounds really weird. – airos Apr 05 '17 at 15:59
  • git pull updates the local repository with the latests commits in the remote one (merging if necessary). It is recommended to git pull before actually modifying files in the local repository for minimizing risk of collision. – airos Apr 05 '17 at 16:00
  • I forgot to mention that git branch creates the new branch from the last commit in the branch you are in that moment. – airos Apr 05 '17 at 16:02
  • I don't know how it happened. because when the machine got passed down to me, everything was already set up. I'm not sure if the those modified files were copy and pasted that's why they appeared modified...yes, I can do a `git add files` then `git stash save` then `git pull rebase origin nameOfTheCopyFromWhereTheFilesCameFrom` ...then `git stash apply theStashName` to put back my own files..is this correct?..then if no collision, I can finally do my own branch and commit there? – sasori Apr 05 '17 at 16:06
  • `git stash save` saves your working directory and changes not staged but not commited (I think that you don't even need to `git add` files previously, as it also saves the working dir). You can then sync your repo with the remote and revise those other changes afterwords and apply them if needed. `git pull --rebase repo branch` gets the commits in the repo to your local copy but putting your local commits on top of the remote fetched ones. That sounds good for syncing with the remote in your current state. `nameOfTheCopyFromWhereTheFilesCameFrom` is a branch? – airos Apr 05 '17 at 16:38
  • yes it's a staging branch based from its real name. thank you – sasori Apr 06 '17 at 00:17