CSC Git Guide

Push Your Local Repository to Your Remote Repository

Push Your Local Repository to Your Remote Repository

When you have your project code in a satisfactory state and want to send your local commit snapshots to your remote NCSU GitHub repository, you need to push your code.

Reminder: Check Your Submission

After a push, you should always go to your NCSU GitHub repository to verify that the files are indeed there.

You can push by using Git Bash or EGit.

Push Using Git Bash

To see the remote repositories currently configured in your Git, execute the following command:

 git remote -v

The -v flag shows the specific URLs that Git has stored for each remote repository.

For example:



Figure: Remote Repositories in Git Bash


In this example, Git tells us that the “short name” for the csc216-001-GP1-021 repository URL is origin. When you clone a remote repository, the shortname defaults to origin unless you specify otherwise.

To push your local commits to NCSU GitHub, execute the following command:

 git push [shortname] [branchname]

The [branchname] will be master, by default.

While not required for this course, you can read more about creating other branches and merging branches in the Pro Git Book.



Figure: After Pushing Local Commits to your Remote Repository


If you are working on a team and sharing the same repository with all team members, the git push command only works if nobody else has pushed code to the remote repository in the meantime.

If you are working as a team and someone else pushes code, then you try to push your own code, your push will be rejected with either a fetch first error or a non-fast-forward error. Whenever you see these errors, try git pull first to pull the remote changes, then try git push again. If git pull results in merge conflicts, you will have to first manually fix conflicts in your code and your teammate’s code.



Figure: Fetch-First Error


Git Bash Hints

Notice the hints provided by Git Bash. The hints tell you exaclty what you should do to try to resolve the problem!

Once again, the Git Bash output tells you exactly what to do: you will have to first git pull your teammate’s pushed code first before you are allowed to git push your own code.

Push Using EGit

If you Commit and Push as the last step in committing files to your local repository, then your code is automatically pushed to your remote repository at NCSU GitHub. If you simply commit without a push, then you must subsequently push the code at a later time follow these steps:

  1. Navigate to the Package Explorer view in Eclipse.
  2. Right-click the project containing the code and choose Team > Push to Upstream.

If you are working on a team and sharing the same repository with all team members, pushing only works if nobody else has pushed code to the remote repository in the meantime.

If you are working as a team and someone else pushes code, then you try to push your own code, your push will be rejected with either a fetch first error or a non-fast-forward error.



Figure: Non-Fast-Forward Error*


Reminder: EGit Push Feedback

Notice that (unlike Git Bash), EGit does not provide helpful hints about what you should do to resolve the problem! This is why we suggest that you use Git Bash whenever you get errors in EGit and don’t know how to proceed.

Whenever you see these errors, try pulling first to pull the remote changes, then try git push again. If git pull results in merge conflicts, you will have to first manually fix conflicts in your code and your teammate’s code.