How do I change the upstream repository's url? How do I see the diff between staged changes and commits? Here are the 27 most common git commands along with explanations of each, ready for you to copy and paste into the terminal.
git init
Initializes the current directory as a git repo
git init
bash
git clone
Copy the repository i.e. username/project
into a new folder called project
:
git clone https://github.com/username/project.git
cd project
bash
Copy username/project
into a new folder called my-project
git clone https://github.com/username/project.git my-project
cd my-project
bash
git status
Shows the status of the working tree
git status
bash
For a less verbose alternative, add -s
for short or -sb
if you want to include the
branch too:
git status -sb
bash
git pull
# Fetches the changes from the remote and merge it into local repo
git pull origin master
bash
git add
Adds the file(s) to the index, which will be committed when you run git commit
. Also
called "staging".
git add script.js
bash
That will stage the script.js
file. To add all files:
git add .
bash
This will ignores the files listed in .gitignore
git commit
Commits the changes to the local repository:
git commit -m "my commit message"
bash
Instead of adding a new commit, this will add the changes to the most recent commit:
git commit --amend
bash
git log
Shows the latest commit logs
git log
bash
git diff
Shows the changes between unstaged files (what's currently in your local project) and what's been committed:
git diff
bash
Shows the changes between staged (after running git add
) files and what's been committed:
git diff --staged
bash
git remote
A "remote" is a version of your code that's stored on the internet. By default if you create a Github repository it will be called "origin". Typically when you fork a repository, the main repo is called the "upstream" repo and has read-only access, and you commit changes to your local version "origin", which can be merged into the upstream repo with a pull request.
To list the remotes:
git remote -v
bash
Add a remote
git remote add [remote name] [remote url]
git remote add upstream https://github.com/mainOrganization/project.git
bash
Change a remote's url
git remote add [remote name] [new url]
git remote set-url upstream https://github.com/main-organization/project.git
bash
git checkout
Change to a branch. This will change your local filesystem to have what's on that branch.
git checkout my-branch
bash
Create a new branch and check it out:
git checkout -b new-feature
bash
To do this you can't have unsaved changes that will conflict with the branch you're changing to. To revert the unsaved changes on your current branch:
git checkout .
bash
To remove the unstaged changes for a particular file:
git checkout -- script.js
bash
git push
Pushes the local changes (commits) to the remote repository. This will push the main branch
to the remote called origin
:
git push origin main
bash
If the remote repository has commits that your local repo doesn't have, you'll need to pull the changes from the remote first.
If you're a bad boy, you can force push with -f
which will replace the remote's files with
whatever you've committed.
git push -f origin master
bash
Push and set the remote as upstream
This is the same as git push --set-upstream origin my-branch
git push -u origin my-branch
bash
git branch
git branch
will list your branches.
To delete a local branch:
git branch -d my-branch
bash
If the branch you're trying to delete hasn't been merged, you'll need to toss an additional
--force
flag.
If you have a lot of local branches, use this snippet to delete the local branches that have already been merged to the main branch.
git clean
Removes all the files and directories that are not yet tracked by git:
git clean -fd
bash
git merge
Merges other-branch
into the current branch:
git merge other-branch
bash
git reset
This will remove all the changes to the tracked files that have not yet been committed:
git reset --hard
bash