0% found this document useful (0 votes)
4 views

git_cheat_sheet

Uploaded by

su lihong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

git_cheat_sheet

Uploaded by

su lihong
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Adding and removing files for staged and commit locations

gi t c ommi t gi t c ommi t - m " My f i x "


- - c heat - s heet
.git directory
/ repository Commit staged changes and Commit staged changes and
open the editor to specify the log specify the log message on the
c ommi t t ed
message describing the changes. same command line.

Staging area The -a option to the git commit command makes Git automatically stage gi t c hec k out <f i l es >
/ index every file that is already tracked before doing the commit. gi t r es et - - har d HEAD <f i l es > gi t c hec k out - - <f i l es > gi t r m <f i l e> gi t r m - f <f i l e> git rm --cached <file> gi t mv <f i l eA> <f i l eB>
/ cache
Remove staged and working directory Checkout the project / unmodify a modified rm <file> remove staged changes remove staged changes mv <fileA> <fileB>
s t aged gi t add <f i l es > gi t r es et HEAD <f i l es > rm <file>
changes. file: actually overwrite files from the git add <file> untrack the file git rm <fileA>
working directory by the ones in the staged git add <file> keep file in local directory git add <fileB>
Stage a file. Unstage a staged file.
Working location or by the ones from latest commit
directory from the specified branch.

modi f i ed

Git configuration Tagging Vocabulary

gi t c onf i g - - l i s t git tag <tag string identifier> git tag Unt r ac k ed Unmodi f i ed Modi f i ed St aged
gi t c onf i g - - s y s t em /etc/gitconfig
Some keys may appear more than once, because reading in all Create a lightweight tag just a pointer to the last commit ID of the List all tags of the current branch.
configuration files. current branch. Add t he f i l e
More specific
.gitconfig
gi t c onf i g - - gl obal
config gi t c onf i g - - gl obal us er . name " J ohn Doe" git tag -a <tag string identifier> [<commit id>] git tag -l <search pattern> Edi t t he f i l e
or
supersedes/ St age t he f i l e
~/.config/git/config gi t c onf i g - - gl obal us er . emai l j ohndoe@ex ampl e. c om git tag -a <tag string identifier> -m "Tagging log message" [<commit id>] Search for a tag matching the Remov e t he f i l e
overrides
higher levels search pattern (wildcard ~regex
gi t c onf i g - - gl obal al i as . <s hor t c ut > <gi t c ommand> Create a tag (a commit) with a pointer to the last commit ID of the Commi t t he f i l e
supported).
gi t c onf i g ./.git/config current branch and a message log. Has a dedicated SHA-1 ID.
gi t c onf i g - - gl obal al i as . <s hor t c ut > ' ! <c ommand>'

Cloning and pushing

gi t c l one <gi t r epo ur l > gi t pus h gi t r emot e - v gi t r emot e add <s hor t name> <r emot e ur l >

gi t c l one <gi t r epo ur l > <di r ec t or y t o c l one t o> Push the default branch master (the default one from List the shortnames of each remote handle you?ve specified. -v Add a new remote explicitely (contrary to the implicit "or i gi n"
remote.* .push) to the default remote repo called origin. adds the remote URL (pull and push) next to it. made by the "gi t c l one" command) and reference to it as the
Implicitly create a default remote repository shortname called "or i gi n", specified shortname.
download the default branch content and name that branch "master ". gi t pus h <r emot e r epo name> <l oc al br anc h> gi t r emot e s how <r emot e r epo s hor t name>
gi t r emot e r m <s hor t name>
gi t pul l As local branchs are not pushed by default, push the local Show the branches the remote repository has, which one we
branch to the remote location specified. track and which ones are concerned (tracked) when doing a pull Remove the specified remote either because the server has
If the current branch is tracking a remote branch, automatically fetch and If we do not have write access or if someone has pushed data in or a push. Untracked (aka "new") branches will be downloaded changed, the mirror is not used anymore or no one uses that
merge that remote branch into the current branch. Tags are pulled too. the meanwhile, the push is rejected and we will have to f et c h with "gi t f et c h <r emot e>". mirror any more.
and mer ge first to continue.
gi t f et c h <r emot e r epo s hor t name> gi t r emot e r ename <s hor t name> <new s hor t name>

Download all the changes made on the remote shortname location, i.e. all gi t pus h <r emot e r epo name> <t ag number > Change the remote shortname to the new one you specified.
objects (and thus commit history) are downloaded, all remote pointers will be Remote tracked branches are renamed in the process.
updated (r ef s / r emot es / * ). It doesn't automatically merge the changes with gi t pus h <r emot e r epo name> - - t ags
files from working directory nor does it modify these files.
Tags are not pushed by default. Push the tag/all tags manually.

Branching Differences and logging changes

gi t br anc h <new br anc h name> gi t c hec k out <br anc h name> $ gi t s t at us - s


gi t c hec k out - gi t mer ge <br anc h name> gi t l og
M README
MM Rak ef i l e
Create a new branch of the specified name. Move the HEAD pointer to the specified branch name/last branch name and Merge the specified branch to the current branch. List in reverse chronological order (paging the result), the ID, A l i b/ gi t . r b
changes working directory files to the last snapchot/commit of the specified If the specified branch is ahead of the current one, the merge will be author, date and message of commits made in the repository. M l i b/ s i mpl egi t . r b
gi t br anc h ?? LI CENSE. t x t
gi t br anc h - v branch. Changing branch is rejected if not all changes to files in working "Fast-forward": the pointer is moved forward.
directory have been committed (staged is still uncommitted). That rejection is If there is a conflict, the merge is paused. The user needs to fix it manually by gi t l og - p - 2
gi t br anc h - - mer ged
gi t br anc h - - no- mer ged New files that aren?t tracked have a "??" next to them.
only true if both branches do not point to the same location (i.e. no commit removing lines and commit the changes manually. Same but showing the difference introduced in each commit New files that have been added to the staging area have an "A".
List all branches to one branch or the other). with a limit to only the last two entries. Modified files have an "M".
<<<<<<< HEAD: <f i l ename>
List all branches with last commit (short hash + log message) <c ont ent f r om br anc h we ar e mer gi ng i nt o> States are displayed according two columns.
List all branches that have been merged (branches safe to remove) gi t c hec k out - b <br anc h name> ======= gi t l og - - s i nc e=2. week s - - unt i l =2016- 08- 10
The left-hand column indicates the status of the staging area
List all branches that have not been merged <c ont ent of br anc h we ar e mer gi ng f r om>
Create a new branch of the specified name and switch to it. >>>>>>> <br anc h name we ar e mer gi ng f r om>: <f i l ename> To specify limits in the commits research. Lots of time formats The right-hand column indicates the status of the working tree.
The * prefix indicates the branch that HEAD points to.
and research patterns available.
In this output, the "README" file is modified in the working directory but not yet
gi t br anc h - d <br anc h name> gi t c hec k out - b <br anc h name> <br anc h name on r emot e> gi t mer get ool gi t l og - - <f i l e/ di r ec t or y > staged, while the "l i b/ s i mpl egi t . r b" file is modified and staged. The
"Rak ef i l e" was modified, staged and then modified again, so there are
Remove specified branch if it has been merged. Create a new branch of the specified name, download the branch content Use an external tool like vimdiff to help resolve merge conflicts. User answers Display the commits having modified the specified files or
changes to it that are both staged and unstaged.
locally and switch to it. This command is useful as f et c h only gets pointers Y/N if conflict have been solved and then commit changes manually. directories.
gi t br anc h - D <br anc h name> to new remote-tracking branches but does not take a local copy of them.
gi t l og - S" s omec ode t o s ear c h f or "
.git directory
Remove specified branch and remove its content even if it has not been
Search for commits where changes to the specified code string / repository
merged. gi t di f f - - s t aged
were introduced. gi t di f f t ool - - s t aged
c ommi t t ed
gi t s how <c ommi t i d>: f i l e
Staging area
Show the file content like it was at the commit ID. Redirect the / index
output to a temporary file with ">" if you want to see a finary file. / cache
s t aged
gi t di f f
gi t di f f t ool

Caption: Working
directory
The red cross means typing the command may involve data loss.
modi f i ed

This Git cheat sheet has been made by William Gathoye (@wget42) and is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-nc-sa/4.0/).
Schemas and texts are inspired from the book progit2 (http://progit.org/) written by Scott Chacon (@chacon) and Ben Straub (@benstraub) and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License (http://creativecommons.org/licenses/by-nc-sa/3.0/).
The Git logo has been made by Jason Long (@jasonlong) and is licensed under the Creative Commons Attribution 3.0 Unported License (http://creativecommons.org/licenses/by/3.0/). The colorscheme of this sheet is inspired from that Git logo. v . 2016- 08- 15 12: 20

You might also like