Help Me Get Git!
git is a great way to manage projects with multiple contributors. You can sync, track, and even undo people’s changes. You may wish to use the helpful script that Dr. Durrant created to manage your
git repository. It can be found here. For those who want to use
git commands directly from the command line, here are some helpful tips:
Make the Directory for your Files
Make a directory for your local files. You must start off with a completely empty directory.
mkdir my_dir_for_files cd my_dir_for_files
Setting the Global Username and Email
Set the global user name and email. Use the same user name and email you use on git.durrantlab.com.
To set the user name:
git config --global user.name "MY_USER_NAME"
To set the email:
git config --global user.email "firstname.lastname@example.org"
Cache Your User Name and Password
If you don’t want to type in your user name and password every time you interact with the git server, use this command:
git config --global credential.helper "cache --timeout=3600"
You now only have to type in your user name and password every hour.
Copy the Remote Git Files to Your Computer
Copy or “clone” remote project files to the new local directory you created. You’ll need the remote URL for the project (see git.durrantlab.com).
git clone https://git.durrantlab.pitt.edu/jdurrant/SOME_NAME.git .
Notice the period at the end of the above command. It’s important!
Make your Own Private Branch of the Code
You’ve cloned the remote
master version (i.e., “branch”) of the software to your local directory. That branch is protected from any changes, so any changes you make to it on your local computer can’t be synced to the code stored remotely at git.durrantlab.com.
You need to create your own private branch of the code, separate from the
master branch. That way you’ll be able to sync changes you make to the remote git server.
git branch MY_BRANCH_NAME
In the previous section, you created a new branch named
MY_BRANCH_NAME, but you didn’t switch to that branch! That means any changes you make to your local code are still going to be made to the
master branch. You need to switch to the new branch.
git checkout MY_BRANCH_NAME
If you’re feeling fancy and want to create a branch and switch to it all in one command:
git checkout -b MY_BRANCH_NAME
If you’re not sure which branch of the code you’re currently using, you can check this way:
If that’s not your private branch, switch to the correct branch:
git checkout MY_BRANCH_NAME
Committing Your Local Changes
Once you’ve changed and saved some local files, you’re ready to “commit” them. Commiting files doesn’t upload them to git.durrantlab.com. It just marks them for future upload.
Why wouldn’t you want to upload all changes to the git repository automatically? Well, maybe you have some temporary files or notes to yourself that you just want to keep locally rather than sharing them with others through the remote repository.
So, first add all the files in your directory to the list of ones that should be synced.
git add .
(Again, notice the period!)
Now, commit all those files, marking them for future upload to the remote server.
git commit -m "A brief description of your changes here."
Be sure to include the brief description of what your changes accomplish so others can easily see what you’ve done.
If you’re feeling fancy, you can add files and commit them in one command:
git commit -a -m "A brief description of your changes here."
If you get an error, you might try this command before the others:
git config --global push.default simple
Syncing Your Changes to the Remote Server
Now that you’ve marked/committed your local files, you can sync your changes to the remote git server (git.durrantlab.com). In git-ese, this is called “pushing” your changes.
git push --set-upstream origin MY_BRANCH_NAME
This will add your changes to a copy of the code that is stored remotely (i.e., your local branch will be synced with the remote branch). Your local branch will NOT sync with the remote
master branch, which you don’t have permission to change, but rather to a special remote branch made just for you.
Syncing Changes from the Remote Server
It’s also possible that the remote version of the code on the server has some useful changes that need to be incorporated into the local copy of the code on your computer. Perhaps these changes were made by other people, or by you on another computer.
Updating the local code with changes stored remotely is called “pulling” remote changes.
Sometimes the version of the code on the remote server and the version you have locally can’t be automatically merged. In this case,
git will mark your code so you can fix the conflict. You’ll get a message that looks like this:
Auto-merging git/README.md CONFLICT (content): Merge conflict in PATH/TO/PROBLEM/FILE Automatic merge failed; fix conflicts and then commit the result.
If you edit the problem file, you’ll see something like this:
<<<<<<< HEAD console.log("Code from one version of the file."); ======= console.log("Code from another version of the file."); >>>>>>> e565fc2690742277fb543c8ace5cb8488cec2082
Remove the version you don’t want to keep and save your file. Now you should be able to sync the local and remote files without a problem.
Merging Changes Made to the Master Branch into Your Branch
Sometimes you might want to merge changes from the master branch into your local version (branch) of the code. Perhaps someone else made useful changes to the master code that you want to incorporate into your local version.
git checkout MY_BRANCH_NAME git merge origin/master
Merging Your Changes into the Master Branch
Once you’re feeling good about your code, come talk to me. We’ll review the changes you made together. If they look good, I’ll merge them into the