Skip to content

Instantly share code, notes, and snippets.

@calvimor
Last active September 11, 2023 01:12
Show Gist options
  • Save calvimor/3a1c0f2cb714ab7d075897a55fc12d91 to your computer and use it in GitHub Desktop.
Save calvimor/3a1c0f2cb714ab7d075897a55fc12d91 to your computer and use it in GitHub Desktop.
Common Git Commands

Git Commands

Initialize git repo

First of all you have to initialize the repository with git init. You can run this command on an empty directory or not.

Once you have initialized the repo, there is a workflow in git that you should do every time you make changes like add/delete/update files.

git status
git add -A
git commit -m "a little description"

Basic workflow

Let you know what is the status of the files and which areas they are also. This is one of the most common commands git status

There are three areas in git

  • Working
  • Staging
  • Repository

Working area

git add command

  • dash Capital A (-A) adds all file including untracked ones: git add -A

  • dash u (-u), only add updated files, not new ones: git add -u

To undo changes on a file in this area you can run git checkout <filename>

If you need to delete some temporary files or some others and these files are not tracked, you do git clean

But you need more options whit this command like -n or -f. git clean -n show all files that will be deleted git clean -f delete these files

You can save your changes for a later commit and it will set your working area clean

git stash
git stash list

This command pops the top item off of the stash and applies it to the current working copy

git stash pop

Staging area

git reset --soft HEAD~1

git reset --hard HEAD~1

Repository area

History of the project like snapshots made by each commit

git log for list commits in current branch git log --format=short or git shortloglist commits by user with the number of commit and their titles git shortlog -sne when you want to show -e email, -n number of commit in decreasing order per user, -s omit the summary of commits each user has made If you need to compare two last commits, you run git diff HEAD~1..HEAD or git diff HEAD~1.. both commands return the same result

If you want to get a log of all references where HEAD has pointed

git reflog

Colaborate commands

It's easier to use SSH URL rather than the HTTP URL if you need to push a lot commits and don't want to authenticate every time you share your changes from local to remote

git fetch
git merge

These two comands are equals to

git pull

If there is problem with the remote branch, you can run

git branch --set-upstream master origin/master

The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to Branch master set up to track remote branch master from origin.

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> master

Branching

Create a new branch that point to a HEAD (actual commit)

git branch <name>

Create a new branch that point to a specific commit

git branch <name> <commit SHA>

Create a new branch and check it out

git checkout -b <name>

Rename a created branch

git branch -m <name> <new name>

Delete a branch

git branch -d <name>

Create a branch from a stash. It will apply the stash to a new branch and check it out

git stash branch <name>

Delete a branch that is not fully merged

git branch -D <name>

Recovery a deleted branch. First with git reflog identify the SHA commit that was deleted

git branch <name> <commit SHA>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment