Git tips

Useful Git commands and options.

Git

git branch -a show current branch and show all branches, -а key means all
git branch newbranch – create new branch with name called”newbranch”
git branch -f master / – move HEAD of “master” branch to commit with hash or to the HEAD of branch called , if there is no master branch, then create it
git branch -d – delete branch, or with -D key if the branch has commits that are not merged to another branches

git checkout newbranch – switch to “newbranch” branch
git checkout -b newbranch – create new branch called “newbranch” and switch to it
git checkout -b master / – create new branch called ‘master” with HEAD pointing to commit ot to the HEAD of branch and switch to it
git checkout -B master / – move HEAD of “master” branch to commit or to the HEAD of branch and switch to it
git checkout / – copy file located at to the current working directory from HEAD of branch or from commit
git checkout HEAD или git checkout – move file from repository(if HEAD is specified) or from index( if HEAD is not specified) to the working directory, for example if there is need to cancel unsaved changes after last commit
git checkout @{-N} – switch back to N last switches between branches
git checkout -f – delete changes in working directory made after last commit, so that reset changes to the current HEAD.
git checkout –conflict=diff3 –merge index.html – in case of a conflict during merge, show all index.html file changes from both merged branched, as well as changes of the file common to the both branches
git config –global merge.conflictStyle diff3 – globally set output of conflicting changes, as per previous command

git push origin newbranch – push “newbranch” branch to the remote repository
git branch -d newbranch – delete “newbranch” branch from the local repository
git push origin –delete newbranch – delete “newbranch” branch from the remote repository

git reset HEAD-1 (–hard) – switch HEAD back to 1 commit, same as – git reset @~
git reset –hard – restore state of all files(including files changed in working directory and not added to the index) to the version of last commit(HEAD)
git reset –merge – restore state of the files(excluding files changed in working directory and not added to the index) to the version of last commit(HEAD)

git merge –abort – cancel merge, so that restore state of the files(excluding files changed in working directory and not added to the index) to the version of last commit(HEAD). Same as git reset –merge
git merge –continue – continue the merge after resolving conflicts, same as running “git commit” after resolving conflicts.
git merge –no-ff branchname – run merge using “merge commit” instead of “fast-forward”. E.g. when you need to save history of branching. This mode could be set up as default in config: git config merge.ff false, same but for specified branch: git config branch.branchname.mergeoptions ‘–no-ff’
git merge -ff branchname – run merge using “fast-forward”(default mode)
git merge –squash branchname – merge the changes from all commits of “branchname” branch to the current working directory(history of the current branch will not have commits from the “branchname” branch but only one collecting commit). Note: using the –squash option will make –abort и –continue options unavailable

git commit –amend -m “Commit name” – rename last commit
git cherry-pick – copy specified commit or commits from any branch by the commit hash
git cherry-pick branchname1..branchname2 – copy commits from “branchname2” branch, which do not exist in branchname1
git cherry-pick has the same options as git merge: ‘–continue’ и ‘–abort’, and also a ‘–quite’ option, all those options could be sued during resolving the conflicts of cherry-pick.
git cherry-pick –no-commit|-n – copy specified commit or commits from any branch by the commit hash only to the working directory and to the index, but do not commit them.
git add -p – select which changes of the file should be added to the index
git commit -a -m “Commit title” – combination of git add -A и git commit -m commands. Note: “-a” key will add only indexed files, new files will be ignored
git commit -m “Commit title” – commit only selected files
git rm -r – delete folder in the filesystem and also add this change to the index, same as ‘rm -r ‘ + ‘git add
git mv – rename in the working directory and add to the index
git rm –cached delete file from the index, but keep in the working directory
git stash – stash not commited changes in the working directory, so that making working directory clean
git stash pop – apply changes delayed after running git stash command

git log –oneline – show commits history by showing each commit in one line
Example of formatting the “git log” output:
git log –pretty=format:’%h %cd %s %d %an’
– %h – short commit hash
– %cd – date
– %s – commit title
– %d – commit references
– %an – author name
git log –oneline –all –graph – show commits history by showing each commit in one line and graphical branching
git log branch1..branch2 – show commits history of “branch1” starting from its branching from the “branch2” branch
git log branch1…branch2 – show commits history of the “branch1” branch and “branch2” branch starting from their branching(except their common commits)
git log –grep someword – show commits with “someword” string in title(case sensitive by default). String could be replaced by regular expression.
git log -Gsomeword – show commits with “someword” string in changes(case sensitive by default). String could be replaced by regular expression.
git log -L ‘//’,’/<\/head>/’:index.html – show history of changes between lines with and text in the index.html file.
git log branchname –first-parent – show only commits of ‘branchname’ branch excluding foreign commits(commits which could get to the ‘branchname’ branch after merge)

git blame index.html -L 5,10 – show details of date, commit and changes author of file index.html in lines 5 through 10

git reflog – show history of operations which cause references changes: commits, switching between branches etc. Same as ‘git log –oneline -g’

git show :/someword – search commits with ‘someword’ string in title

git clean -dfx – delete all untracked files and directories, including those from .gitgnore. “-d” – remove folders, “x” – remove files from .gitgnore as well

git diff – show difference between working directory and index(changes that were added by “git add”, but not commited)
git diff HEAD – show difference between working directory and repository(commited changes)
git diff –cached – show difference between(changes that were added by “git add”, but not commited) and repository(commited changes)
git diff – show differences between specified commits
git diff – ‘…’ show differences between commit and the commit where the branch with commit branched off the branch with commit
git diff –name-only – show only file names(without changes).
git diff –no-index – show current differences between files, even if the files are not tracked by Git
git diff HEAD^1 и git diff HEAD^2 – show differences of the current state(after merge) from the first and second parents respectively. Unlike ‘~’ the ‘^’ operator is used mostly with merge commits

git rebase -x ‘some command’ branchname – ‘-x’ option lets specifying a command to run by Git during rebase, e.g. useful to run the tests before “rebase” applies a commit.
git rebase –rebase-merges branchname – run rebase including merge commits(by default rebase skips merge commits)
git rebase -i branchname – run rebase in interactive mode, allows to choose the commits, remane them, change order, etc

Notes:
– “git diff” command does not include untracked files
– reflog keeps log entries for 90 days by default, or for 30 days, if a branch was deleted. reflog is not being passed to the server, exists only locally.
– For a full cleanup of the working directory from unsaved changes there is need to delete changes in tracked files(e.g. using “git reset…”), as well as in untracked files(e.g using
“git clean…”)
– ‘–‘ parameter specifies to Git that following string will be a path, and not a branch name. For example: git diff — master – specifies that “master” is a file name, not a branch name.
– ‘..’ and ‘…’ operators could have different meaning in different Git commands.

Useful resources:
Git documentation
Free Git course from freeCodeCamp.org

Напишите комментарий

Your email address will not be published. Required fields are marked *