Git
Staging files
add
add
Adding files to stage (add
)
add
)Add all uncommited files for staging.
Add only selected files.
Adding and Removing multiple files to stage (add -i
)
add -i
)You can iteractively remove or add from stage as.
Then you can use options:
3
: To revert (remove) files from the staging area.4
: To add files to the staging area.
To select changes in a file, and commit them separatly (add -p
)
add -p
)This will make you select hunk
s (Chunks) of the code that will be commited.
reset
reset
Removing files from stage (reset
)
reset
)To remove a file from the stage area.
To remove all from the stage area.
diff
diff
To see what is changed in the staging area (diff --staged
)
diff --staged
)Commiting
commit
commit
Create commit (commit
)
commit
)commit -S
commit -S
-S
will tell Git to make the commit using the configured signinkey
. (GPG signatures)
commit --amend
commit --amend
Fix the last created commit message.
This has to be un-pushed commits.
This will update the LAST commit.
reset
reset
Fixing several un-pushed commits (reset
)
reset
)Must be on un-pushed commits. (Already pushed commits must be fixed with `?`)
The reset
can accept:
A
commit ID
as target.A syntax of
HEAD~2
, specifing that must go back 2 commits before the HEAD.
--hard
reset
--hard
resetWhich will move the HEAD
to the specified commit AND revert all the changes done in the code.
Useful when you dislike what you did and want to do a ctrl-z
.
Commits reseted with --hard
are only marked for deletion, so you can revert back if needed.
The changes done WILL be deleted when git runs it's garbage collector, which it does from time to time or manually, Garbage Collector.
--soft
reset
--soft
resetWill only move the HEAD
to the specified commit. (This means that will only UNDO the erase the commits, so you can commit them again)
rebase
rebase
Fixing several un-pushed commits (rebase
)
rebase
)Must be on un-pushed commits. (Already pushed commits must be fixed with `?`)
An official way of fixing multiple done commits is with rebase
.
-i
will open a window for you to tell rebase what to do with the commits.
pick
Use commit.
reword
Use commit, but edit the commit message.
edit
Use commit, but stop for amending.
squash
Use commit, but meld into previous commit.
fixup
Like squash
, but discard this commit's log message.
exec
Run command (the rest of the line) using shell.
drop
Remove commit.
Tip: For this you can leave the first commit on the list as pick
and the others as squash
.
Logging
log
log
Show commit logs from the current branch (log
)
log
)Show logs formatted in a one line
Show logs with GPG signatures
Show only last n
logs
n
logsBranches
branch
branch
Creating branches with (branch
)
branch
)Deleting branches with (branch -D
)
branch -D
)checkout
checkout
Create branches and checkout to them with (checkout -b
)
checkout -b
)Tagging
tag
tag
There are two types of tags lightweight
and annotated
.
lightweight
tags are just a simple tag name pointing to a commit.
annotated
tags are stored as full objects in Git, containing a tagger name, email, date and a message.
By deafult git push
doesn't transfer tags to remote servers.
Explicitly push a specific tag with
git push origin <tag-name>
.Explicitly push all tags with
git push origin --tags
.
Create annotated tags with (tag
)
tag
)Create lightweight
tags with (tag
)
lightweight
tags with (tag
)Will create a tag pointing to the last commit.
Create tags to specific commits with (tag
)
tag
)Delete tags
Delete tags from local repository with:
However this will not remove it from the remote server, to do it:
Save Uncommited Work
stash
stash
Stash work in a branch with (stash
)
stash
)Must be done on staged
files.
To unstash the work with (stash pop
)
stash pop
)To list the WIP (Work in Progress) stashes
diff
diff
Save work done to a .patch
file with (diff
)
.patch
file with (diff
)Must be done on staged
files.
Apply .patch
file to working branch with (apply
)
.patch
file to working branch with (apply
)Garbage Collector
Git runs it's garbage collector from time to time or manually with:
The garbage collector will delete changes done that were deleted with reset --hard
for instance.
Rewriting git commit history (BFG Repo-Cleaner
)
BFG Repo-Cleaner
)The commits sha1
which are the commits ID, use the commits files to be created. This means that if you re-write a commit without a file or with a file changed, it's sha1
will change.
The git history structure is a graph, where each commit knows where it belongs by knowing the previous commit's sha1
. So if you change one of the commits, you will change it's sha1
and break all the git history.
To do this safely without breaking everything, you can use bfg
, that will allow you to change a commit and then re-write all of the structure, maintaining all the graph.
Installing bfg
Dealing with large files (lfs
)
lfs
)Files that are not text
are interpreted by git as binary
and these cannot have their deltas checked and done. This means that their changes cannot be tracked, so when commited, these files are copied in its entirety and attached to the commit.
So the more changes are made in this files and commited, the more number of files you will have in the project, and this will raise considerably the size of the project.
To deal with this use lfs
lfs
When using lfs
the file itself does not go in the commit, instead only a reference to the file goes to the commit.
So when pulls
are executed or when the project is cloned, only the last version of these big files are downloaded, and not all the files versions.
On linux install it with:
Then on the project root you initialize it with:
Then tell git what types of files to use it on lfs
.
Last updated