Skip to content

Instantly share code, notes, and snippets.

@leonhoffmann86
Last active August 21, 2021 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonhoffmann86/f53043d6d6cad2c306d6ac23bfa8f694 to your computer and use it in GitHub Desktop.
Save leonhoffmann86/f53043d6d6cad2c306d6ac23bfa8f694 to your computer and use it in GitHub Desktop.
Most important Git commands

Most important Git commands

Set User

  • git config –global user.name "[name]" -> sets author name
  • git config –global user.email "[email address]" -> sets author email id

.gitignore

  • Create a .gitignore file and define which files are not added to git
  • Don't push vendors (f.e. composer or node-modules) to git
  • Never git add, git commit, or git push sensitive information to a remote repository (f.e. Passwords SSH keys, API keys, Credit card numbers), instead use enviroment variables (and gitignore them)

Setup repo

Adding a remote repo to your local

  1. git init [repository name] -> start new repository
  2. git clone [url] -> obtain a repository from an existing URL
  3. git remote -v - if you want to double check, verifie the new remote URL

Adding a local repo to a remote (GitHub)

  1. git init -b main -> initializes the local directory as a Git repository
  2. git add . -> adds the files in the local repository and stages them for commit. To unstage a file, use git reset [file name]
  3. git commit -m "[your commit message]" -> commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again
  4. git remote add origin [remote url from GitHub] -> sets the new remote
  5. git remote -v - if you want to double check, verifie the new remote URL
  6. git push -u origin main -> pushes the changes in your local repository up to the remote repository you specified as the origin

Working with local and remote repo

remotes

  • git remote -v -> checking the current remote (for git push and git pull (or git fetch git merge ))
  • git remote add [variable name] [remote server link]" -> used to connect your local repo to the remote repo on GitHub

stage

  • git status -> show the working tree status
  • git add -> if git status was used before adds all untracked from working tree status to staging
  • git add [file] -> adds a specific file to the staging
  • git add . -> adds all files to the staging

commit

  • git commit -m "[your commit message]" -> records or snapshots the file permanently in the version history and add a comment to it. You can add a description also with just repeating the -m flag and a messag is quotes, like git commit -m "[your commit message] -m "[your commit description]"
  • git commit -a -> automatically stages files that have been modified and deleted, but new files you have not told Git about are not affected
  • git tag [commitID] -> used to give tags to the specified commit

push

  • git push [origin, remote url or variable name] [branch] -> sends the branch commits to your remote repository
  • git push -u [origin, remote url or variable name] [branch] -> you can set an upstream if your want to save typing. Upstream branches define the branch tracked on the remote repository by your local remote branch. Set your upstream git push -u [origin, remote url or variable name] [branch] so you only need to type git push and it will automaticall push to the set upstream ([origin, remote url or variable name] [branch])
  • git push –all -> pushes all branches to your remote repository.

pull

  • git pull [remote url] -> pulls the copy of the current branch stored in the repos and merges it with the local repo
  • git pull --rebase -> replaces remote repo changes with the local staus. git pull --rebase is more like the svn update command than a pure git pull
  • git config --global pull.rebase "true" -> is a configuration command that autmatically does a rebase when ulling

fetch and merge

  • git fetch [remote url or variable] -> gets all data and branches from the remote repo
  • git merge [branch] -> merge the given local branch into your current local branch
  • git merge [branch variable]/[branch] -> merge the given fetched remote branch into your current local branch

Managing branches

  • git branch -a -> lists all the local branches in the current repository
  • git branch [branch name] -> creates a new branch
  • git checkout [branch name] -> used to switch from one branch to another git checkout -b [branch name] -> creates a new branch and also switches to it
  • git merge [branch name] -> merges the specified branch’s history into the current branch
  • git branch -d [branch name] -> deletes the local branch
  • git push origin --delete [branch name] -> deletes the remote branch

Managing The Repo

Check differences between staging and repo

  • git-diff - shows changes between commits, commit and working tree
  • git diff –staged -> show comparison between staged changes and the latest commit
  • git diff [first branch] [second branch] -> differences between the two branches given

Unstage files

  • git reset [file] -> unstages the file, but it preserves the file contents
  • git reset [commit] -> undoes all the commits after the specified commit and preserves the changes locally
  • git reset –hard [commit] -> discards all history (deletes/overwrites files if Git don't know about them) and goes back to the specified commit

Delete files

  • git rm [file] -> deletes the file from your working directory and stages the deletion

Check repo history

  • git log -> used to list the version history for the current branch
  • git log –follow[file] -> lists version history for a file, including the renaming of files also
  • git show [commit] -> shows the metadata and content changes of the specified commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment