or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
---
Introduction to Git –- Fall 2020
Lecture 3: Basic commands
Slides: https://hackmd.io/@hpc2n-git-2020/L3-commands
Getting help
Creating a repository
In case you want to start a project from scratch called myproject:
this will create a folder called .git in the current folder which contains the Git-related files.
We can now ask about the status of the repository:
Cloning a repository
One can clone an existing repository with the command:
repository_location is the path of the Git repository (if it is in your local machine) or a URL if it is on the internet. path_where_it_will_be is the path where you want to have the cloned repository.
Three stages of files
Adding files' modifications
After initializing the repository, we decide to create a file called first.txt
If we ask about the status of the repository we will see the following message,
We can now add the first.txt file to the staging area:
and then check the status of the repository:
Unstaging files' modifications part I
If you want to unstage the changes (maybe you are not convinced of them) type the line suggested by Git:
Notice that Git suggests this command because the repository is brand-new and nothing has been committed yet.
Committing changes
Once the changes are staged, they can be commited with the command
this will open a window of the default text editor in your system (in this case Vim)
write a commit message and upon saving the file the changes will be committed.
the status after committing is
Fast (lazy?) commit quick option:
this command will add all files that were modified (and tracked) and commit them with the quoted message.
Unstaging files' modifications part II
Imagine that after doing the first commit for first.txt file, you modify this file and stage it (git add first.txt), the status now looks like:
Git suggests a different way to unstage the file as we saw before, because now there is a HEAD pointer.
In the recent versions of Git you can also use the following command to unstage:
Adding multiple files
In case you want to add multiple files, that follow a pattern, at the same time you can use Linux-type wild cards. As an example, we can add the files file1.txt, file2.txt, file3.txt at once with the commands (equivalent for this test case):
If we want to add all the files that can be staged we can do:
Adding files' modifications interactively
Suppose we add a couple of lines to our file first.txt and now it looks like
the status command tells us that we did some modifications to the file:
We know that the new text lines refer to different topics and because of that it would be better to have them in different commits. We can add the modifications interactively:
p option will create a patch. Need to define patch
select the file 1:
At this point you would be able to edit the patch with option e this will display a message in your text editor, I will delete the last line * Summary:
and save the file.
Then, you can take a look at the status of the file
Notice that the file first.txt has been partially staged.
The difference between staged changes and the unstaged ones can be seen with:
the difference between staged changes and the previous commit with:
and the difference between the unstaged changes and the previous commit with:
At this point, you can commit the staged changes and later on stage and commit the remaining changes.
Renaming files/folders
Imagine that you want to change the name of the file first.txt to Readme.txt, in this case you can use the command:
Although there is not feedback from the command the status of the file has now changed
you can now commit the changes. Renaming files, instead of creating new ones, can help you to keep a consistent history of the files.
Moving files
Similar to the mv Linux command, the git mv command can be also used to move a file to a different location:
Removing files
If some file is not useful any longer and we want to delete it from our repository, we can issue the command,
Sometimes it is more convenient to rename files instead of deleting them to keep the history more consistent.
Ignoring files
It could be that you want to ignore some files in your repository, for instance, temporary (.tmp) or binary (.bin) files. One way to accomplish this is by creating a file called .gitignore in the repository with some rules with a Linux-type wild cards syntax
then, you will have to commit the .gitignore file as usual.
One caveat of using a .gitignore file is that it will be shared with all the collaborators. One can instead use the local exclude file in .git/info/exclude to define the rules. This file won't be shared.
Generating aliases
The following command shows a graph of the commits' tree in an organized way:
This command is too long to type/remember. Git allows you to create shortcuts/aliases for commands:
In this way, you can use a customized git graph command:
A second way to generate an alias is by adding it to your .bashrc file:
which will make the command graph available on the command line.
Simplifying commits
Suppose that you have a series of commits which are close-related in your local repo:
in this case, the three last commits refer to the ingredients of a guacamole and could be summarized into one,
choose the option s (squash)
a text editor message will appear where you can type the message for the squashed commit:
Finally, we check the log file and see that we have now only two commits with the new commit message:
Note: use this command in commits that haven't been pushed on public branches. This command modifies the history.