53
loading...
This website collects cookies to deliver better user experience
> git --version
git version 2.16.2.windows.1
> sudo dnf install git-all
git --version
command again to make sure git is in your path. Once you’ve done that, you’re ready to start using it!getting_started.py
Python script. You begin by writing the code for that file until you get to a reasonable stopping point. You look over the code you wrote and, if everything looks good, you decide to commit the file to the repository.file_two.py
but it’s not quite ready to be an official part of the project history. In this case you’ll create a new branch off of the official branch (often named something like main, master, or trunk). In this way, you can experiment with an alternate timeline. If you like the work you did on the new branch, you can then merge that branch into main. This will make all of the work you did on the alternate branch part of the official history of main.git_project
and cd into it. I’m doing this on PowerShell, so it looks like this:> mkdir git_project
> cd git_project
> git init -b main
.git
. Don’t mess with it, I just want you to know that it’s supposed to be there. Also, the directory from which you ran this command is now the project’s root.getting_started.py
. Since we’re in the command line, I like to use commands to do this, but you can do it from explorer, or an IDE, or anything else you want. Either way, create the file and open it in your editor. In Linux I use> touch getting_started.py
> echo '' > getting_started.py
greet
that looks like the following:def greet(name):
return f'Hello, {name}!'
git status
and take note of the output. This is what mine looks like, yours should be fairly similar:> git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
getting_started.py
nothing added to commit but untracked files present (use "git add" to track)
getting_started.py
is listed under “untracked files.” This is because we haven’t yet told Git that this is a file we want to keep track of. In order to do this, we have to add the file to the repository. The command to do so is probably exactly what you think:> git add getting_started.py
getting_started.py
to the repository and Git will track it’s changes from now on. Go ahead and run git status
again and note the new output:> git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: getting_started.py
getting_started.py
is listed under “Changes to be committed.” This is a pretty nice segue into our next command, commit
.greet
function in the getting_started
script. It’s time to commit these changes, or make them an official part of this project’s history. Like I said earlier, we’ll have to come up with a short message to describe what we’ve done. All we’ve really done was create the greet
function, so let’s go with that. The command to commit our changes with this message is as follows:> git commit -m "Create greet function"
> git status
On branch main
nothing to commit, working tree clean
getting_started.py
though, I want to add a new function called double_this
. The function will look like this:def double_this(number):
return number * 2
> git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: getting_started.py
no changes added to commit (use "git add" and/or "git commit -a")
getting_started.py
is not listed under “Untracked files” but “Changes not staged for commit.” That’s because whenever we change a file, we have to run git add
again to stage the changes before we can commit them. In other words, once we’re ready to commit our changes, we have to first run the add
command and then the commit
command again. Don’t run the following commands (I’m about to show you a better way) but this is what they would look like:> git add getting_started.py
> git commit -m "Added double_this function"
a
as a command line argument in your commit
command. The above example can be condensed into a single command that looks like this:> git commit -am "Added double_this function"
git add
) Personally, this is the way I do commits 99% of the time. Now, “Added double_this
function” is officially part of our project’s history. But what is this history I keep talking about? Well, it’s really more of a log, let’s talk about it:git log
, which can show us our entire history, take a look:> git log
commit bfae576bca086e682eae6ca0a7b12d23621234a8 (HEAD -> main)
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:01:47 2021 -0500
Added double_this function
commit 47308246e7a8dbfe6fd3b4361f28c4a1e6643f79
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 19:50:01 2021 -0500
Create greet function
commit
” with a hash. This is, unsurprisingly, the commit hash. It’s unique to your whole project and there are commands we’ll learn later in which you will use this hash. Well, you’ll actually only use the first few characters of the hash as Git is usually smart enough to figure out the rest.git config user.name
or set it by running git config --global user.name "new-name"
if you need to. The same goes with the email address that follows.getting_started.py
script again and lets mess with the double_this
function by making it use a float instead of an integer:def double_this(number):
return number * <strong>2.0</strong>
git status
to make sure getting_started.py
is listed as “Changes not staged for commit”> git checkout getting_started.py
getting_started.py
in your code editor. Notice anything? The double_this
function should have returned to it’s original state. Isn’t that convenient? Even if you have multiple changes spanning across several files, as long as those files are tracked, the checkout
command will return them to their most recent commit.add
and checkout
to multiple files instead of running one command per file. Most of the time, I just run> git add .
> git checkout file1.txt file2.py file3.sql
> git add *.rb
checkout
command won’t work for undoing those kinds of changes, but there is an option, it’s called revert.double_this
function in our script at all. Sure, we could delete it manually and make a new commit with the message “deleted double_this.” But if we wanted to surgically remove all the work we did, we could use the revert command. The first thing you have to do is find the hash of the commit you want to undo, remember that we get that from the log
command. Check it out:> git log
commit <strong>bfae576bca086e682eae6ca0a7b12d23621234a8</strong> (HEAD -> main)
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:01:47 2021 -0500
Added double_this function
commit 47308246e7a8dbfe6fd3b4361f28c4a1e6643f79
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 19:50:01 2021 -0500
Create greet function
> git revert --no-edit bfae576bca086e682eae6ca0a7b12d23621234a8
double_this
is now gone. Go back to the terminal and run git log
to see what the history looks like:> git log
commit 275c72742cf806d2517661d5ae8f2d9e516efed2
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:51:56 2021 -0500
<strong>Revert "Added double_this function"
This reverts commit bfae576bca086e682eae6ca0a7b12d23621234a8.</strong>
commit bfae576bca086e682eae6ca0a7b12d23621234a8
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:01:47 2021 -0500
Added double_this function
commit 47308246e7a8dbfe6fd3b4361f28c4a1e6643f79
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 19:50:01 2021 -0500
Create greet function
revert
command, which is what we get when we pass the --no-edit
flag. If you wanted to write your own message, leave that flag out. Git will ask you what program you want to use to write your commit message (I usually use notepad++ on Windows and Nano on Linux). Once you’re done editing the file, close the program and the commit will be made.main
branch so that I can investigate the source of the bug and fix it. When I make a new branch from the main one, I essentially copy it: everything from the code to the commit history. But as I make changes to the new branch, the main one stays as it is. I can even switch back to it if a new, more important thing needs addressing, and then switch back to my other work when that’s done.git_project
repository, we decide we want to make a new function once again, but we don’t want it to be on the main branch until we’re done with it. First, what we do is create a new branch with the branch
command. Then we checkout that branch so that the work we do gets tracked in the new branch’s history rather than the main one.git branch
. The output should have the work “main” in green with an asterisk next to it. The asterisk indicates which branch you’re on (being “on” a branch means that the changes you make are being tracked and recorded in that branch’s history). Let’s make a new branch with the branch
command:> git branch add-new-method
add-new-method
branch:> git checkout add-new-method
Switched to branch 'add-new-method'
“. In the future, if you would like to create a branch and check it out in one command, you use the checkout
command with a -b
argument like so:> git checkout -b add-new-method
add-new-method
branch, I want you to see something. In the terminal, run git log
and see the output. It should be the exact same as it was last time you looked at it on the main
branch. In fact, until we commit anything in either branch, their log will be the same.triple_this
which will do pretty much what you’re probably expecting:def triple_this(number):
return 3 * number
> git commit -am "Add triple this function"
git log
:commit 65ed67a53aeb94b27865e401cc456e6b2b33bf2a (HEAD -> add-new-method)
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 22:55:58 2021 -0500
Add triple_this function
commit 0ea7a943b604f5efebaecb7e2ede33cf00cf7de2 (main)
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:54:16 2021 -0500
Revert "Added double this"
This reverts commit ae2a9fe016ab88a07dc6b06b9ca4417dd3f0933e.
commit ae2a9fe016ab88a07dc6b06b9ca4417dd3f0933e
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:53:37 2021 -0500
Added double this
...
(truncated)
triple_this
function is in this branch’s history. Let’s switch back to the main
branch, this is going to make it all make more sense. In the terminal type:> git checkout main
getting_started.py
script and notice that the triple_this
function is not there. That’s because that work was on the add-new-method
branch, not the main one. Additionally, run git log
again and notice that the commit for triple_this
isn’t in there.greet
method. So in the terminal, make sure you’re on the main
branch by typing git branch
and ensuring main
is starred. Now, type in the following command:> git checkout -b edit-greet-method
edit-greet-method
branch, so head into the getting_started.py
script and set the greet
method to look like this:def greet(name):
return f'What\'s up, {name}?'
> git commit -am "Edit greet function"
main
branch by typing git checkout main
in the terminal and once again note that the most recent commit message in the log is the one about reverting the double_this
method. Also note that in the getting_started.py
script, the method is how we left it, with the “Hello” greeting instead of “What’s up.”main
branch and unifying the histories. First, make sure you’re on the main
branch. First, we’re going to merge in the edit-greet-method
. In the terminal type:> git merge edit-greet-method
greet
function is now using the “What’s up” greeting instead of “Hello.” In the terminal, check out the log:> git log
commit ad0ca3092e6e49a86d8f228d2b9e7f39b8a559ca (HEAD -> main, edit-greet-method)
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 23:08:03 2021 -0500
Edit greet function
commit 0ea7a943b604f5efebaecb7e2ede33cf00cf7de2
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:54:16 2021 -0500
Revert "Added double this"
This reverts commit ae2a9fe016ab88a07dc6b06b9ca4417dd3f0933e.
commit ae2a9fe016ab88a07dc6b06b9ca4417dd3f0933e
Author: erik-whiting <[email protected]>
Date: Thu Mar 25 20:53:37 2021 -0500
Added double this
...
(truncated)
edit-greet-method
branch is now unified–or merged–with the commit history of main
. Essentially, we’ve made the work we did on the edit-greet-method
branch part of the official history.add-new-method
branch. We created that branch before we created the edit-greet-method
branch though, do you think that’s going to cause problems? Let’s find out. Run the following in the terminal:> git merge add-new-method
warning: Cannot merge binary files: getting_started.py (HEAD vs. add-new-method)
Auto-merging getting_started.py
CONFLICT (content): Merge conflict in getting_started.py
Automatic merge failed; fix conflicts and then commit the result.
add-new-method
branch, the getting_started.py
file’s git history was one way, but when we merged edit-greet-method
into the main
branch, that history was updated. This means that add-new-method
is now out of sync with the main
branch, causing a merge conflict. There’s an easy way to fix this though, don’t worry.<<<<<<< HEAD
return f'What\s up, {name}'
=======
return 'Hello, {name}'
def triple_this(number):
return 3 * number
>>>>>>> add-new-method
<<<<
type of things we don’t need, but also, we want to delete the old return method from the greet
function. In other words, your final script should look like:def greet(name):
return f'What\s up, {name}!'
def triple_this(number):
return 3 * number