-
Wigal, Jacob (CIV) authoredWigal, Jacob (CIV) authored
This tutorial is for the following functions:
- creating and switching branches
- pulling the latest files
- making a commit
- pushing changed files and/or new files from your computer to GitLab
Startup git
Replace /filepath/filename with the filepath of your git repository.
cd /example/filepath/get-started
What are branches?
Now, if you type git status
you should see, once again, that you are “On branch master”. What does that mean? First it is important to understand how git tracks versions. Each time someone changes a file and makes a “commit” (which we will get to in just a minute), git creates a snapshot of that file. This is great for keeping a record of file versions. But sometimes you may want to test something out (make changes to multiple files and maybe delete them later), or track changes you are making to your files without changing any of the files someone else is working from. These types of situations are why we create branches. In most repositories, the master branch is rarely changed. On this repository, the master branch is a “protected” branch, meaning almost no one can make changes to it, even if they wanted to. If you want to make changes to the repository, you will just have to create a new branch, and/or push to a branch that is not the master. The below image shows a visualization of a repository’s commit history. In the center is each commit’s unique identifier or “hash” generated by git. A branch points to a specific moment in this timeline. In this case, someone created a new branch called “testing” which at this point is an exact copy of the master. The “HEAD” box in this image signifies that the user is currently working from the testing branch and not the master.
Borrowing from git’s documentation, “A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made.” But you can create as many branches as you want. You can read more about branches here.
Before we create a branch of our own we need to do one more thing..
Pull
This command gives us the most up to date version of a branch from GitLab. Always pull before working on files to ensure you have the most up-to-date version of your branch. (syntax: git pull origin branch_name)
git pull origin master
Create a new branch
Let’s make a new branch called “develop”. (syntax: git branch new_branch_name)
git branch develop
Great! We just created a new branch now let’s check what branch we are on…
git branch
Switch branches
Notice how we are still on master. Use this command to switch branches. (syntax: git checkout branch_name)
git checkout develop
(In the future you can use this command to create a new branch and
switch to it at the same time! git checkout -b new_branch_name
)
There was already a “develop” branch on GitLab when we created the develop branch on our machine, so we need to pull one more time to update our personal develop branch. (It is also important to consider that creating a branch on your machine does not automatically create one on GitLab. Branches new to GitLab need to be “pushed” before they show up on GitLab. We will talk more about pushing at the end of this tutorial.)
git pull origin develop
Make a Commit
Use this to finalize a file edit or addition and provide a reason.
Step 1
Make a change to your file, or place a new file to be pushed somewhere in your local git folder structure. For this tutorial, open up end_of_git_tutorial.txt in the tutorial_files folder inside your get-started repostory and add your first and last name to the first empty line you see.
Step 2
Choose file(s) to commit. Replace the filepath at the end of the cell below with the filepath of the file(s) you would like to commit. (Note: If you would like to commit multiple files at once with the same commit message, you can do so by adding the parent folder of all edited files.) For this example we can add the filepath of our tutorial_files folder or the filepath of our end_of_git_tutorial.txt file.
git add Users/username/Desktop/get-started/git/end_of_git_tutorial.txt
Step 3
Place your reason for the commit inside quotes. If this is a new file to the repository, simply put “Initial commit”. For this example, write anything you think will communicate to others why you are changing this file, or what you are changing about it.
git commit -m "Adding my name..."
Push
Use this to update files in the remote repository with your committed files. Only try to push to branches with the same name as the one on your machine. (e.g. if you want to push a branch named “my_cool_branch”, you should type “git push origin my_cool_branch”). There is already a “develop” branch on GitLab, so by pushing we will update the develop branch on GitLab, replacing it with an exact copy of the develop branch on our machine!
(syntax: git push origin branch_name)
git push origin develop
That’s it!
You should now see the updated end_of_tutorial.txt file with your name on GitLab on the develop branch!
The process we just followed is visualized in this cheat sheet. You can follow this each time you work on a file.