759

I've got a project checked locally from GitHub, and that remote repository has since had changes made to it. What's the correct command to update my local copy with the latest changes?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom
  • 33,626
  • 31
  • 85
  • 109
  • 34
    Its worth noting that github have produced a set of very informative and helpful guides for using git and github. I found them invaluable when I first made the move to git. https://help.github.com/ – Mark Embling Sep 18 '09 at 08:33

10 Answers10

1054

Probably:

was: git pull origin master

now: git pull origin main

James Healy
  • 14,557
  • 4
  • 33
  • 43
  • 32
    It complained: "You asked to pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line." So I tried "$ git pull origin master" and it worked fine. – Juan Lanus Jan 09 '14 at 18:40
  • 8
    git pull origin master – Andrzej Rehmann Dec 02 '14 at 20:36
  • 1
    I deleted some files and it is not bringing them again, any idea? – Aquarius Power Feb 14 '15 at 02:44
  • 2
    do we do a git commit before running this? everytime i run this command, i end up with 18347213846 modified files that i didnT even touch!!! – Orkun Feb 20 '15 at 19:38
  • 8
    I just learned the hard way that pulling a new remote repository branch does not create a branch of that name locally, but instead pulls that remote branch into whatever local branch happens to be checked out. I'm not sure how to undo this short of ditching and re-cloning the repository. In any case, it seems that the local branch is independent of the remote branch and one should always make sure you're in the intended local branch before pulling. – Joe Lapp Jan 07 '16 at 18:58
  • Why we want to do merge ,but I think fetch is OK .What if code is diseased or not healthy contribution? Is git pull one is safe command? – TheExorcist Oct 30 '17 at 08:21
  • If I made changes lokally but don't want them be merged, just get the exact copy of the remote repo, then I have to delete or rename my lokal repo and just clone it again, right? – franc Mar 06 '18 at 12:49
  • @JuanLanus why additional origin master required? won't git pull will be sufficient? – Ankush Jain Jun 18 '18 at 05:24
  • @Ankush: for most of us there is no need to name a branch because we work on a single branch "master", but multi-developer teams usually work in many branches at once, thus the requirement to identify it. For Git, the "master" name is not special, is just another branch name. – Juan Lanus Jun 19 '18 at 11:20
  • franc I always try to remember to take a full copy of my current local directory, because I don't know git arcana well enough to undo anything strange - like Joe Lapp's problem above – WillC Oct 24 '18 at 11:36
  • 1
    `git remote -vv` and `git branch -vv` might help you understand what's going on too: first one gives you the details of the 'remote' repository - the repository on the server, not your local/working repository: second one gives you the details of the branch from your local repository you have checked out, and the details of the corresponding branch from the remote repository . – WillC Oct 24 '18 at 11:48
  • 2
    2021 update: `git pull origin main` Many repos are moving away from master - and Github repo's default is now `main` – Troper Jun 02 '21 at 18:31
135

This should work for every default repo:

git pull origin master

If your default branch is different than master, you will need to specify the branch name:

git pull origin my_default_branch_name
waxwing
  • 117
  • 5
Andrzej Rehmann
  • 12,360
  • 7
  • 39
  • 38
74
git fetch [remotename]

However you'll need to merge any changes into your local branches. If you're on a branch that's tracking a remote branch on Github, then

git pull

will first do a fetch, and then merge in the tracked branch

Gareth
  • 133,157
  • 36
  • 148
  • 157
  • 6
    If you are using the `git fetch` method, you'll also want to fetch tags with `git fetch -t`. If you're satisfied with the changes (`git log HEAD..FETCH_HEAD`), you can then merge them in with `git merge FETCH_HEAD`. – Brad Grissom May 21 '14 at 22:44
25

This question is very general and there are a couple of assumptions I'll make to simplify it a bit. We'll assume that you want to update your master branch.

If you haven't made any changes locally, you can use git pull to bring down any new commits and add them to your master.

git pull origin master

If you have made changes, and you want to avoid adding a new merge commit, use git pull --rebase.

git pull --rebase origin master

git pull --rebase will work even if you haven't made changes and is probably your best call.

Joe McMahon
  • 3,266
  • 21
  • 33
  • 2
    Rebase, and the dangers of rebase: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase. Paragraphs 2 and 3 of the "Understanding the dangers of rebase" really make me miss SVN's command set. – WillC Oct 24 '18 at 11:42
17

With an already-set origin master, you just have to use the below command -

git pull "https://github.com/yourUserName/yourRepo.git"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shailendra
  • 189
  • 1
  • 3
12

Complete Workflow for check out a branch and pull changes from master

Pull all remote branches

git pull --all

List all branches now

git branch -a

Download your branch

git checkout -b <feature branch name copied from list of branches above>

Shows current branch. Must show <feature branch> with * In front of it

git branch

Checkout changes from master to current branch

git pull origin master

OR checkout any other <feature branch> into current branch

git pull origin <feature-branch>

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
7

To pull from the default branch, new repositories should use the command:

git pull origin main

Github changed naming convention of default branch from master to main in 2020. https://github.com/github/renaming

christok
  • 111
  • 1
  • 6
  • 1
    Regardless of GitHub's policy change, main might not be the GitHub default branch. The user can specify a default branch name. If your goal is to pull from the GitHub default branch then there is no need to specify a branch...main, master, or otherwise. "git pull" will act on the default remote and the default branch. – K. hervey Mar 15 '21 at 12:48
1

Use this to update your local repo with the updated one in remote repo like Gitlab, Github, etc.

git pull origin master
  • 1
    This exact answer has already been provided multiple times. Please upvote existing answers instead of adding new answers, unless you have a new approach. – Jeremy Caney Jul 16 '22 at 19:36
0

After Git Clone, if want to get the remote branches use

git fetch --all

Then checkout to the branch you want

git checkout the-branch-you-need
Daisy
  • 288
  • 3
  • 8
0

git pull origin master // Change in github, it take effect in local reprository

NB: Working Fine

All about git: https://gist.github.com/subrotoice/b27f7d20617bb3da04827a223faf6450

Complete Documentation
googel>>Install Git
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git // Type "git" to check it work or not?

#Basic Windows command like cd
cd\  = back to root directory c drive does not metter where its current postion 
cd .. = One step back
cd /d D: = C Drive to D drive
dir or ls(LS)  = List all file and folder of current directory. "ls" is more clear to read
mkdir mynewfolder = Create New Folder
cd "folderName" = To enter Folder for doing some task
cls = Clear Screen

#Upload A full new project (Make sure no file there even readme.md to avoid error)--Working==========
git config --global user.name "subrotoice"
git config --global user.email "subroto.iu@gmail.com"

git init    // Basically 3 steps, 1. add, 2. Commit, 3. Push
git add .   // Add to local repositories
git commit -m "first commit"  // Commit to local repositories
git remote add origin https://github.com/subrotoice/ccn.git  // ("origin user-defined", origin=url.git, variable e value assign korar moto)
git push -u origin master  // push,  origin user define name like variable contain url. (master default brunch name, you can create brunch like, https://prnt.sc/26pq9x2

#master(default),  If you want to create other brunch not master(default), here brunch name is "main", user-defined name
git branch -M main // Create new branch main
git remote add origin https://github.com/subrotoice/33sfdf.git  // origin(any name) is variable name contain url, age url assing thakle ei line dorkar nai
git push -u origin main

git branch // Show current branch
git checkout master // Switched to branch 'master'

#Work on existing Project----------------
First you have to download project otherwise it will not work
git clone https://github.com/Tilotiti/jQuery-LightBox-Responsive.git   // Pull
cd folder_name // Need to change to inside folder
git add . For all new file and folder (git add file_names.exten  it is for single file)
git status  // to check the status of git files [optional]
git commit -m "committed message" For asingle file(git commit -m "committed message" file_names.exten)
git push -u origin master
git pull origin master // Change in github, it take effect in local reprository. 'Synchronization'

# Clone a Specic Brunch, in stade of main brunch master
git clone --branch <branchname> <remote-repo-url>
git clone -b <branchname> <remote-repo-url>
git clone -b main9 https://github.com/subrotoice/test9.git  // Working, Here brunch name main9
git push -u origin main9 // Push to main9, Error: if use master as brunch name
git pull origin master // Change in github, it take effect in local reprository 

#VS Code--- Command dorkar nai, Sob visually kora jai
https://www.youtube.com/watch?v=2oihkInZ880  (Hindi)
1st time step: -----(Local: 1-3, Remote: a-d)--------------
1. Initialize Repository // https://prnt.sc/V7oDXeeOi9CO
2. Commit  // Visually Commit
3. COnfig Git(If ask)

a. Add Remote  // Visually Commit https://prnt.sc/-IWSFNeadc1H
b. Push  // Commit and push option ase vscode
c. Github Auth
d. Push Again (If required)

2nd Time (Old Project):
1. Pule (clone) // https://prnt.sc/K2us0_eYZFuq
2. Commit

a. Push
# https://prnt.sc/5ii9wCPT9Qut // Change in github, it take effect in local reprository
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Subroto Biswas
  • 553
  • 7
  • 5