Created by: Simón Ovidio Miranda Chiri /ovidio.miranda@jalasoft.com
“Git is a distributed version-control system for tracking changes in source code during software development.”
git --version
git version 2.32.0.windows.1
git config --global user.name "Your Name"
git config --global user.email example@gmail.com
Editor
git config --global core.editor vim
To verify these are set:
git config --global -l
git config --global -e
cd C:my_project
Initialize Git.
git init
vim first.txt
With information.
Method A
Method B
Method C
git add first.txt
Add all the modified files of your project.
git add .
git add --all
git add -A
git add *second.txt
Add changes by hunk.
git add --path
git add -p
git add --interactive
git add -i
git status
Give the output in the short-format.
git status --short
git status -s
git commit -m "My first commit in this repo"
Show information about remote.
git remote show origin
git branch develop
Switch to a different branch.
git checkout develop
To create a new branch and switch to that branch.
git checkout -b feature-X
git checkout develop
Then merge branches.
git merge feature-X
git merge origin/feature-X
git checkout -b feature-X
git checkout -b feature-X
git checkout develop
git merge feature-X
git branch -d feature-01
Force delete branch.
git branch -D feature-01
git branch -m feature-2
Rename another branch.
git branch -m feature-2 feature-02
git branch
List remote branches.
git branch -r
List all branches.
git branch -a
Change the message of the last commit.
git commit --amend -m "Update commit message"
Add changes to the last commit without changing its commit message.
git add new_file
git commit --amend --no-edit
Git aliases are a powerful tool that create shortcuts to frequently used Git commands.
git config --global alias.st status
git st
Ignore changes in tracked files with Git.
To return to normal.
Using aliases.
git config --global alias.unwatch 'update-index --assume-unchanged'
git config --global alias.watch 'update-index --no-assume-unchanged'
git unwatch first.txt
git watch first.txt
See the changes in the working directory.
git diff
Show name of files changed.
git diff --name-only
Show all files in conflict.
git diff --name-only --diff-filter=U
Git has the ability to tag specific points in a repository’s history as being important.
Creating a tag.
git tag -a v2.0 -m "My version 2.0"
git tag -a v1.0 3643c10 -m "My version 1.0"
Push the tags to the remote repository.
git push origin --tags
See local tags.
git tag
See the tag data along with the commit that was tagged.
git show v1.0
Checking out Tags.
git checkout v1.0
git checkout -b version2 v2.0.0
Deleting a tag.
git tag -d v1.0
git clean -fd
Delete a remote branch.
Remove the branches that have already been merged in develop.
git branch --merged develop | grep -v '^\*' | xargs -n 1 git branch -d
Cleanup unnecessary files and optimize the local repository.
git gc
git log --oneline -5
Pretty git log
git log --oneline -5 --pretty="%C(Yellow)%h %C(reset)%ad (%C(Green)%cr%C(reset))%x09 %C(Cyan)%an: %C(reset)%s" --date=short
Show the branches
git log --graph
git checkout 789abcd
To roll back to a previous commit while keeping the changes(without pushing).
git reset --soft HEAD^
To permanently discard any changes made in the last commit.
git reset --hard HEAD^
Revert some existing commitst(especially when those commits have been pushed to a remote
repository.)
git revert HEAD^
git reflog -5 --date=format:'%Y-%m-%d %H:%M:%S' --format='%C(auto)%h - %an%x08 - %C(green)%ad -%C(blue)%cr -%C(reset) %gs (%s)'
You're back where you started
git reset --hard 2be3846
git cherry-pick 2be3846
Quickly switch to the previous branch.
git checkout -
git stash
To apply our changes
git stash apply
If you want to recover and delete the stash.
git stash pop
git stash save -u "Add Main page"
Show list of stashes.
git stash list
stash@{0}: On feature-page: Add Main page
To recover the changes you use the number of stash {0}.
git stash apply 0
git stash drop 0
project-name/.git/hooks
vim .git/hooks/pre-push
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi
rm .git/hooks/pre-push
vim .git/hooks/pre-push
#!/bin/bash
current_branch=$(git rev-parse --abbrev-ref HEAD)
target_branch='develop'
git_ahead_and_behind=$(git rev-list --left-right --count origin/$target_branch...$current_branch | awk '{print ""$1"-"$2""}')
behind=$(echo $git_ahead_and_behind | cut -f1 -d-)
if [ $behind -gt 0 ];
then
echo -e "\e[31mAlert:\e[0m The '$current_branch' branch does not have the latest changes from the '$target_branch' branch."
echo "The source branch is '$behind' commits behind the target branch".
fi
vim .git/hooks/pre-push
#!/bin/bash
read -p "Do you want to execute the Javadoc task? [y|n] " -n 1 -r < /dev/tty
echo
if echo $REPLY | grep -E '^[Yy]$' > /dev/null; then
echo "** Executing the Javadoc task **"
gradle javadoc
echo "** Finished the Javadoc task **"
else
echo "** The javadoc task was not executed **"
fi
vim .git/hooks/pre-push
#!/bin/bash
# Section 1.
current_branch=$(git rev-parse --abbrev-ref HEAD)
target_branch='develop'
git_ahead_and_behind=$(git rev-list --left-right --count origin/$target_branch...$current_branch | awk '{print ""$1"-"$2""}')
behind=$(echo $git_ahead_and_behind | cut -f1 -d-)
if [ $behind -gt 0 ];
then
echo -e "\e[31mAlert:\e[0m The '$current_branch' branch does not have the latest changes from the '$target_branch' branch."
echo -e "The source branch is '$behind' commits behind the target branch\n"
fi
# Section 2.
read -p "Do you want to execute the Javadoc task? [y|n] " -n 1 -r < /dev/tty;
if echo $REPLY | grep -E '^[Yy]$' > /dev/null
then
echo -e "\n** Executing the Javadoc task **"
gradle javadoc
echo -e "\n** Finished the Javadoc task **"
else
echo -e "\n\e[31m** The javadoc task was not executed **\e[0m"
fi
git help add
git add -h