Send the link below via email or IMCopy
Present to your audienceStart remote presentation
- Invited audience members will follow you as you navigate and present
- People invited to a presentation do not need a Prezi account
- This link expires 10 minutes after you close the presentation
- A maximum of 30 users can follow your presentation
- Learn more about this feature in our knowledge base article
Transcript of Git basics
Register to GitHub
Setting up repos
First baby steps
The git config command lets you configure your Git installation (or an individual repository) from the command line. This command can define everything from user info to preferences to the behavior of a repository.
git config --global user.name <name>
git config --global user.email <email>
The git clone command copies an existing Git repository. As a convenience, cloning automatically creates a remote connection called origin pointing back to the original repository. This makes it very easy to interact with a central repository. Now this central repository is our course repository hosted on GitHub.
So in the next step pls clone the course's repository to your local machine.
git clone <url>
The git init command creates a new Git repository. Executing git init creates a .git subdirectory in the project root, which contains all of the necessary metadata for the repo.
So create a folder and transform it into a Git repository for today's class.
Now let's create some content in our repo
Add some content to file1
echo "Hello world" >> file1
The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. Usage e.g.: git add <file>, git add . , git add *
git add file1
git add .
After you’re happy with the staged snapshot, you commit it to the project history with git commit. The git commit command commits the staged snapshot to the project history. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicity ask it to. Along with git add, this is one of the most important Git commands.
git commit -m "my first commit"
Now edit this file and add some content
edit file1 and add the lines below
new line added
The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
You can ignoring files. Untracked files can be unnecessary files,big files or files what you wanna hide from git.
add the lines below
Now commit our changes
git commit -am "new line added\hide some files"
The git log command displays committed snapshots. It lets you list the project history, filter it, and search for specific changes. While git status lets you inspect the working directory and the staging area, git log only operates on the committed history.
Now we use git checkout to checking out files/dirs or commits to view an old state of the project. Checking out a file lets you see an old version of that particular file, leaving the rest of your working directory untouched.
git checkout <first commit s hash> file1
After the commit we hide our modifications what we did in our second commit.
git commit -am "only this is what we need"
Now we will undoing changes so create some content again
Edit file1 add the missing lines from below
git commit -am "1"
edit file3 and add some content
git commit -am "add content to file3"
edit file1 and put the missing lines
add some new line
git add new_file
git commit -am "added lines/file4"
The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. With this method we not loosing history.
git revert <hash of add content to file3>
echo "this is not needed" >> file5
echo "this is not needed" >> file6
git add *
Now complete the following steps
Reverting vs. Resetting
It's important to understand that git revert undoes a single commit—it does not “revert” back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.
#If git revert is a “safe” way to undo changes, you can think of git reset as the dangerous method. When you undo with git reset(and the commits are no longer referenced by any ref or the reflog), there is no way to retrieve the original copy—it is a permanent undo. git reset <file> removes a single file git reset --hard removes all unstaged files.
git reset --hard
So now hit the command below
echo "this is not needed again" >> file5
The git clean command removes untracked files from your working directory. Like an ordinary rm command, git clean is not undoable, so make sure you really want to delete the untracked files before you run it.
git clean -f
Create some content for the next task
edit file1 and add the last line
forgot to add
The git commit --amend command is a convenient way to fix up the most recent commit. It lets you combine staged changes with the previous commit instead of committing it as an entirely new snapshot. It can also be used to simply edit the previous commit message without changing its snapshot. But, amending doesn’t just alter the most recent commit—it replaces it entirely. With --no-edit option we don't need to add a message again.
git add file1
git commit --amend --no-edit
So now try this out
Controling remote repositories
The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. Instead of providing real-time access to another repository, they serve as convenient names that can be used to reference a not-so-convenient URL. So now add our github repo for this task.
git remote add <name> <url>
Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch, but whereas fetching imports commits to local branches, pushing exports commits to remote branches. This has the potential to overwrite changes, so you need to be careful how you use it.
A branch represents an independent line of development. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in a fork in the history of the project.
Create a new branch called experiment. This does not check out the new branch.
git branch "experiment"
The git checkout command lets you navigate between the branches created by git branch. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch. Think of it as a way to select which line of development you’re working on. Please note we used checkout before to view old commits.
Check out the specified branch, which should have already been created with git branch.
git checkout experiment
Put a line into this file
echo "This is an experiment to create a new branch" >> file5
Then, you can commit new snapshots just like we’ve seen in previous modules:
git add file5
git commit -m "file5 created"
This shows you the state of the repository before you started your feature. From here, you have the option to merge in the completed feature, branch off a brand new, unrelated feature, or do some work with the stable version of your project.
git checkout master
The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.Note that all of the commands presented below merge into the current branch.
git merge experiment
Show the existing branches and pls note the actual checked out is marked with a star
Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes. With capital -D you can fore delete a branch. Please note if you want to keep your git repository tidy remove all unused branch.
git branch -d experiment
Create a new branch for the next task.
git branch "Experiment2"
git checkout Experiment2
Now edit file1 what we used before
edit file1 and insert the missing lines
4 - Forgot to add
5 this is an experiment
The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks.
Before the rebase we have some task.
git checkout master
git add file6
git commit -m "master branch commit"
git checkout experiment2
git rebase master
git checkout master
git rebase experiment2
Now with git log you can see what we did.
Upload the changes to github
git push -f