This repo provides a description of several common git commands. It also includes a tutorial that will offer you the opportunity to put your git skills to the test.
Check out the Table of Contents below to simply look for descriptions and explanations of git commands or dive into a quick tutorial to make sure you really know how to use git.
- Created by Linus Torvalds for the development of the Linux Kernel
- Git is a distributed version-control system for tracking changes in source code during software development.
- Allows many programmers to work on the same project at the same time without accidentally overriding the changes of someone else
Git installation: https://git-scm.com/downloads
- Windows Users :(
- Mac/Linux
- Yay! Git is already installed
- If it is not, please navigate to the link above and follow the instructions for downloading git
- Create a GitHub account: https://github.com/join
- Go to your terminal(git bash for windows) and set up your git config
- This is important, because all the commits you make will be tied to these configurations.
- This is how Github can keep track of your contributions to a project.
> git config --global user.name “Your name”
> git config --global user.email “[email protected]”
A repository is the directory in which git is tracking the changes for the project. The repository holds a .git directory that holds all of the data that git uses to keep track of changes and other information about the particular project. A repository can be local and(or) remote.
Remote is the location of a repository that is not on the machine of the programmer. An example of a remote location is github.com. Having a remote location for a project allows multiple programmers to push new code to the same project and pull the changes made by their fellow programmers.
A branch diverges from another branch(typically master) and allows programmers to add features without making changes to the master or branch that was branched from. The new branch works off of the last commit of the branch branched from. The programmer can then work on features and add commits to their branch. Using branches allows all programmers on the project to work on a separate features on separate branches all without making changes directly to the master or production branch.
Once a programmer finishes building a feature on a branch, they need to add this new code to the production branch. To do this, they can merge the branch of their new feature to the production branch. This will apply all of the changes from the branch onto the master branch or any other selected branch.
When merging, there is a chance that another programmer has made changes to some lines of code following the branch operation of someone else that made changes to the same lines on their branch. Because the brancher's branch does not know about these changes, there is a conflict on these lines when the brancher tries to merge. This will need to be resolved by the programmer merging by selecting the lines that they actually want to keep and continuing the merge.
This is the state that contains all of the files that have been modified since the last commit. This may include new files that are not yet being tracked by git or files that are being tracked and have some kind of changes. To commit these changes, they first need to be added to the staging area using the git add command.
This is the state that determines all of the changes that will be included in the next commit. This is essentially like packing a box of desired contents before you ship it. When you are ready to ship the box, you use the command git commit.
Committed files are a grouping of changes applied and associated with a message that explains the intent of the changes. You can think of a commit as a building block. Each commit gets stacked on top of all of the previous commits to build the current version of the project. These commits can be used to revert to old versions and look back to changes made in the past.
- git clone
- git init
- git status
- git add
- git commit
- git remote add
- git pull
- git push
- git branch
- git checkout
- git merge
- git log
- Clone will go to the URL provided and copy the git repository located at this link
- The link typically refers to a Github repository
> git clone <repo_url>
- Init will initialize a new git repository by creating a .git directory that will be used for holding all the git related information about the project you are working on
- This command is typically used at the start of the project
> git init
- Status gives you information about what files are tracked and un-tracked
- Also lists which of the 3 states your changed files are in
> git status
-
Add will move files from the modified state to the staging state
-
You can add all files in the current directory and subdirectories to the staging state by executing the following command
> git add .
- You can add just one file to the staging state by executing the following command
> git add <path_to_file>
- Commit will move files from the staging state to the committed state
- Remember to associate a message with your commit so that you and your fellow programmers know the purpose of the commit's changes
> git commit -m "<message_explaining_changes>"
-
Remote is the location of your non-local repository
-
Click here to read about what remote is in the git principles section
-
Remote add is the command that allows you to tell git where you want your repository to live not on your computer
-
You would do this so that you could work on the same project with multiple people
-
To add a remote location, execute the following command
- When adding a remote location, we create a name for it (kinda like a variable name for the url)
- The convention is to call our main remote location "origin"
> git remote add origin <url_of_remote_repo>
- Pull will get all of the changes made to the current branch of the remote repository and add them to your local repository
- These changes may have been made by your fellow team members
> git pull
- Push will push all of the local changes that you have committed to a branch to the remote branch of the repository
- You would do this so that you could share the changes you made with your team members
> git push
- Note: When pushing to your remote branch for the first time, you will have to execute
- The -u stands for upstream
- "origin" is the upstream location that is associated with the remote location of the repository
> git push -u origin <branch_name>
- Branch will create a new branch with the given name
- Click here to read about what branching is in the git principles section
> git branch <new_branch_name>
- Checkout will switch you to the given branch
- You will now be able to make changes and commits to the switched to branch
> git checkout <branch_name>
- Merge will allow you to apply the commits on the selected branch to the current branch
- Click here to read about what merging is in the git principles section
> git merge <branch_to_merge>
- Log will show a lits of all of the commits made to the current branch in chronological order
- You can continue pressing the enter key to continue down the list
- You can press the q key to exit
git log
In this tutorial, we are going to:
- Initialize a git repository
- Setup a remote repository on Github
- Create and work on a branch
- Merge that branch to master
- And push to Github
- Setup
- Clone the repo
- Remove the .git directory
- Initialize your git repository
- Add and commit the current files in the project
- Setting up a remote Github repository
- Creating and checking out to a branch
- Making commits on that branch
- Merging that branch and pushing to remote
- You did it!!!
If you have not already, please follow the instructions here!
- We are going to clone this repo, so that we can copy all of the code in this Github repository
- Make sure that change into a directory where you want to keep these files
> git clone https://github.com/zpinto/learn-git-with-hack.git
-
We are going remove the .git directory from inside the repo, so we can go through the motions of initializing this project to be a git repository (pretending that you wrote all this code from scratch)
- The .git directory holds all the data about all of the commits that I made and the remote location of my Github repository
- We want this to be your repository, so execute the commands below
-
If you have not yet, change into the cloned directory
> cd learn-git-with-hack
- Remove the .git directory with the following command
> rm -rf .git
- We are now going to initialize a new git repository, as if we were just starting our project
- Make sure that you are in the project directory
> git init
- Now we have a clean git repository that is ready to track our changes
- Because we created a new git repository, none of the files are being tracked yet
- We can check the status to verify this
> git status
- Let's add the files, moving them from the modified state to the staging state
> git add .
- Now that we have added the files, they are in the staging state
- Let's move our files from the staging state to the committed state
> git commit -m "initial commit"
- Yay! We committed our first changes
- We want to make our code available in a remote repository on Github
- Let's create a Github repo for our remote location
- Now we need to set the remote location in our git repository and push it to Github
> git remote add origin <github_repo_url>
> git push -u origin master
- Yay! Our code is on Github
-
Now that we have our remote repository set up, we want to add a feature to our project
-
The feature we are going to add is the solution to problem 1
-
We want to create a new branch so that we can add this feature without interfering with any other changes that our potential team members may be merging to the master branch(typically the production branch)
- It is bad practice to work on the master branch, so it is important to create a new branch when working on a feature
- Remember that we want to create a branch based off of master, so we need to be on the master branch when we create the branch
> git branch <new_branch_name>
- Now let's checkout to that branch and start working
> git checkout <created_branch's_name>
- Now that we are on the new feature's branch, let's change into the problems directory so we can work on the solution
> cd problems
> cd problem 1
- Open question1.txt file and read the question
- Code your solution in the file named answer1.py
- You can do this part yourself!
- Run tester1.py to check your answer
python tester1.py
- Now that you have made changes to the answer1.py file, we need to add and commit those changes to the branch
> git add .
> git commit -m "solve problem1"
- Yay! We committed the solution to our feature's branch
-
Now that we have a working feature on our branch, it is time to merge it to the master branch where all of our production code is
-
We need to checkout to the master branch and merge our feature's branch
-
Note: Always remember to pull before making any changes to master! On of your team members may have made changes since the last time you pulled
> git checkout master
> git pull
> git merge <created_branch's_name>
- Yay! Now our changes on the feature's branch have been applied to master
- Now let's push it to our remote repository on Github so our potential team members can see the changes as well
> git push
- Yay! It is on Github!
- To make sure that you have the flow mastered, I would recommend repeating these steps with problem2 as well.
There is much more to learn about git, but you should now have a better understanding of some of the most popular and useful aspects of git.
Now you can use git in your projects to collaborate and keep track of your projects progression.