Main Content

Pull, Push and Fetch Files withGit

Use this workflow to work with a remote repository. With Git™, there is a two-step workflow: commit local changes, and then push to the remote repository. In MATLAB®,only access to the remote repository is through thePull,PushandFetchmenu options. All other actions, such asCompare to AncestorandCommit, use the local repository. This diagram represents the Git workflow.

Pull and Push

To get the latest changes, right-click the Current Folder browser and selectSource Control>Pull. Pull fetches the latest changes and merges them into your current branch. If you are not sure what is going to come in from the repository, use fetch to examine the changes first and then merge the changes manually.

Note

在合并之前,您必须注册二进制费尔es to prevent Git from inserting conflict markers. SeeRegister Binary Files with Git.

Pull might fail if you have conflicts. With a complicated change you might want to create a branch from the origin, make some compatibility changes, then merge that branch into the main tracking branch.

To commit changes to the local repository, right-click the Current Folder browser and selectSource Control>View and Commit Changes.

To see if your local changes have moved ahead of the remote tracking branch, right-click the file or white space of the Current Folder browser and selectSource Control>View Details. TheGit informationfield indicates whether your committed local changes are ahead of, behind, or coincident with the remote tracking branch.

将本地提交发送到远程repository, right-click in the Current Folder browser and selectSource Control>Push. A message appears if you cannot push your changes directly because the repository has moved on. Right-click in the Current Folder browser and selectSource Control>Fetchto fetch all changes from the remote repository. Merge branches and resolve conflicts, and then you can push your changes.

Using Git, you cannot add empty folders to source control, so you cannot selectPushand then clone an empty folder. You can create an empty folder in MATLAB, but if you push changes and then sync a new sandbox, then the empty folder does not appear in the new sandbox. To push empty folders to the repository for other users to sync, create agitignorefile in the folder and then push your changes.

Fetch and Merge

UseFetchto get changes and merge manually. UsePullinstead to fetch the latest changes and merge them into your current branch.

Note

After fetching, you must merge. Before you can merge branches, you must register binary files to prevent Git from inserting conflict markers. SeeRegister Binary Files with Git.

To fetch changes from the remote repository, right-click in the Current Folder browser and selectSource Control>Fetch. Fetch updates all of the origin branches in the local repository. Your sandbox files do not change. To see others’ changes, you need to merge in the origin changes to your local branches.

For information about your current branch relative to the remote tracking branch in the repository, right-click the file or white space of the Current Folder browser and selectSource Control>View Details. TheGit informationfield indicates whether your committed local changes are ahead of, behind, or coincident with the remote tracking branch. When you see the messageBehind, you need to merge in changes from the repository to your local branch.

For example, if you are on the main branch, get all changes from the main branch in the remote repository.

  1. Right-click in the Current Folder browser and selectSource Control>Fetch

  2. Right-click in the Current Folder browser and selectSource Control>Branches.

  3. In the Branches dialog box, selectorigin/mainin theBrancheslist.

  4. ClickMerge. The origin branch changes merge into the main branch in your sandbox.

If you right-click the Current Folder browser and selectSource Control>View Details,Git informationfield indicatesCoincident with /origin/main. You can now view the changes that you fetched and merged from the remote repository in your local sandbox.

UseGitStashes

Store uncommitted changes for later use by creating a Git stash. Use stashes to:

  • Store modified files without committing them.

  • Move changes easily to a new branch.

  • Browse and examine the changes within a stash.

To create and manage stashes, in the Current Folder browser, right-click the white space in a folder managed by Git and selectSource Control>Stashes.

In the Stashes dialog box:

  • To create a stash containing your currently modified files, clickNew Stash.

  • To view modified files in a stash, select the stash underAvailable Stashes. Right-click modified files to view changes or save a copy.

  • To apply the stash to your current branch and then delete the stash, clickPop.

  • To apply the stash and keep it, clickApply.

  • To delete the stash, clickDrop.

Related Topics