-
Notifications
You must be signed in to change notification settings - Fork 0
gitBook
- Summary
- Commands
- Remotes
- Repo
- Contribute
- Branches
- Submodules
- Merging
- SSH
//////////////////////////////////////////////////
This snipped needs clear rework. This does not suit our quality standards.
//////////////////////////////////////////////////
-
blame
: who has change what -
git rm --cached mylogfile.log
or for directoriesgit rm --cached -r mydirectory
: Remove file wihtout deleting local copy -
git push origin branchname
: push to origin repository. No requirement to name the remote repository origin: in fact the same repository could have a different alias for another developer -
git mv
: like the normalmv
-> rename and/or move files (safer and better than justmv
) - Get total commit count of an already checked-out repo:
git log --oneline --all | wc -l
(last one is a lowercase L)
remote
s are simply an alias storing the url of repositories. Origin
(widely used) is just a name (the alias).
Show available remotes:
git remote -v
origin git@github.com:holzkohlengrill/blahblub.git (fetch)
origin git@github.com:holzkohlengrill/blahblub.git (push)
Push to an origin: git push
origin master
HTTPS > SSH or vice versa
If you want to switch from https to ssh you need to change your origin/remote (or add a new one). Basically you modify an existing origin
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
Adds a remote named origin
: git remote add origin https://github.com/user/repo.git
.
Removes/deletes a remote named origin
: git remote remove origin
git clone https://holzkohlengrill@bitbucket.org/holzkohlengrill/yourRepo.git myMightyFolderNameForMyProject
git clone git@github.com:WHOEVER/WHATEVER.git PROJECTNAME
cd existing_folder
git init # Initialise git "env"
git remote add origin https://github.com/TeamFlowerPower/kb.wiki.git # Add repo URL to remotes
git add . # Add all files
git commit # Commit files to repo
git push -u origin master # Push and set upstream (-> `-u`)
Follow this guide: https://docs.github.com/en/free-pro-team@latest/github/using-git/splitting-a-subfolder-out-into-a-new-repository
Basically you apply git filter-branch
on the desired branch and (sub)folder, change the origin and push to the new repository:
- Filter:
git filter-branch --prune-empty --subdirectory-filter dir/to/keep/ branchToKeep
- Check:
git log
- Create new repository and add it as the new origin:
git remote set-url origin git@new-repo.org/repo.git
- Check:
git remote -v
- Push:
git push -u origin branch
Say you have an old repository and due to infrastructure it gets pushed to a new one but without preserving the history. You want to combine those two afterwards.
old repo: A-B-C-D-E
new repo: F-G-H-I-J-K
combined: A-B-C-D-E-F-G-H-I-J-K
How this is done is described here
-
add >> wildcards work as well: e.g.
git add mypath/VL*/*yaml
(>> note, if you put single quotes, it prevents the shell from expanding the wildcard and might produce unexpected results, also depends on how or whether or not your shell is expanding wildcards) -
commit
- partial commit:
git commit -m "<message> #-> dot after this message is important!!" .
- partial commit:
- push >> apply changes
git add contributors.txt # add a file or folder
git commit -m 'Initial commit with contributors' # -m : message; -a : all (Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.)
git push origin master # origin: default remote by git clone, master: default branch; push either by remote(origin master) or url(git@github.com:git/git.git master)
You can do an interactive commit with:
git commit -p # patch
# or
git commit -mp "Some commit message"
-> You might want to select only parts of the file to be committed. For the available commands see the table below:
Abbreviations:
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
You may have changes that you want to "save" for later. Git stash works like a stack.
# Save you work (push to stack) and reset to the last clean state
git stash
# View stash stack
git stash list
# Get back saved state from stash (pop from stash stack)
git stash pop
# Name a stashed state
# Starts an interactive prompt to select hunks for stashing (choose what to stash)
git stash save -p "Name of your stash"
# ^^---- print diffs + starts interactive session (can be ommitted if you only want to give it a name)
Cmd | Description |
---|---|
y |
stash this hunk |
n |
do not stash this hunk |
q |
quit; do not stash this hunk or any of the remaining ones |
a |
stash this hunk and all later hunks in the file |
d |
do not stash this hunk or any of the later hunks in the file |
g |
select a hunk to go to |
/ |
search for a hunk matching the given regex |
j |
leave this hunk undecided, see next undecided hunk |
J |
leave this hunk undecided, see next hunk |
k |
leave this hunk undecided, see previous undecided hunk |
K |
leave this hunk undecided, see previous hunk |
s |
split the current hunk into smaller hunks |
e |
manually edit the current hunk |
? |
print help |
We assume you are currently on master
.
- Apply potential changes from origin/master to your (modified) master branch
git rebase origin/master
(userebase
to avoid merge commits)- If there are merge conflicts fix them and continue with
git rebase --continue
-> creates a merge commit in this case - If the conflicts are to bad stop (
git rebase --abort
) and try amerge
instead
- If there are merge conflicts fix them and continue with
- Create the new branch:
git branch myNewBranch
- Reset your
master
back toorigin/master
:git reset --hard origin/master
- Check the log of your branches if you did everything correct then push your branch
git diff
- Fork and clone the repo where you want to contribute
- Create a local branch for your changes
- Make, commit and push your changes to your local branch
- On the web page for the upstream repo go to the pull requests tab and click on "New Pull Request"
- Select the branch you want to submit to
- Describe your changes (what problem does it solve, user-visible impact, how did you solve it)
Command | Description |
---|---|
git log |
shows last commits (+metadata) |
git log --name-status |
Show changed files and their status per commit |
git whatchanged -p or git log -p
|
Show textual diffs of last commit |
git whatchanged |
shows changed files + commit messages |
To see commits of one specific author you can use both options with the --author="<commiters-name>"
argument. You don't have to state the complete name.
git commit --amend # Opens the default editor for editing
-
git commit --amend
(an editor will open -> modify the commit message; save & close) git push --force
-
git rebase -i HEAD~4
(say you want to edit the 4th commit message you see ingit log
) - An editor will open: change
pick
tor
(editing the commit message at this step has no effect; you will see a short command reference in the editor as comments); close the editor - Another editor will open: here you can modify the commit message; close the editor
git push --force
-> git reflog
might be of use here
-
git rebase -i -p HEAD~4
(say you want to edit the 4th commit message you see ingit log
) git commit --amend --author "New Name <new-email@address.com>"
git rebase --continue
git push -f
-> git reflog
might be of use here
-
git reset --soft HEAD~1
: Throws your last (local) commit away (make sure you're on HEAD)- Regarding the
~1
see https://stackoverflow.com/a/2222920/4773274
- Regarding the
-
git reset --hard HEAD~1
: Throws your last (local) commit away and reverts all your local changes to the last commit (make sure you're on HEAD) -
git add ./blah && git commit --amend
: Include a file to your last commit (i.e. if you have committed only for one file but forgot another which matches you commit message)
This is also useful if your rewrote the git history (locally) and want to undo the rewrite (just checkout the hash from git reflog
).
See history:
$ git reflog
07540e8 HEAD@{0}: checkout: moving from gen4-1 to master
cdc974c HEAD@{1}: reset: moving to HEAD
cdc974c HEAD@{2}: reset: moving to HEAD@{5}
b15c929 HEAD@{3}: reset: moving to HEAD~1
cdc974c HEAD@{4}: reset: moving to HEAD~1
185ac8a HEAD@{5}: reset: moving to HEAD~1
46c49ef HEAD@{6}: revert: Revert "Added Smart Algo for UDP client"
185ac8a HEAD@{7}: pull origin master: Merge made by the 'recursive' strategy.
cdc974c HEAD@{8}: cherry-pick: Map: added json output/conversion to duplicate finder
b15c929 HEAD@{9}: checkout: moving from master to gen4-1
Go to specific point in time:
git reset 'HEAD@{1}'
See here
You are on a branch (!*) and accidentally committed and pushed a file modification.
- Checkout the original file state (in this case it's
master
):git checkout master ./path/to/file/to/reset
- Commit the change:
git commit -m "Reset master state of irrelevant file" .
(the message is not so important since it will be removed later on) - Do interactive rebase for the last x commits (here: 4):
git rebase -i HEAD~4
- A text editor opens; For the commit of step 2:
- Make sure you move the commit after the one introducing it (latest commits are at the bottom)
- Change
pick
tosquash
- Save and close the editor
- A text editor opens; For the commit of step 2:
- Check if everything went fine (->
git status
,git log
,git whatchanged
,git log -p
) - Force push:
git push --force
orgit push --force-with-lease
* Works also on master but this solutions uses a force push which is only recommended on non-shared branches (-> not master
); read also about the (force push with lease option)[https://salferrarello.com/never-git-push-force/]
Nice article about rebase
: https://nathanleclaire.com/blog/2014/09/14/dont-be-scared-of-git-rebase/
We assume that we are currently on the feature branch and want to rebase on top of master
.
-
git fetch
# get current state - If there is something to commit:
git commit -m "blahMessage" .
-
git rebase origin/master
# Put my changes ontop of current status/changes (on server) git push
A visualisation can be found here.
See also: https://blog.algolia.com/master-git-rebase/
git rebase --onto <newbase> <oldbase>
git branch # all available branches
git branch <newBranchName> # Creates new branch >> Note: This does NOT enter the branch
git branch -a # all available & deleted branches
git branch -r # all removed branches
git push -u origin <branchName> # Push branch to remote
Output example of git branch -a
:
* master
dev
remotes/origin/HEAD -> origin/master
remotes/origin/master
*
inidcates current branch
Maybe helpful (not checked/tried) http://stackoverflow.com/questions/1778088/how-to-clone-a-single-branch-in-git
git checkout <branchName>
git checkout <commit-hash> # Switch to the state after that specific commit
# e.g.
git checkout e442f37a10291a7d93fd9c5068b4e10f554738ed
Works also for individual files (checkout
is only applied to the current path, so, be aware of this):
Revert by checking out latest commit or master.
git merge <branchName>
Combines the specified branch’s history into the current branch
This is useful when you want to test or integrate your changes with the current state of the master branch.
git merge feature master
# <into> <from>
git branch -d <branchName>
Delete all branches that are deleted on the remote server
git fetch --prune
The repository ("super repo") pulling the submodules can be seen as a meta repository. It only holds the commit ID's of its submodules (and possibly files that are checke into the super repo like in any other repository).
$ git submodule add --name <submodule-name> git@github.com:holzkohlengrill/submodule-repo.git <path-in-super-repo>/
path-in-super-repo
and submodule-name
are optional.
Do this only once:
$ git submodule init
Then:
$ git submodule update
- Checkout the commit of the submodule to update (
cd
into the submodule folder andgit checkout
the commit) -
cd
back to the super repo root folder and commit the changes (to verify look at the output ofgit status
)
- Left: Fast-Forward (no changes on master until merge) >> Linear result (default in git)
- Right: No-Fast-Forward >> Merge is visible (as a commit)
When a merge conflict come up your file(s) will be edited by git (some markers and the conflicts will be inserted):
<<<<<<< HEAD:file.txt
Hello world
=======
=======
Goodbye
>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
Where 77976da35a11db4580b80ae27e8d65caf5208086
is the commits hash you tried to merge from.
- Fix merge conflict in stated files by git (see markers).
git add <filename>
-
git commit -m "blah, fixed merge conflict"
(NO DOT at the end!) git push origin master
Cloning using ssh and private keys is also possible, you only need to add your public ssh-key cat ~.ssh/id_rsa.pub
(implied that you ever run ssh-keygen
otherwise id_rsa.pub does not exist) to bitbucket/github in your profile section.
- Use git bash (no cygwin, Msys2, ... since the paths might be different)
ssh-keygen -t rsa -C "your_email@example.com"
- Add key on github/bitbucket/... (show key with
cat cat ~/.ssh/id_rsa.pub
)
A good start is this.
For rebasing an old version of a repository underneath a new version of another repository see here
You can use keywords (variants) to close issues with the following keywords:
close{, s,d}
fix{, s,d} // Leaves the issue open (only references the issue with the commit)
resolve{, s,d}
For example "Closes #35"
closes issue number 35.
For further information refer to the official website
-
extension:ini
(no space!) will search for files having the file extention.ini
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License *.
Code (snippets) are licensed under a MIT License *.
* Unless stated otherwise