Nota:: Usar o Q Command To Quit
Nota:: Usar o Q Command To Quit
git init
git init
The word init means initialize. The command sets up all the tools Git
needs to begin tracking changes made to the project.
Git Workflow
git status
git add
The word filename here refers to the name of the file you are editing,
such as scene-1.txt.
Nota:
git add .
Adiciona todos os ficheiros do working directory.
git diff
Imagine that we type another line in scene-1.txt. Since the file is
tracked, we can check the differences between the working directory
and the staging area with:
git commit
git commitis the command we’ll do next. However, one more bit of
code is needed for a commit: the option -m followed by a message.
Here’s an example:
git log
-------------------------------------//----------------------------------------
Backtracking Intro
head commit
In Git, the commit you are currently on is known as the HEAD commit.
In many cases, the most recently made commit is the HEAD commit.
The output of this command will display everything the git log
command displays for the HEAD commit, plus all the file changes
that were committed.
git checkout
What if you decide to change the ghost’s line in the working directory,
but then decide you wanted to discard that change?
You could rewrite the line how it was originally, but what if you forgot
the exact wording? The command
will restore the file in your working directory to look exactly as it did
when you last made a commit.
git reset I
What if, before you commit, you accidentally delete an important line
from scene-2.txt? Unthinkingly, you add scene-2.txt to the staging
area. The file change is unrelated to the Larry/Laertes swap and you
don’t want to include it in the commit.
It does not discard file changes from the working directory, it just
removes them from the staging area.
git reset II
Just like retracing your steps on that hike, Git enables you to rewind
to the part before you made the wrong turn. You can do this with:
Before reset:
After resetting:
-------------------------------------//----------------------------------------
GitHub
GitHub is both a website and a service that facilitates software
development by allowing you to store your code in containers,
called repositories, and by tracking changes made to your
code.
In addition, it offers a hosting service and tools to build, test, and
deploy code.
GitHub uses Git, a version-control development tool, to manage your
projects by tracking changes to files and allowing multiple people
to work on the same project.
1-2. Open the Start menu and search for the app, git bash.
3. Navigate to GitHub’s articles on setting up your Git
username and email and follow the instructions for each using Git Bash.
Instructions
1. In your Command Line Interface, make sure your current working
directory is your new Git repository. Navigate there if not.
2. Check the status of which files and folders are new or have been
edited. There should be no files modified.
$ git status
3. On GitHub, create a new repository by clicking the New
repository button on the home page.
4. On the new repository page, give your repository a name. It’s not
necessary, but it would be convenient to name it the same as the
directory, git_practice. After naming the repository, click Create
repository.
5. After creating a repository, GitHub displays the repository page. At
the top of the page, make sure “HTTPS” is selected.
There you have it! Your first GitHub repository, linked to your local Git
repository. You’ve taken some huge leaps, so be proud! Now you can
use your knowledge of Git to track progress on your local computer
and push that progress to GitHub whenever you want.
Managing Branches
With Git, each teammate can create their own branch off of the main
project in order to work on bug-fixes, new features, experimental
code, etc.
Each repository can have one or more branches. The main branch —
the one where all changes eventually get merged back into, is
called main. The main branch is usually the working version of a
project and contains the production code, so it’s very
important to only merge clean and correct code into it!
-------------------------------------//----------------------------------------
GitHub and Markdown
With a little bit of Markdown magic, you can achieve text that includes headings,
hyperlinks, emojis, checkmarks,
You’ll learn the basics of Markdown and how to use it to write a good README for
your project!
-------------------------------------//----------------------------------------
Make your project stand out look professional by at least including the
following elements in your README:
It can help your future self as well when you step away for a while and
need to get reacquainted with your project. Not to mention it’ll leave
a positive impression on future interviewers who look at your GitHub
profile.
-------------------------------------//----------------------------------------
GIT BRANCHING
git branch
Note: GitHub has changed the naming convention of the main branch
from master to main.
You can use the command below to answer the question: “which
branch am I on?”
git branch
branching overview
The circles are commits, and together form the Git project’s
commit history.
New Branch is a different version of the Git project. It
contains commits from the main branch but also has
commits that it does not have.
git branch 2
Right now, the Git project has only one branch: master.
git checkout
The master and fencing branches are identical: they share the same
exact commit history. You can switch to the new branch with
Once you switch branches, you will now be able to make commits
on the branch that have no impact on master.
Note: if you find that your cursor is stuck in Git log, press q to
escape.
git merge
merge conflict I
The merge was successful because master had not changed since we
made a commit on fencing. Git knew to simply update master with
changes on fencing.
merge conflict II
You’ve made commits on separate branches that alter the same line
in conflicting ways. Now, when you try to merge fencing into master, Git
will not know which version of the file to keep.
<<<<<<< HEAD
master version of line
=======
fencing version of line
>>>>>>> fencing
EXERCICIO:
delete branch
The command
Now that master contains all the file changes that were in fencing, let’s
delete fencing.
-------------------------------------//----------------------------------------
GIT TEAMWORK
git clone
Sally has created the remote repository, science-quizzes in the directory curriculum,
which teachers on the school’s shared network have access to. In order to get your
own replica of science-quizzes, you’ll need to clone it with:
In this command:
remote_location tells Git where to go to find the remote. This could be a web
address, or a filepath, such as:
/Users/teachers/Documents/some-remote
clone_name is the name you give to the directory in which Git will clone the
repository.
EXERCICIO:
OU
git remote –v
One thing that Git does behind the scenes when you clone science-
quizzes is give the remote address the name origin, so that you can
refer to it more conveniently. In this case, Sally’s remote is origin.
You can see a list of a Git project’s remotes with the command:
git remote -v
Notice the output:
origin /home/ccuser/workspace/curriculum/science-
quizzes (fetch)
origin /home/ccuser/workspace/curriculum/science-
quizzes (push)
Git lists the name of the remote, origin, as well as its location.
Git automatically names this remote origin, because it refers
to the remote repository of origin. However, it is possible to
safely change its name.
The remote is listed twice: once for (fetch) and once
for (push). We’ll learn about these later in the lesson.
git fetch
git fetch
This command will not merge changes from the remote into
your local repository. It brings those changes onto what’s
called a remote branch.
git merge
Even though Sally’s new commits have been fetched to your local
copy of the Git project, those commits are on the origin/master branch.
Your local master branch has not been updated yet, so you
can’t view or make changes to any of the work she has added.
Updating a2ba090..bc87a1a
Fast-forward
biology.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Git workflow
Now that you’ve merged origin/master into your local master branch,
you’re ready to contribute some work of your own. The workflow for
Git collaborations typically follows this order:
git push
The command:
will push your branch up to the remote, origin. From there, Sally
can review your branch and merge your work into
the master branch, making it part of the definitive project version.
REVIEW:
We also learned the following commands
-------------------------------------//----------------------------------------
You can set up your GitHub Pages to deploy every one of your
repositories in addition to <username>.github.io. This will allow you
to ensure all of your sites are deployed automatically whenever you
push to GitHub.
In GitHub, navigate to your <username>.github.io repository and
click Settings.
-------------------------------------//----------------------------------------
Command Line
ls
In addition to -a, the ls command has several more options. Here are
three common ones:
Cat
cat action/superwoman.txt
will output all the text from superwoman.txt.
This is a useful command for peeking at files without opening them,
and confirming the result of other commands that change the
contents of files.
cp Part I
cp source.txt destination.txt
We could also copy a file to a destination directory:
cp source.txt destination/
cp Part II
To copy multiple files into a directory, use cp with a list of source files
as the first arguments, and the destination directory as the last
argument. Here, we copy the files file1.txt and file2.txt into the
same directory.
Wildcards
cp * my_directory/
Here, w*.txt selects all files in the working directory starting with “w”
(prefix) and ending with “.txt” (suffix), and copies them
to my_directory/.
cp w*.txt my_directory/
mv
To move a file into a directory, use mv with the source file as the first
argument and the destination directory as the second argument. Here
we move my_file.txt into my_directory/.
mv my_file.txt my_directory/
To move multiple files into a directory, use mv with a list of source files
as the first arguments, and the destination directory as the last
argument. Here, we
move my_file_1.txt and my_file_2.txt into my_directory/.
To rename a file, use mv with the old file as the first argument
and the new file as the second argument. By
moving file_original.txt into file_renamed.txt, we rename the file
as file_renamed.txt.
mv file_origin.txt file_renamed.txt
rm
rm unwanted_file.txt
rm -r unwanted_directory
REDIRECTION
$ echo "Hello" > hello.txt
The > command redirects the standard output to a file.
Here, "Hello" is entered as the standard input, and is then
redirected to the file hello.txt by > .
$ cat hello.txt
>
> takes the standard output of the command on the left, and
redirects it to the file on the right. Here the standard output
of cat deserts.txt is redirected to forests.txt.
>>
$ cat deserts.txt >> forests.txt
<
$ cat < deserts.txt
< takes the standard input from the file on the right and inputs it into
the program on the left. Here, deserts.txt is the standard input for
the cat command. The standard output appears in the terminal.
|
| is a “pipe.” The | takes the standard output of the command on the
left, and pipes it as standard input to the command on the right. You
can think of this as “command to command” redirection.
$ cat volcanoes.txt | wc
You can view the output data of this chain by typing cat
islands.txt.
Sort
$ sort continents.txt
sort takes the standard input and orders it alphabetically for the
standard output (it doesn’t change the file itself). Here, the continents
in continents.txt will be listed in alphabetical order:
Africa
Antarctica
Asia
Australia
Europe
North America
South America
$ uniq deserts.txt
grep I
The above commands are a great way to get started with grep. If you
are familiar with regular expressions, you can also use regular
expressions to search for patterns in files.
grep II
$ grep -R Arctic /home/ccuser/workspace/geography
/home/ccuser/workspace/geography/deserts.txt:Arctic Desert
/home/ccuser/workspace/geography/oceans.txt:Arctic Ocean
/home/ccuser/workspace/geography/deserts.txt
/home/ccuser/workspace/geography/oceans.txt
Sed
In this case, sed searches forests.txt for the word “snow” and
replaces it with “rain.” Importantly, the above command will
only replace the first instance of “snow” on a line.
sed as we’ve used it will only rewrite the command line output and
the actual file won’t be changed. In order to rewrite the actual
file, we need to use -i at the beginning of the command: