Skip to content

Instantly share code, notes, and snippets.

@Alchemik
Last active February 3, 2018 07:24
Show Gist options
  • Save Alchemik/e877930d616606f2a6d1 to your computer and use it in GitHub Desktop.
Save Alchemik/e877930d616606f2a6d1 to your computer and use it in GitHub Desktop.
Git commands
Notes Git commands
Configure the author name and email address to be used with your commits.Note that Git strips some characters (for example trailing periods) from user.name. git config --global user.name "Sam Smith"
git config --global user.email sam@example.com
Create a new local repository git init
Create a working copy of a local repository: git clone /path/to/repository
For a remote server git clone username@host:/path/to/repository
Add one or more files to staging (index) git add <filename>
git add *
Description Command
Commit changes to head (but not yet to the remote repository) git commit -m "Commit message"
Commit any files you've added with git add, and also commit any files you've changed since then git commit -a
Send changes to the master branch of your remote repository git push origin master
List the files you've changed and those you still need to add or commit git status
If you haven't connected your local repository to a remote server, add the server to be able to push to it git remote add origin <server>
List all currently configured remote repositories git remote -v
Create a new branch and switch to it git checkout -b <branchname>
Switch from one branch to another git checkout <branchname>
List all the branches in your repo, and also tell you what branch you're currently in git branch
Delete the feature branch git branch -d <branchname>
Push the branch to your remote repository, so others can use it git push origin <branchname>
Push all branches to your remote repository git push --all origin
Delete a branch your local repository git branch -d the_local_branch
Delete a branch on your remote repository git push origin :<branchname>
Fetch and merge changes on the remote server to your working directory git pull
To merge a different branch into your active branch git merge <branchname>
View all the merge conflicts git diff --base <filename>
View the conflicts against the base file git diff <sourcebranch> <targetbranch>
Preview changes, before merging git diff
After you have manually resolved any conflicts, you mark the changed file git add <filename>
You can use tagging to mark a significant changeset, such as a release git tag 1.0.0 <commitID>
CommitId is the leading characters of the changeset ID, up to 10, but must be unique. Get the ID using git log
Push all tags to remote repository git push --tags origin
If you mess up, you can replace the changes in your working tree with the last content in head: git reset --hard origin/master
Changes already added to the index, as well as new files, will be kept git checkout -- <filename>
Instead, to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it, do this git fetch origin
Search the working directory for foo() git grep "foo()"
$ git clone https://github.com/Alchemik/apps-android-wikipedia.git
//git remote add <shortname> <url>
$ git remote add wiki-origin https://github.com/wikimedia/apps-android-wikipedia.git
> <empty>

Change 'origin' of repository

$ git remote rm origin
$ git remote add origin git@github.com:aplikacjainfo/proj1.git
$ git config master.remote origin
$ git config master.merge refs/heads/master
$ git remote add wiki-origin https://github.com/wikimedia/apps-android-wikipedia.git
$ git fetch wiki-origin
$ git merge wiki-origin/master
$ git remote
> origin
`$git remote -v`
origin  https://git.wikimedia.org/git/apps/android/wikipedia.git (fetch)
origin  https://git.wikimedia.org/git/apps/android/wikipedia.git (push)
upstream        https://github.com/wikimedia/apps-android-wikipedia.git (fetch)
upstream        https://github.com/wikimedia/apps-android-wikipedia.git (push)
$ git push alchemik-wikipedia master
$ git branch newfeature
// switch to new branch
$ git checkout newfeature
// squash the last 3 commits
$ git rebase -i HEAD~3

This will bring you into your editor with some text that will look something like this:

pick df94881 Allow install to SD pick a7323e5 README Junkyism pick 3ead26f rm classpath from git

To squash those commits into one, change to something like this:

pick df94881 Allow install to SD squash a7323e5 README Junkyism squash 3ead26f rm classpath from git

//Submitting a Pull Request

$ git push origin newfeature
How to commit to remote git repository?

All You have to do is git push origin master, where origin is the default name (alias) of Your remote repository and master is the remote branc

GIT

Start i konfiguracja

ssh-keygen – do generowania klucza ssh potrzebnego, by później połączyć się ze zdalnym repozytorium, np. z GitHub
git config  user.name Mateusz – ustawienie user name dla repo na Mateusz
git config  user.email kontakt@mateuszmidor.com – ustawienie user email na kontakt@…
git config –global user.name Mateusz – ustawienie globalnego user name
git config –global user.email kontakt@mateuszmidor.com – ustawienie globalnego user email

Podstawy

git init – zakłada nowe repo w bieżącym katalogu
git clone -b [branch name] [url] – klonuje gałąź ze zdalnego repo do bieżącego katalogu
git add . – wrzuca wszystkie pliki z bieżącego katalogu do stagging area (patrz: wyjaśnienie koncepcji)
git commit -am “Pierwszy commit” – zapisuje zmiany ze stagging area do repo

git checkout SHA1code Readme.txt- przywraca określoną przez SHA1code wersję pliku z repo
git checkout master Readme.txt – przywraca najnowszą wersję pliku z repo

Zdalne repozytorium

git remote – wyświetla skonfigurowane repozytoria zdalne
git remote -v – repozytoria zdalne z informacją o ich url
git remote add [nazwa] [url] – dodanie zdalnego repo do lokalnej konfiguracji
git remote rm [nazwa] – usunięcie zdalnego repo z lokalnej konfiguracji
git pull [nazwa] [branch] – pobranie i zmergowanie zmian z [repo][branch], np. git pull origin master
git mergetool – narzędzie do ręcznego mergowania zmian
git push [nazwa] [branch] – wysłanie zmian do zdalnego repo na wskazany branch
git reset –hard HEAD – przywraca cały workdir do wersji HEAD z repo

Informacje o wersjach

git status – stan katalogu pod kontrolą wersji
git log – wyświetla historię commitów
git reflog – historia akcji na repo w formie kompaktowej
git show – pokazuje ostatniego commita + diff
gitk – graficzna nakładka na historię commitów

Branchowanie

git branch -a – pokaż branche lokalne + zdalne
git checkout -b newfeature – utworzenie/switch do brancha newfeature
git checkout -b master – powrót do głównego brancha
git branch -d newfeature – usunięcie brancha newfeature
git push origin newfeature – wysłanie zmian do zdalnego repo na branch newfeature
git push origin :newfeature – usunięcie brancha newfeature ze zdalnego repo
git merge newfeature – będąc na master branch: zmergowanie brancha newfeature do maina

Utworzone repo na GitHubie

Z poziomu Git'a - najpierw przejdz do katalogu z src
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Alchemik/path_to_src_folder
git push -u origin master
$ git log --pretty=oneline      shows a terse history mapping containing the commit id and the summary
$ git rebase --interactive      provides the summary for each commit in the editor it invokes
                                if the config option merge.summary is set, the summaries from all merged commits will make their way                                     into the merge commit message
$ git shortlog                  uses summary lines in the changelog-like output it produces
$ git format-patch              use it as the subject for emails
                                reflogs, a local history accessible with git reflog intended to help you recover from stupid mistakes,                                   get a copy of the summary

git fetch origin or git fetch --all - fetch all the remote branches to local

git checkout --track origin/<The_remote_branch you want to switch over>

Links

http://datamapper.org/using-git.html

http://ben.balter.com/2015/12/08/types-of-pull-requests/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment