General Flashcards

1
Q

what is origin?

A

origin is an alias on your system for a particular remote repository. It’s not actually a property of that repository.

By doing

git push origin branchname

you’re saying to push to the origin repository. There’s no requirement to name the remote repository origin: in fact the same repository could have a different alias for another developer.

Remotes are simply an alias that store the URL of repositories.

In the push command, you can use remotes or you can simply use a URL directly. An example that uses the URL:

git push git@github.com:git/git.git master

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Remotes are simply an alias that store the URL of repositories. You can see what URL belongs to each remote by using

A

git remote -v

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

This alias name is not hard coded and could be changed using following command prompt:

A

git remote rename origin mynewalias

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

“detached HEAD” state

A

usually when you do git checkout, you check out a branch, not a commit

however, you can also say git checkout SHA1 has of a specific commit

git checkout a05eff1

The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project’s working directory). Normally, when checking out a proper branch name, Git automatically moves the HEAD pointer along when you create a new commit. You are automatically on the newest commit of the chosen branch.

When you instead choose to check out a commit hash, Git won’t do this for you. The consequence is that when you make changes and commit them, these changes do NOT belong to any branch.
This means they can easily get lost once you check out a different revision or branch: not being recorded in the context of a branch, you lack the possibility to access that state easily

So if you want, say, to see an older version, you can check out this particular commit into a new branch and then make commits to this branch

git checkout -b test-branch 56a4e5c08

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

HEAD

A

Woher weiß Git, auf welchem Branch Sie gegenwärtig sind? Es besitzt einen speziellen Zeiger namens HEAD. Zeiger auf den aktuellen Commit des aktuell ausgecheckten Branches

Bei Git handelt es sich bei HEAD um einen Zeiger auf den lokalen Branch, auf dem Sie sich gegenwärtig befinden.

Let’s say we have master and anotherbranch with the same last commit. Current branch is master. So HEAD is pointing to the latest commit on master.

when we do git checkout anotherbranch, our HEAD switches to point to the commit of this another branch. At the moment, it is the same commit as that on master

Beim Commit wird der HEAD automatisch auf diesen neuen Commit nach vorne verschoben. So anotherbranch is now one commit ahead of master

if we do git checkout master again after that, the head will point again to the old commit which is the latest on master

https://git-scm.com/book/de/v2/Git-Branching-Branches-auf-einen-Blick

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

With the “git checkout” command,

A

you determine which revision of your project you want to work on. Git then places all of that revision’s files in your working copy folder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if you have changed a file and want to restore it to the state it is in the index, don’t delete the file first, just do

A

git checkout – path/to/foo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If you would like to incorporate the changes you made in branch tmp into master

A

run git merge tmp from the master branch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

list all local branches

A

git branch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

list all local and remote branches

A

git branch -a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

list all remote branches

A

git branch -r

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Merge into the current branch the remote branch next

A

git pull origin next

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

get remote branch master into your local repo, without pulling it into working space

A

git fetch origin master

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Print all refs from remote (branches, tags, …):

A

git ls-remote origin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can I tell a local branch to track a remote branch?

A

git branch -u origin/dev

In cases when you simply forgot, you can set (or change) a tracking relationship for your current HEAD branch at any time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

show remote repo info and its branches

A

git remote show origin

17
Q

check out and track remote branch

A

git checkout –track origin/dev

18
Q

push your local branch that does not exist in remote yet to remote, to create there the counterpart

A

git push -u origin dev

19
Q

remotes\origin\HEAD

A

indicates the default branch on the remote. The logic is that you can then use origin as a shorthand whenever you would otherwise use origin/master. E.g. it makes git log origin/master equivalent to git log origin.

20
Q

what to do if git branch -a does not show all remote branches and any commands including some remote branches show errors that branch is not known

A

if git fetch –all does not help (it should load all remotes into the local repo):

  1. in the project check dir .git\refs\remotes\origin there must be files for each remote branch
  2. check git config (in the project!) for this line
    fetch = +refs/heads/:refs/remotes/origin/
  3. to add this line to config run
    git config remote.origin.fetch ‘+refs/heads/:refs/remotes/origin/
  4. then again git fetch –all
  5. if this doesn’t work we need to remove remote completely from local computer. So run:

git remote remove origin
git remote add origin
git fetch origin

  1. then run git branch -a to check
21
Q

remove local branch

A

git branch -d
Git will not let you delete the branch you are currently on so you must make sure to checkout a branch that you are NOT deleting.

The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn’t been pushed or merged yet.

to force deletion
git branch -D

22
Q

rename local branch

A

git branch -m master tobedeleted

23
Q

view hash of your current branch

A

git rev-parse HEAD

24
Q

see git tree

A

git log –graph –decorate –oneline –all –full-history

25
Q

show what files changed between 2 commits

A

git diff –name-only f43d17a8a7fa913d4ccb4d15a55d2e6e8d0920af test_dependencies

we can use name of branch or hash of commit!

26
Q

adding another repository as subdirectory of your project

A

submodules

subtree

27
Q

revert unstaged changes

A

git checkout –

28
Q

check with which commit a file disappeared from the repository

A
  1. find the full path
    git log –diff-filter=D –summary | grep delete | grep MyFile
  2. find history of the file
    git log –all –full-history – path_to_file
  3. you can go through every commit

git show e4eb97d71b40e6c6e8919edc49c441c7fd3e1463 – “.gitlab-ci.yml”

or

list all commits that affected the file

git log –oneline –follow – full/path/to/MyFile.js

checkout the last commit
git checkout bd8374c – full/path/to/MyFile.js

it will show error because the file is not found any more

29
Q

filter results of git log or git diff

A

–diff-filter=[(A|C|D|M|R|T|U|X|B)…​[*]]
Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …​) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

30
Q

force git pull to overwrite ALL local files

A

git fetch –all

downloads the latest from remote without trying to merge or rebase anything.

git checkout -b backup-master
backup your current branch

git reset –hard origin/master
resets the master branch to what you just fetched. The –hard option changes all the files in your working tree to match the files in origin/master

31
Q

force git pull to overwrite SOME local files

A

git fetch

git checkout origin/master

32
Q

pull changes from remote while you have your own local changes which could lead to conflicts

A

git stash
git pull origin master
git stash pop —» here resolve conflicts