Most important Git commands
Set User
git config –global user.name "[name]"
-> sets author namegit 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
git init [repository name]
-> start new repositorygit clone [url]
-> obtain a repository from an existing URLgit remote -v
– if you want to double check, verifie the new remote URL
Adding a local repo to a remote (GitHub)
git init -b main
-> initializes the local directory as a Git repositorygit add .
-> adds the files in the local repository and stages them for commit. To unstage a file, usegit reset [file name]
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 againgit remote add origin [remote url from GitHub]
-> sets the new remotegit remote -v
– if you want to double check, verifie the new remote URLgit 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 (forgit push
andgit pull
(orgit 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 statusgit add
-> if git status was used before adds all untracked from working tree status to staginggit add [file]
-> adds a specific file to the staginggit 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, likegit 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 affectedgit 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 repositorygit 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 upstreamgit push -u [origin, remote url or variable name] [branch]
so you only need to typegit 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 repogit pull --rebase
-> replaces remote repo changes with the local staus.git pull --rebase
is more like thesvn update
command than a pure git pull
Managing branches
git branch -a
-> lists all the local branches in the current repositorygit branch [branch name]
-> creates a new branchgit checkout [branch name]
-> used to switch from one branch to anothergit checkout -b [branch name]
-> creates a new branch and also switches to itgit merge [branch name]
-> merges the specified branch’s history into the current branchgit branch -d [branch name]
-> deletes the local branchgit 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 treegit diff –staged
-> show comparison between staged changes and the latest commitgit diff [first branch] [second branch
] -> differences between the two branches given
Unstage files
git reset [file]
-> unstages the file, but it preserves the file contentsgit reset [commit]
-> undoes all the commits after the specified commit and preserves the changes locallygit 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 branchgit log –follow[file]
-> lists version history for a file, including the renaming of files alsogit show [commit]
-> shows the metadata and content changes of the specified commit
This article is powered by volatos.
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.
Was ist Headless WordPress?
Was bedeutet der Begriff Headless WordPress? Wenn wir über Headless... Programmieren lernen
11 Feb 2020
Filter Hooks im WordPress Development
Filter Hooks im WordPress Development verwenden Was sind WordPress Filter... Programmieren lernen
6 Feb 2020
Action Hooks im WordPress Development
WordPress Hooks – Action-Hooks Action Hooks im WordPress Development: Mit... Programmieren lernen
14 Jan 2020
Kommentare