Git: most used git commands

command line git

Here’s a list of the git commands I use most often:

git status

when to use it: to know what is the status of the files in your branch. It will show what files have been modified, added, removed, committed, etc. A snapshot of your branch’s current situation. It’s super safe because it doesn’t change anything. It just give you… the status. I git status everything, every time.

how to use it:

git status

git add

when to use it: after writing some code you have to stage it (prepare for commit)

how to use it:

git add .

or

git add -A

or

git add path/to/my/file

The first two options will stage everything you changed (“modified” or “M” in git-speak). Using the dot (.) will stage all modified files that are already known by your branch (that have been previously added and committed) and the option “-A” will stage all modifications, including new files you just added or files you deleted (“untracked” or “U” and “deleted” or “D” in git speak). I normally stage everything I’m working on but it’s possible to stage individual files by passing the file path to the command: git add path/to/my/file

git commit

when to use it: to create a commit and add a commit message

how to use it:

git commit -m "add my commit message here"

or

git commit --amend.

The “- -amend” option will allow you to commit the staged changes to a previous commit and also allow you to edit the commit message. It will open the commit message in the text editor configured in your OS.

git push

when to use it: to send the staged and committed changes to a remote repository

how to use it:

git push remote-name local-branch-name

These are for sure the top 4 because they are part of the development cycle: you write some code, you check the status of the files, you stage it (git add), you commit it and you push it up to your remote repo.

Then I also use:

git checkout

when to use it: to access the code in a specific branch or to revert changes that have not been staged (“added”) yet.

how to use it:

git checkout branch-name

to access a specific branch or

git checkout file_name

to discard the changes to file_name.

git branch

when to use it: to list all branches

how to use it:

git branch

or

git branch -a

The first option will list all local branches and the “-a” will list all remote branches

git branch -d

when to use it: to delete a local branch

how to use it:

git branch -d branch-name

or

git branch -D branch-name

Use the “-D” option if the changes in that branch haven’t been merged (one example is when you created a branch to try something out but you will not want to commit that). Careful here, there is no way (that I know of) to recover from these.

git pull

when to use it: when you want to update the content of the local branch in your local repository with the latest changes from the remote repository. It’s a combination of two git commands (fetch and merge).

how to use it:

git pull

git reset

when to use it: to revert staged changes (the changes you have already “added” but not committed yet)

how to use it:

git reset -- file_name.txt

The following are not used very often but quite helpful to know:

git stash

when to use it: when you want to save the changes in your current branch but don’t want to stage them yet.

how to use it:

git stash

or

git stash save -u “stash message” 

and

git stash list

and

git stash pop

The second option will stash changes on untracked files and save a message – for easier retrieval later. The “stash list” will list all saved stashes and “stash pop” will remove the changes from the stash and apply them to the current branch.

git remote add

when to use it: add remote reference locally. For example: you fork a repo in GitHub and then you want to add your fork as a remote reference to your local repo (so you can push your changes to your remote repo in GitHub).

how to use it:

git remote add remote_name remote_git_reference

example:

git remote add origin git@github.com:your-github-name/react.git

Sometimes when using this command you might get an error such as:

message: fatal: remote origin already exists.

You can troubleshoot this case by listing the existing remote references on that project:

git remote -v

It will show:

origin https://github.com/facebook/react (fetch)

origin https://github.com/facebook/react (push)

In this specific case it shows that “origin” is already the remote name of the original project (in react’s case, it is facebook’s). To fix this remove that remote, add yours and then push this remote to your GitHub:

git remote rm origin
git remote add origin git@github.com:your-github-name/react.git
git push -u origin master

This is just an overview of the commands I personally use most often. For a more in depth explanation of these commands and how git works check Atlassian’s git documentation (this is an easier reference) or git’s official documentation (it’s a bit dry).

The post Git: most used git commands was originally published on flaviabastos.ca

Related Posts

3 thoughts on “Git: most used git commands

  1. Pingback: git stash 101

Comments are closed.