Git Docs1
Git Docs1
download :
git-sct.com/download
update git if already installed
git update-git-for-windows
check version
git version
If you already have a GitHub account, verify that you can sign
in.
If you’re not sure whether you’ve already configured Git, you can list
your configuration by executing git config --list.
get cofiguration
git config --global --list
===================================================================================
2) An introduction version control : GIT
What is Version Control?
A version control system maintains a record of changes to code and
other content. It also allows us to revert changes to a previous point in time.
Types of Version control
There are many forms of version control. Some not as good:
Save a document with a new date (we’ve all done it, but it isn’t
efficient)
Google Docs “history” function (not bad for some documents, but limited
in scope).
Some better:
Git
Mercurial
Subversion
This means that you don’t have to worry about a collaborator (or your
future self) overwriting something important. It also allows two people working on
the same document to efficiently combine ideas and changes.
open : fig_1.png
open : fig_2.png
open : fig_3.png
GitHub also allows you to share your work and collaborate with
others on projects.
open : fig_4.png
===================================================================================
3) First steps with git: clone, add, commit, push
Create a new repository on GitHub
1. To begin, sign in to your user account on GitHub.
2. In the upper right corner, click the + sign icon, then choose New
repository. This will take you to a page where you can enter a repository name
(this tutorial uses test-repo as the repository name), description, and choose to
initialize with a README (a good idea!).
3. It is a good idea to add a .gitignore file by selecting one of the
languages from the drop down menu, though for this tutorial it will not be
necessary.
4. Similarly, in practice you should choose a license to that people
know whether and how they can use your code.
5. Once you have entered a repository name and made your selection,
select Create repository, and you will be taken to your new repository web page.
Git commands: These are commands that are specific to git and
will only be available if you have git installed on your computer. Git specific
commands will always started with a call to git (e.g. git status, git clone, etc)
Next, on your local machine, open your bash shell and change your
current working directory to the location where you would like to clone your
repository. Note that here we are using a bash command - cd (change directory).
cd Documents
Once you have navigated to the directory where you want to put your
repository, you can use:
The git clone command copies your repository from GitHub to your local
computer. Note that this is a git specific command.
When you run git clone repo-path-here, You should see output like:
Note: The repository name and output numbers that you see on your
computer, representing the total file size, etc, may differ from the example
provided above.
cd my-repo-name
cd folder_name
If you list all the files in this directory (using ls -a), you should
see all of the files that exist in your GitHub repository:
ls -a // dir
Simply open your file browser and navigate to the new local repo.
Important Tip The .git element is listed when you use the ls -a
command shows up is actually a directory which will keep track of your changes (the
commits that you make) in git. Warning: Do not edit the files in this directory
manually!
Using either method, we can see that the file structure of our cloned
repo mirrors the file structure of our forked GitHub repo.
Edit a file in your repo
Next, open up your favorite text editor and make a few edits to the
README.md file. Save your changes.
Once you are happy with your changes and have saved them, go back to
your terminal window and type git status and hit return to execute the command.
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be
committed)
(use "git checkout -- <file>..." to discard changes in
working directory)
modified: README.md
The output from git status indicates that you have modified the file
README.md. To keep track of this change to this file, you need to
git add: takes a modified file in your working directory and places the
modified version in a staging area.
git commit takes everything from the staging area and makes a permanent
snapshot of the current state of your repository that is associated with a unique
identifier.
These two commands make up the bulk of many workflows that use git for
version control.
open : fig_5.png
Add files
You can add an individual file or groups of files to git tracking. To
add a single file, use
To add the README.md file that you just modified, you’d use:
To add ALL of the files that you have edited at the same time, you can
use:
git add .
git add --all
Use git add --all with caution. You do not want to accidentally add
things like credential files, .DS_Store files, or history files.
Commit files
Once you are ready to make a snapshot of the current state of your
repository, you can use git commit. The git commit command requires a commit
message that describes the snapshot / changes that you made in that commit.
A commit message should outline what changed and why. These messages
help collaborators and your future self understand what was changed and
why
allow you and your collaborators to find (and undo if necessary)
changes that were previously made.
If you are not committing a lot of changes, you can create a short one
line commit message using the -m flag:
If you have configured git to use your favorite text editor (via git
config --global core.editor your-fav-editor-here), then you can open that editor to
write your commit message using the git commit command:
git commit
Once you save your commit message and exit the text editor, the file
that you created will contain your commit message.
git push
You will then be prompted for your GitHub user name and password. After
you’ve pushed your commits, visit your repository on GitHub and notice that your
changes are reflected there, and also that you have access to the full commit
history for your repository!
===================================================================================
4) Introduction to undoing things in git
How to undo changes:
- before they’ve been staged (you haven’t used git add yet to add or
stage them),
- after they’ve been staged with git add, and
- after they’ve been committed to git.
Undoing unstaged changes
If a file has been changed, but these changes have not yet been staged
with git add, then the changes can be undone using git checkout. The instructions
for using git checkout to undo changes are described in the output of git status.
Let’s look at an example. First, let’s modify the readme file by adding
some text to it at the command line.
Next, type git status to see how that change impacted git
$ git status
In the example above, git status was run in the command line after a
file was edited. When you run git status, git will first provide the following
output:
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working
directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
The output from git status tells you that you can use git checkout --
<file> to discard changes to that file in your repo. So, if you don’t like the
changes made to the README.md file, you can revert back to the last committed
version using:
git status
Which returns:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Now, the contents of your README.md file has been reverted to the last
saved or committed version and you’ve discarded the most recent changes.
open : fig_6.png
Fortunately, the output of git status gives us a hint for how to undo
our staged changes:
# modify the README file
echo 'Some more changes' >> README.md
modified: README.md
You use git reset HEAD <file> to unstage our changes. HEAD refers to
the most recently committed version of the file:
Data tip: HEAD refers to the most recent version of your file. You can
also revert to an older version using HEAD~1, HEAD~2 etc.
When you use git reset, your changes still exist in the file, but the
file has been unstaged (the changes are not added to git, yet).
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working
directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Now that you have changes that are not staged, you can use git checkout
to undo those modifications. Git reset is essentially the opposite of the command
git add. It undoes the add.
** Undoing a commit **
If you have modified, added and committed changes to a file, and want
to undo those changes, then you can again use git reset HEAD~ to undo your commit.
Similar to the previous example, when you use git reset the modifications will be
unstaged.
# add it
git add --all
# commit
git commit -m 'Accidentally including my social security number
in my file'
git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
Now you can undo this commit with git reset HEAD~:
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
social-security.txt
If you inspect the output of git log, you will notice that your
previous commit is no longer part of the repository’s history.
Now open the that file in a text editor and add the following lines
below:
Any files listed in this file will be ignored by git. You can also tell
git to ignore entire directories.
===================================================================================
=
5) How to find and navigate a repo on the GitHub website
Navigate GitHub
Repositories (AKA repos)
According to the GitHub glossary:
Notice the format of the repository name. Repository names will always
begin with the account or organization name followed by the repo name, like this:
organization-or-account-name/repo-name
Next, below the repo full name, explore the header tabs
Notice the following headers that you will use in this workshop:
===================================================================================
6) How to fork a repo in GitHub
What is a fork?
A GitHub fork is a copy of a repository (repo) that sits in your
account rather than the account from which you forked the data from. Once you have
forked a repo, you own your forked copy. This means that you can edit the contents
of your forked repository without impacting the parent repo.
When you fork a repo, you make an exact copy of the repo in your own account.
Once you create a copy in your account you own it! Thus, you you can freely modify
it as you wish. Image source: Colin Williams, NEON
This workflow has a central repository - which is the one that SLK-SDET owns.
Everyone in the workshop will then contribute to the central repository. There are
other Git and GitHub workflows too. However in this workshop, we are demonstrating
a central repo workflow.
open : fig_8.png
open : fig_9.png
YOUR-USER-NAME/SLK-SDET.
https://github.com/username/SLK-SDET
https://github.com/YOUR-USER-NAME/SLK-SDET
A good way to figure out which repo you are viewing is to look at the
The fork will remain in sync with the central repo until:
In this lesson, you’ll learn how to submit a pull request to suggest that
your edits are included in another (the central Earth Lab) repo.
open : fig_10.png
Pull requests are the heart of collaboration on GitHub. When you open a
pull request, you’re proposing your changes and requesting that someone review and
pull in your contribution and merge them into their project.