Kleine CodeCrowd von programmieren lernen
Leon Hoffmann von codeCrowd Leon, 21 Aug 2021

Notiz: Die wichtigsten Git commands im Überblick

Programmieren Lernen Blog Featured-Thumbnail Die wichtigsten Git commands im Überblick

In der Kategorie NOTIZveröffentliche ich meine persönlichen Notizen, die ich mache, während ich selbst neue Themenbereiche erlerne. Vielleicht helfen diese Notizen ja auch dir.🤓

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, commit, or 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

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

P.S. Wenn dir der Artikel gefallen hat, abonniere doch die wöchentlichen Tutorials auf YouTube, den E-Mail-Newsletter oder Beteilige dich an der Diskussion auf Facebook.

Kommentare