Git Command line cheat sheet
Git is an open source version control system that enables developers to work together on projects while maintaining a history of what changed and by who. At first, it can seem overwhelming but with steady practice, the learning curve isn’t all that steep.
1) Starting with setup
Set up git in the local folder/directory you want to track changes by entering the following command.
$ git init
$ git status
2) Set up your remote repository on GitHub.
At this point, reate a new repo in either GitHub, Gitlabs, or Bitbucket. For this article, I’m using GitHub. Sign in, locate the create new project button and enter a name for your repository along with a brief description of your project.
Next, leave the rest of the options as default, > click Create repository (repo).
Copy the repo’s url https://github.com/YOUR-USERNAME/PROJECT-NAME.git
3) Switch back on our local machine:
We will set the remote origin for our project.
$ git remote add origin https://github.com/YOUR-USERNAME/PROJECT-NAME.git
Q: Can I add more than one remote location?
Yes, in addition to the origin set above say add another remote location.
git remote add github https://github.com/Company_Name/repository_name.git
Now you can push to two different remote locations.
If you need to change the remote URL:
git remote set-url origin https://github.com/YOUR-USERNAME/NEW-REPOSITORY.git
4) Adding your files to the repo
A. Every good project has a readme.md file.
$ touch README.md
B. Typing Git status
will reveal README.md as an Untracked file.
C. Let’s add it to our local repo.
$ git add README.md
D. Next, we will commit the file to the project. This also lets us note our changes.
$ git commit -m "first commit adding README.md to our project"
E. Finally, since the file is added and committed now we will push the new changes from out local project up to the repo on GitHub.
$ git push origin master
Open a browser and navigate to your repo on github to see the changes you just pushed.
🥁 … That’s it, congratulations now you know how to commit changes locally and to gitHub. 🎉
What’s our new montra? 📢
add (a file)
commit (your work)
push (to origin/branch)
pull (newest changes before creating new work)
Shortcut: to add & commit all at once: git commit -am "added a detailed message to this commit"
Push new local branch to repo
First time push
git push --set-upstream origin {Name of local branch}:{Name of remote branch}
Then afterwards
git push -u origin {Name of remote branch}
Git Clone
The git clone command copies an existing Git repository. This is sort of like svn checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is an isolated environment from the original repository.
git clone git@github.com/A-USERNAME/THE-REPO.git
As a convenience, cloning automatically creates a remote connection called origin pointing back to the original repository. This makes it easy to interact with a central repository.
Keeping a fork up to date
1. Clone your fork:
git clone git@github.com/YOUR-USERNAME/YOUR-FORKED-REPO.git <br> ### 2. Add remote from original repository in your forked repository:
cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream <br> ### 3. Updating your fork from original repo to keep up with their changes:
git pull upstream master <br> # setting up .gitignore file
First let’s remove the existing files from the repository:
`find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch`
Now add the line to your .gitignore file, which is at the top level of your repository (or create one if it isn’t there already).
.DS_Store
Then add and commit the file
git add .gitignore
git commit -m '.DS_Store banished!'
if you are not familiar with Markdown documentation
Referances & Resources:
- github-git-cheat-sheet
- Official Git
- Git site:
- atlassian getting started
- Youtube videos:
- Printable sheet PDF:
- Scribd: