Table of contents
- What's Git
-
Most common Git commands
- git clone
- git init
- git remote add
- git add
- git commit
- git log
- git push
- git branch
- git branch {branch-name}
- git branch -d {branch-name}
- git branch -D {branch-name}
- git push {remote-name} --delete {branch-name}
- git merge
- git fetch
- git pull
- git checkout
- git checkout -b {branch-name}
- git checkout {commit-hash}
- git checkout {commit-hash} {file-name}
- git reset
- git reset --hard {commit-hash}
- git revert
What's git
Git is distributed version-control system for tracking changes in files. It was originally created by Linus Torvalds in 2005 for development of the linux kernel as an open-source project. Current maintainer of git is Junio Hamano. Having a distributed architecture, Git is an example of Distributed Version Control System. That means that rather than having only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's working copy of the code is also a repository that can contain the full history of all changes.
Most common Git commands
Here are the most common Git commands and their explanation.
NOTE: As Git hosting I'm going to reffer to GitHub, but it's the same for every other Git hosting (GitLab, BitBucket, etc.)
git clone
COMMAND
:
git clone {url-of-the-project}
git clone -b {some-specific-branch} {url-of-the-project} # clone from specific branch
git clone {url-of-the-project} {folder-in-which-to-clone} # clone in specific folder
DESCRIPTION
:
- It initializes Git, adds new remote and clones some existing project from GitHub to your computer
NOTE
:
- This command is used only once per project
- That project can be empty, or it can already have some files in it
- When cloning a project, by default it will clone the master branch, if not specified otherwise
USAGE
:
- If we want to get some project from GitHub and start working on it
git init
COMMAND
:
git init
DESCRIPTION
:
- It initializes Git in the folder in which we're calling the command
NOTE
:
- This command is used only once per project and this is usualy the first command you'll run in a new project
- When initializing Git in a project, by default it will initialize it on master branch
USAGE
:
- Convert an existing, unversioned project to a Git repository or initialize a new, empty Git repository
git remote add
COMMAND
:
git remote add {name} {url-of-the-project}
DESCRIPTION
:
- It adds new remote
- It takes two arguments:
- a remote name eg.
origin
- a remote url eg.
https://github.com/{username}/{repo}.git
- a remote name eg.
NOTE
:
- At the begining of the project, we should set new remote
- We can add many remotes, as long as they have different names (
origin
,upstream
, etc.) - If the remote name already exists, Git will throw an error
USAGE
:
- We want to add new remote, so we can push our local repository to GitHub
git add
COMMAND
:
git add {files-to-add}
git add . # add every change
DESCRIPTION
:
- It adds a change in our working directory to the staging area
- Basically it tells Git that we want to include these changes to some file in the next commit
NOTE
:
- It doesn't really affect the repository until we make a commit
USAGE
:
- We've made some changes in our working directory (our project), and we want to include these changes in our next commit
git commit
COMMAND
:
git commit # it will open some editor so you can write your commit message
git commit -m "some message" # commit message is written inline
DESCRIPTION
:
- It records every change that we've made in our working directory (that we've added with
git add
) as an object in our local repository
NOTE
:
- It will save the changes in our local repository, not on GitHub
USAGE
:
- We've made some progress in our project, and we want to save our project in this exact state
- It's sort of like a backup for a project
git log
COMMAND
:
git log
DESCRIPTION
:
- It will show all the commits in the project
USAGE
:
- We've want to see all previous commits that occured in the project
git push
COMMAND
:
git push -u {remote-name} {branch} # it pushes to GitHub, and sets the branch as upstream so that next time you won't have to specify the branch on which to push
git push {remote-name} # if you have already set an upstream branch
DESCRIPTION
:
- It will push (copy) all your local repository content to GitHub
NOTE
:
- Once we push to GitHub, we can see all our work on the remote repository (GitHub)
- You have to set remote name first with
git remote add {name} {url}
, so that Git will know what the remote is
USAGE
:
- We want to be sure that the commits we've made, won't be lost
- We want to be able to access the project from multiple computers
- We want to be able to collaborate with other people on a project
git branch
COMMAND
:
git branch
DESCRIPTION
:
- It will show as all our local branches
USAGE
:
- We want to see what branches do we have locally
git branch {branch-name}
COMMAND
:
git branch {branch-name}
DESCRIPTION
:
- It will create new branch
NOTE
:
- The branch will be created, but you will remain on the same branch (it won't switch you to the newly created branch)
USAGE
:
- We want to create a new branch
git branch -d {branch-name}
COMMAND
:
git branch -d {branch-name}
DESCRIPTION
:
- It will delete a branch that's already merged
NOTE
:
- This is safest way to delete a branch
USAGE
:
- We want to delete a branch that's been merged and we don't need that branch anymore
git branch -D {branch-name}
COMMAND
:
git branch -D {branch-name}
DESCRIPTION
:
- It will force delete a branch even if it's not already merged
NOTE
:
- Do this if you are 100% sure that you don't need that branch
USAGE
:
- We want to force delete a branch, and we don't care if it's merged or not
git push {remote-name} --delete {branch-name}
COMMAND
:
git push {remote-name} --delete {branch-name}
DESCRIPTION
:
- It will delete a remote branch (a branch that's on GitHub)
NOTE
:
- We can delete a remote branch directly from GitHub
USAGE
:
- We want to delete some remote branch
git merge
COMMAND
:
git merge {branch-name}
DESCRIPTION
:
- It will merge the specified branch on the current branch
NOTE
:
- If there are conflicts when you merge, you'll have to fix the conflicts first, remove the part you don't need and leave the part that you need
- The current branch will be updated to reflect the merge, but the target branch will be completely unaffected
USAGE
:
- We want to combine work that's been done on two separate branches
git fetch
COMMAND
:
git fetch {remote-name} {branch-name}
DESCRIPTION
:
- It will copy the contents from a remote repository branch into your local repository
NOTE
:
- Git will automatically make local branch with the same name as the remote branch
- It won't affect your current branch or any other branch
- If you run
git branch
it won't show you the newly created branch, to see it you will have to rungit branch -r
, however you can checkout to that branch withgit checkout {branch-name}
USAGE
:
- We want see the work that's been done on some remote branch
- We want to review the commits before merging them into our local repository
git pull
COMMAND
:
git pull {remote-name} {branch-name}
DESCRIPTION
:
- It will copy the contents from a remote repository branch and merge all the changes into the current branch
- Basically it will do
git fetch
andgit merge
NOTE
:
- When you run
git pul
l, it will automatically merge the branch you are pulling with the branch you are currently on
USAGE
:
- We want to update our local repository with a version from remote repository
git checkout
COMMAND
:
git checkout {branch-name}
DESCRIPTION
:
- It will switch to some branch
NOTE
:
- Git checkout has many uses
USAGE
:
- We've created some branch with git branch {name}, and now we want to switch to that branch
git checkout -b {branch-name}
COMMAND
:
git checkout -b {branch-name}
DESCRIPTION
:
- It will create and switch to that branch
NOTE
:
- It basically does
git branch {branch-name}
andgit checkout {branch-name}
USAGE
:
- We've created some branch with
git branch {name}
, and now we want to switch to that branch
git checkout {commit-hash}
COMMAND
:
git checkout {commit-hash}
DESCRIPTION
:
- It will go back to some commit so we can inspect the files at that point in time (when we commited)
USAGE
:
- We've want to see how the project looked like at some point in time (when we commited)
git checkout {commit-hash} {file-name}
COMMAND
:
git checkout {commit-hash} {file-name}
DESCRIPTION
:
- It will get the version of some file and it will put it in place of our current file
USAGE
:
- We want to revert back some file as it was at some point in time (when we commited)
git reset
COMMAND
:
git reset {commit-hash}
DESCRIPTION
:
- It will go back to some commit, but it will delete all commits that were after the specified commit
NOTE
:
- All commits that were after the specified commit will be deleted
- It won't make any changes in the working directory (files will be preserved as they were)
USAGE
:
- We started working on something, and then we realized that we are doing something wrong and we want to fix that
git reset --hard {commit-hash}
COMMAND
:
git reset --hard {commit-hash}
DESCRIPTION
:
- It will go back to some commit, but it will delete all commits that were after the specified commit and it will revert all the files as they were at the specified commit
NOTE
:
- All commits that were after the specified commit will be deleted
- All the files will be reverted as they were at the commit that we've specified
USAGE
:
- We started working on something, and then we realized that we are doing something wrong and we want to start all over again
git revert
COMMAND
:
git revert {commit-hash}
DESCRIPTION
:
- It will go back to some commit, but it will create new commit while doing this
NOTE
:
- All commits will be preserved
- It will create new commit
- Git revert is the safest command if we want to go back to some commit
USAGE
:
- When we want to return to some point in time (to some commit), but we want to be sure that no commit will be lost
If you think you have found any misleading information or a typo in this post, please comment below
Top comments (0)