Getting started with Git - YKul/Tutorials GitHub Wiki
- Learn how git works
- Install git
- Configure git
- Create a local repository
- Add a file to the staging area
- Commit the staging area
- Change some files and track differences
- Pushing to a remote repository
- Add .gitkeep to push nested directory structures
- Commit and push version with embedded directories
- Get motivated
Just a foreword for clarity. When we use the term "locally", we are referring to something that is on your computer, such as files and folders , or programs you have installed.
For now, you won't need to understand collaborative tools, but watching up to 2:45 of How Git Works: Explained in 4 Minutes will introduce enough ideas and terms to get us started. I recommend pausing the video at 2:45 so you can refer back to the diagram with commands to help you understand exactly what we're doing at each step going forward.
I will refer you to the installation instructions by The Carpentries for your respective systems. Note that Windows users will have already installed git when they set up their bash terminal.
We will be setting global configurations as opposed to repository-specific configurations. For simplicity, you can use the e-mail address you registered your GitHub account with.
Please refer to these instructions to set up your git username using the command line
Please refer to these instructions to set up your e-mail in git
Popular convention used to be to name the default branch 'master'. GitHub has transitioned to the convention of naming the default branch 'main'. Git took a while to update for this convention change, so your version may not reflect this.
Enter git config --global init.defaultBranch main
to make sure your default branch is named 'main'.
If you are ever working with a repository that has a 'master' branch, there is no difference between the 'master' and 'main' branch except the name.
-
In your command line terminal, navigate to your Winter_2024 directory we made previously
-
With Winter_2024 as your working directory, enter the command
git init
-
Enter
ls -a
. Recall that the-a
flag allows us to see "all" files. Notice that a new folder named ".git" has been created. You may recall that files/folders with.
at the beginning of the name are 'hidden', so this folder will not show in your usual file manager (unless you have enabled it), or with thels
command without the flag. This folder contains all the information for your project, including its history, so deleting this folder will delete your project's history. However, if you must, you can delete it with therm
command using the-r
flag. Just be careful. -
Enter
git status
to verify that you are on branch 'main'.
- First let's create an empty file "acquired_skills.txt". We will use the
touch
command, which accesses a filepath to update timestamp data. If the filepath does not exist then an file will be created.
Create the file: touch acquired_skills.txt
-
Now if we use
git status
we see that our new file in listed under "Untracked files" -
Use
git add acquired_skill.txt
to add the new file to the staging area, waiting to be committed. Usegit status
and notice the file is now waiting to be committed.
Git generally requires you to enter a short message describing your commits. This is good practice for repository management, so make the messages short, clear, and descriptive of what the changes will do. If you need to roll-back a project to a previous version, you'll be thankful for helpful descriptions of each version. Likewise, if you're collaborating with others, commit messages allow you to claim a history of your contributions and communicate to others what has been modified. Lastly, this makes you think more carefully about what you are committing so that the repository history doesn't get needlessly cluttered.
So let's commit our staging area.
git commit -m "Adding empty acquired_skills.txt file"
The -m
flag tells the command that we are entering a message. If you do not use this flag, git will open a text editor and make you write a message, or it will not perform the commit.
If we use git status
now, we see there is no longer anything to commit.
A history of commits can be viewed with git log
Let's add some text to acquired_skills.txt. To do this we will use the command echo
, which pushes a message through the output stream. By default, the target for the output stream is our terminal, so whatever we type will be repeated back to us as output.
For example:
To change the target we use the >
command, followed by a new target for the output stream.
For example
echo - I made a GitHub account and started my first Wiki documentation > acquired_skills.txt
Let's add another line. Instead of directing the output stream to the target file with >
, we will tell it to append the output stream to the end of the target using >>
.
For example
echo - I learned some basic bash and command line skills >> acquired_skills.txt
git status
will now indicate that our file has been modified. git diff
will tell you what changes were made and where they were made. The output is cryptic, and won't be of great importance, so I will leave it up to you to explore how this command works in more detail.
Finally, add the modified file to the staging area and commit the staging area with a descriptive message as we did previous.
- First, let's create a new remote repository on GitHub named "SchoolWork", the same as we did before. However, this project might have sensitive information like assignment work that people might plagiarize, so we might want to select "private", instead of "public".
If you are inclined, there is an official command line tool called GitHub CLI that provides integration between GitHub and your terminal.
-
Create a personal access token (classic). This will be used in place of a password to log you in from the command line, so copy/paste you token to a text document.
-
Add your repository's HTTPS address to origin
-
You can find the HTTPS address of your repository by clicking on the green "<> Code" button. It should look something like
https://github.com/yourusername/yourrepositoryname.git
-
Enter the command
git remote add origin [YOUR_HTTPS_ADDRESS]
-
Enter the command
git push -u origin main
-
You will be asked for your GitHub username and password. Your username is the same as it is on GitHub, and your password is the personal access token you created in the previous step.
-
Reload your repository page in your web browser. You should see your acquired_skills.txt document!
You've probably noticed that while acquired_skills.txt was transferred to your repository, your empty folders weren't. This is because git only stores files, not empty directories. If you want to transfer a directory structure, we need to put a hidden file in each folder.
OPTIONAL
I will provide the answer below, but if you have some time it might be a rewarding exercise to search Google for a single-line command that will put a file in a folder and all its sub-folders. Try using the term "UNIX" to make sure you get command-line specific results. StackExchange or StackOverflow are where you will usually find the most useful answers to such questions. Just be aware that some commands can damage your computer, so don't run anything unless you have an idea of how they work (even if you have to search elsewhere to piece it together). Perhaps navigate to your sandbox to make a test directory you can delete later, or if you need to make a back-up of Winter_2024, navigate to your sandbox directory and enter cp -R Winter_2024 Winter_2024.backup
. cp
means "Copy", the -R
flag means "recursively" (recursively go into each subdirectory and copy), followed by the file you want to copy, and then the name of the new copy.
Searching for answers on Google is a great way of getting exposure to new materials you would have never looked up otherwise. It's also a practical skill that we all rely on. Finding solutions can feel rewarding, and perhaps you will find it challenging but satisfying to solve and understand your first command line automation solution by yourself.
I will also provide the explanation of how the answer works separately, so you can make sure you found the correct answer first, and still try figure out how it works by yourself.
(Click to show answer) Find a single-line command that will create the file .gitkeep in all subdirectories
With Winter_2024 as your working directory, type the command `find . -type d -exec touch {}/.gitkeep \;`(Click to show answer) Explanation
There is a pretty good explanation of the parts of the command [here](https://askubuntu.com/questions/339015/what-does-mean-in-the-find-command).Essentially we are going to `find` in the current directory `.` elements of `-type` directory `d`, and execute `-exec` the command `touch` using the results of `find`, that is the names/paths of all the nested directories, `{}` (remember that we [previously](https://github.com/YKul/Tutorials/wiki/Getting-started-with-command-line-and-bash#5-make-something-cool) used the curly-brackets to indicate an explicit list of names or number ranges in a directory path. Now it is a list of search results from `find`) and we are going to extend that pathname to include a file within that directory `/.gitkeep`.
We end the `-exec` command with a semi-colon, but because the semi-colon may be interpreted as a special character with its own meaning, we use a backslash `\` to indicate that it is part of the command.
If that last part doesn't make complete sense... Recall that we shouldn't use spaces or special characters in file names because spaces often separate the different parts of a command, and special characters can have special meanings or mathematical functions. When these symbols must be used, we distinguish them with the backslash, or sometimes by putting them in quotes. This tells the shell to treat them as literal plain-characters.
- You can use
git add .
to add the entire current working directory to the stage, instead of specifying all the folders/files you just changed - After you push and reload your repository, you should have folders for all your courses, with all the same Assignments and Notes folders
This all seems pretty useful, but there's a lot of different commands you have to enter to make it work. With bash, you can create custom commands, or you can map long commands to shorter "aliases". Different systems are organized a bit differently, but there's usually a profile or configuration file for bash. By adding a few lines of text you can combine these multi-step processes into a single command, or shorten them so they're easier to remember. For example, alias newnotes="mkdir -p Winter_2024/{BIOL1000,CHEM1000,ENG1000}/Notes/$(date +%Y_%m_%d)"
will map the long command which creates a date-stamped directory in your notes folders to the much easier to remember command "newnotes". You can try this in your terminal, but unless you add it to your profile or configuration file, it will reset when you close your terminal session. But these are hidden files, and hidden files are usually hidden from the standard user for a reason.
If you take the time to learn bash script you can start getting into some real programming. Even though we've covered command-line and git pretty quickly, you have starting foundations for some pretty powerful and versatile stuff.
The Carpentries has a longer tutorial on git that covers a few extra details. You can also find recordings of their workshops for bash, git, and python/R on YouTube (they're usually taught together)