Gitlab how to pull down current code on master to the local branch - gitlab

How does one get the code from the master to the local branch called "mybranch" so the local branch has the latest code? Assume local branch did not have code changed since it was created from master and now it is few versions behind the master.

I've ran the following commands, which pulled the latest changes from master.
git checkout mybranch
git pull origin master

You can do that with
git fetch upstream
git merge upstream/master
Assuming that you are on your local branch.
source

checkout to your branch:
git checkout mybranch
then update master and rebase your branch.
git pull --rebase origin master
this is in case you don't mind rebasing and also updating master.
You could also not rebase (say, if mybranch is in the remote and you don't want to remove it to be able to push later) and just merge with
git pull origin master

Related

How i can overwrite changes on this file?

I am trying to deploy my Laravel project on my live server, but i have a problem with composer.json file
no#zz8807:/var/www/html$ sudo git pull origin master
Username for 'https://github.com': whiteG9291
Password for 'https://whiteG9291#github.com':
From https://github.com/aekraes/zuxweb
* branch master -> FETCH_HEAD
Updating b19275b..4127e3a
error: Your local changes to the following files would be overwritten
by merge:
composer.json
Please commit your changes or stash them before you merge.
Aborting
this is the error I get.
Notice: I have deployed this before and it worked successfully, but I have done some changes on composer.json permissions
You cannot take a pull with uncommitted changes, so to pull changes with some local uncommitted changes you have two option ->
Stash your changes
commit your changes
Stash changes
git stash
git pull origin master
If you pop your stashed changes now, you can merge with the pulled changes,
git stash apply
Commit changes
git add composer.json
git commit -m "updated composer"
git pull origin master
Now you will to merge the data which came from the origin with your local.
After merge in both cases commit your merged changes,
git add composer.json
git commit -m "merged"
git push origin master
If you want to keep your local changes then commit them to your git repo:
git add composer.json
git commit -m 'your message'
git push
If you don't want to keep your changes to composer.json, just stash them on the stack and never fetch them down, then you can pull from master
git stash
git pull origin master

git push hangs weird

When I want to push new contents to my Github, I used
git push
or
git pull
It's not working, like this but I used it successfully one hour before.
So I tried
git push origin master
Now it's success, magic.
But I don't know why about it, and how I need to do to go back that just needs input 2 words.
I tried
git config --global sendpack.sidehand false
Thanks in advance.
Add:
git push -u origin master
told me "Branch master set up to track remote branch master from origin"
I know that's right, but git push still can't work as expected
The configuration you mentioned is unrelated to your problem (you should probably undo that). If you want to set up the master branch on the origin remote repository as the default branch to push to from your current branch, run once
git push -u origin master
# or
git push --set-upstream origin master
and then use just git push afterwards.

GIT hard reset all from local folder (overwrite only on localhost)

I have some problems with my project so i need to pull all from master again.
When i type git pull it give me that is everything is all up to date, but i can see on my project that my project have some issues on my local.
How i can pull all from master, but i need to rewrite all files from master to my localhost?
Really important is that i need to rewrite files just on my local from master, because i work for company and if anything happen to project it will be my foul :)
I have tried with --hard too, but nothing changes.
To make your local master identical to its remote counterpart :
# let's save your local changes (if any)
git stash
# make sure to be on master branch
git checkout master
# let's save your current master branch (to allow for rollback)
git branch backup_old_master
# make sure to get last version of remotes
git fetch
# reset to remote version
git reset --hard origin/master

How does Git know which repository to push to?

I'm a complete noob when it comes to version control, but I recently started using GitHub to host some of my projects. I blindly use the command git push origin master to push changes to either of the two repositories. I don't understand how Git knows which repository to push to. I use the same command to push to each. Does the directory I'm in have anything to do with it?
Thanks for clearing this up for me.
A word of advice, "blindly use"ing anything is a bad idea.
git has a system of remotes which allows to specify URLs and transports to repositories other than the one where you're working. git push origin master pushes the current branch to the remote called origin as the branch master. You have a remote called origin. This is created by default when you clone a repository from a URL.
git remote add origin http://abc.com/def/ghi.git tells git the url that remote 'origin' points to.
When you use git push origin master, you aren't pushing to two repositories - you are pushing to the master branch in your repository who's name (alias) is origin.
Think of it like this:
I'm pushing my stuff to origin which is my repository address. What am I pushing there? My master branch.
If you are new and set git up yourself through git init, you probably aren't pushing to two repositories. Run this command to see what your remotes are:
git remote -v
This will return something like (assuming you have a git hosting service like github):
blahblah git#github.com:yourGithubName/fileName.git (fetch)
blahblah git#github.com:yourGithubName/fileName.git (push)
git#github.com:yourGithubName/fileName.git is your repository address. Some addresses are prefixed by https:// .
Git repositories contain a .git directory that contains metadata about the repository. That's what Git uses to determine where to push your changes.

'Your branch is ahead of 'origin/master' by 1 commit.' after merge

I have done the following:
git add <filenames>
git commit
git push origin master:mybranch
From my origin server, I did the following:
git merge mybranch
All of my changes are now present on the origin server, however git (and myself) seems incredibly confused now. When I did a git status on the origin, two of the new files I added were present, but listed as untracked files. However, the modified files are not listed as modified.
When I do a git status on my remote server I receive the error Your branch is ahead of 'origin/master' by 1 commit.
When I do a git diff origin/master on my remote server all of my changes are listed as not present on the origin server.
What is going on here?
do a git pull in your master and then merge again
Which branch are you currently on?
usually this is how it goes:
git checkout somebranch
*make some changes
git add *any new files here*
git commit -am "commit message"
git checkout master
git pull origin master
git merge somebranch
git push
if the master-branch had any changes in it when you pulled you will need to merge those changes over to your working branch.
git checkout somebranch
git merge master
you have only pushed the mybranch to your remote. This is why after you merge it into master on the remote you get a "ahead by 1" message. This is not an error.
Do a git fetch and you will have the latest in sync.
Sounds like you don't understand how Git's history structure is built. Google for "git for computer scientists" to get a quick overview, or spend some time reading progit.org/book.

Resources