guides:software:git
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
guides:software:git [2020/07/19 23:20] – fixed some list formatting jhoggard | guides:software:git [2024/07/25 15:01] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 7: | Line 7: | ||
===== Overview: What is git? ===== | ===== Overview: What is git? ===== | ||
- | Version | + | Git is a version |
- | Repositories | + | When working with git, our local files can be in three states: |
+ | * In the working directory, with changes not yet tracked by git. | ||
+ | * In the //staging area//. This is a temporary holding area where the current files are copied, in preparation for adding them to the repository. | ||
+ | * Recorded as a snapshot in the repository. Files in the staging area are moved into a //commit// in the repository, which is a permanent snapshot of the state of all files at that point in time. If we make further changes to any files, these will be untracked changes until will stage and commit them. | ||
- | Local repository: files, staging area, repository | + | (Note that there may also be a // |
- | init, add, status, commit | + | To use git to track changes to a project, we do the following: |
+ | - Initialize a git repository in a directory (or //clone// a repository from a server) | ||
+ | - Create files or make changes to our files in the directory. These files are now considered //untracked changes// by git. | ||
+ | - After we have made changes to a file, we copy the file to git's //staging area//. This is a temporary location before git permanently makes a copy of our changes. | ||
+ | - When we are ready, we //commit// the changes in our files. This moves all files in the staging area into the git archive. This creates a permanent snapshot of what the project looked like at that moment. | ||
+ | - (Optional) If we have a connection to an archive on a remote repository (on a service like GitLab or GitHub), we can //push// those changes to remote repository. This stage is needed to share the project with others, if we wish. | ||
+ | |||
+ | As a simple example, we will look at the process of setting up a git repository for a simple " | ||
+ | |||
+ | * First, let's make a directory to store the program in. We'll create a directory '' | ||
+ | < | ||
+ | $ mkdir HelloTest | ||
+ | $ cd HelloTest | ||
+ | $ ls | ||
+ | $ | ||
+ | </ | ||
+ | * As we can see, the directory is initially empty. We will set up this directory as a git repository, with the command '' | ||
+ | < | ||
+ | $ git init | ||
+ | Initialized empty Git repository in ~/ | ||
+ | </ | ||
+ | * This creates an invisible subdirectory called '' | ||
+ | < | ||
+ | $ ls -a | ||
+ | . .. .git | ||
+ | </ | ||
+ | * Now let's put a file into the directory. Using an editor, we create a file '' | ||
+ | < | ||
+ | #include < | ||
+ | |||
+ | int main() { | ||
+ | |||
+ | std::cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | * We can compile this as usual to create an executable program we'll call '' | ||
+ | < | ||
+ | $ ls | ||
+ | hello hello.cpp | ||
+ | </ | ||
+ | * We can run the command '' | ||
+ | < | ||
+ | $ git status | ||
+ | # On branch master | ||
+ | # | ||
+ | # Initial commit | ||
+ | # | ||
+ | # Untracked files: | ||
+ | # (use " | ||
+ | # | ||
+ | # hello | ||
+ | # | ||
+ | nothing added to commit but untracked files present (use "git add" to track) | ||
+ | </ | ||
+ | * Let's add the source code file '' | ||
+ | < | ||
+ | $ git add hello.cpp | ||
+ | [19:13] jhoggard@cslab100: | ||
+ | # On branch master | ||
+ | # | ||
+ | # Initial commit | ||
+ | # | ||
+ | # Changes to be committed: | ||
+ | # (use "git rm --cached < | ||
+ | # | ||
+ | # new file: | ||
+ | # | ||
+ | # Untracked files: | ||
+ | # (use "git add < | ||
+ | # | ||
+ | # hello | ||
+ | </ | ||
+ | * We see in the above that we have one file waiting to be committed ('' | ||
+ | < | ||
+ | $ git commit -m " | ||
+ | [master (root-commit) bbf762b] Initial commit: Hello world program. | ||
+ | 1 files changed, 8 insertions(+), | ||
+ | | ||
+ | </ | ||
+ | * We used the commit message " | ||
==== Server: GitLab, GitHub, etc ==== | ==== Server: GitLab, GitHub, etc ==== | ||
- | Create and clone | + | We can also work with a //remote// repository, which holds a copy of all our work on a server. |
+ | |||
+ | The department maintains its own GitLab server at codestore.cs.edinboro.edu. | ||
+ | |||
+ | === Create and clone === | ||
+ | |||
+ | If you want to set up a remote repository on codestore, you can click the " | ||
+ | |||
+ | You can select " | ||
+ | |||
+ | Afterwards, you will see instructions for cloning your repository to your local machine, but it is not hard: On your project' | ||
+ | |||
+ | Then on your machine (possibly cslab103), type the following in the directory where you want to clone the repository: | ||
+ | < | ||
+ | git clone < | ||
+ | </ | ||
+ | where ''< | ||
+ | |||
+ | Now you have a local copy of the repository, and by default, it is connected to the remote repository. | ||
+ | |||
+ | However, '' | ||
+ | |||
+ | === Push and Pull === | ||
+ | |||
+ | To send your commits up to '' | ||
+ | < | ||
+ | git push origin master | ||
+ | </ | ||
+ | (The above sends your //master// branch commits to '' | ||
+ | |||
+ | To update your local repository with any changes added to the remote repository, you need to //pull// the files. | ||
+ | < | ||
+ | git pull | ||
+ | </ | ||
+ | This will fetch any commits made on your current branch (probably master) and merge them into your current directory. | ||
+ | |||
+ | You can also separate this into two steps. The first retrieves any new snapshots on the remote repository, and the second merges the changes into your current files. | ||
+ | < | ||
+ | git fetch | ||
+ | git merge | ||
+ | </ | ||
+ | |||
+ | ==== Fork Another Repository ==== | ||
+ | |||
+ | You may sometimes want to copy someone else's repository on codestore into your very own version of the repository. | ||
+ | |||
+ | In GitLab (for example, at codestore), you can click on the " | ||
- | Push and pull | + | Then you can clone //your// fork of the project to your own computer, |
- | Fork another repository | ||
- | === Setting | + | ==== Setting |
+ | * Name and email | ||
+ | * Editor for commits | ||
===== Common Commands ===== | ===== Common Commands ===== | ||
The following are common tasks to complete using git. Where we refer to GitLab, the same task can be completed with other git servers, such as GitHub, usually with very little or no change. | The following are common tasks to complete using git. Where we refer to GitLab, the same task can be completed with other git servers, such as GitHub, usually with very little or no change. | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
- | * '' | + | * '' |
guides/software/git.1595200815.txt.gz · Last modified: 2024/07/25 15:01 (external edit)