General git notes, git concepts, git commands reference, etc
Last Updated: August 01, 2018 by Pepe Sandoval
If you find the information in this page useful and want to show your support, you can make a donation
Use PayPal
This will help me create more stuff and fix the existent content...
Disclaimer
git
git
is a version control program that can be described as a a set of layers and each layer has a function. The function is dependent on the layer and all you do with git
is you move files between these layers (layers are all stored inside the .git
folder). In simple words it's a way of storing snapshots
You can fully operate with only local repository but is common to have a remote repository that is hosted by a service (GitHub, Bitbucket, etc)
Download and install git for windows. Download link Click on Downloads for Windows button to get the executable (.exe file) and run it. Recommended Installation options:
One of the first thing you NEED to do after you install git is to configure your username and email.
git config --global user.name "FirstName LastName"
git config --global user.email example@email.com
git config --list
.bashrc
, modify PS1
variable:export PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$(__git_ps1 '(%s)')\$ "
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$(__git_ps1 '(%s)')\$ "
git config --global core.autocrlf input
git config --global diff.submodule log
to avoid adding --submodule
to git diff
If you do not configure the editor whenever the editor is needed git by default will use vim. To configure editor:
git config --global core.editor "'C:\Program Files (x86)\Notepad++\notepad++.exe'"
To configure the diff and merge tools you will need to install a differential tool like p4merge and give the path to where it was installed so git can launch it
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path C:/Program\ Files/Perforce/p4merge.exe
git config --global --add mergetool.prompt false
git config --global diff.tool p4merge
git config --global difftool.p4merge.path C:/Program\ Files/Perforce/p4merge.exe
git config --global --add difftool.prompt false
The .gitignore file is the file that contains files and folders that will be ignored by Git
On the folder of the repo create a file called .gitignore
touch .gitignore
Edit the .gitignore file and add any rule to ignore. For example:
*.pdf
*.doc
*.temp
folderInRepo/folder
...
stage the .gitignore file, make commit and push it as any normal change
git add .gitignore
git commit -m "message" .gitignore
git push origin <name_of_branch>
Cloning/clone in git means copying a git repo to the system where you will be working. In others word it creates a local copy of the repo that is stored on the git server (external server). Examples
git clone <url-to-repo>
git clone https://github.com/phacility/arcanist.git
git clone git@bitbucket.org:jose1791/jms-sandbox.git
git status
: command that gives you the current status of your LOCAL repo this means if you have changes in a file or if you have commits that you have not already push to the git server, etc..Fetching and pulling are used for the same purpose which is updating your local copy of the repo. You use to to get the latest changes that are stored on the repo stored on the git server
git fetch
git pull
On git you stage files which means you are preparing them for a commit
git add <path_to_file>
git reset HEAD <path_to_file>
When you already did a commit a forget some changes or forget to add a file, made a mistake on the commit message etc. you can amend
your commit
If you forget to add a file, stage it and then use amend
git add <forgotten_file>
git commit --amend -C HEAD
If you want to change the commit message or add extra changes to a file that its already part of the commit, stage the file and use one of the following commands both will include your changes on the commit
git commit --amend -m "New message here"
: Also updates commit messagegit commit --amend
: Edit commit message on your default editorIf you want to change the author of a commit that you haven't pushed
git commit --amend --author="New Author Name <email@address.com>"
git remote set-url origin
command:
- git remote set-url origin https://<user>@gitlab.developtools.com/<path_to_repo>/<repo_name>.git
- git remote set-url origin ssh://gitlab.developtools.com/<path_to_repo>/<repo_name>.git
This will stage all the tracked files that are modified and commit them using the provided message, this will not add untracked files
git commit -am "Commit Message here"
On git when you push it sends your changes to the remote server. Its used to "publish" your changes so other people can see them. Once you do this your changes will be visible to all the people who have access to the repo
git push origin <name_of_branch>
git push origin master
git push origin my_development_branch
git log
: command that gives you a list of the latest commit you have in your LOCAL repo. Usually used to know in which commit you are. Hit q
to exit log viewgit log -p
: show git log and all the file changes of each commit changesgit log --stat --summary
: show commits log info with summary of changes of each modified filegit log -p -- <path/to/file>
: history of changes in filegit log --stat --summary <path/to/file>
: check who last modified a filegit grep <regexp> $(git rev-list --all)
: To search for commit content (actual lines of source)You do this when you have a branch and want to integrate/update the changes that are on another branch to yours
git checkout <branch_source>
git pull
git checkout <branch_destination>
git pull
Make sure you are on the branch that needs to integrate changes from another branch
git checkout <branch_destination>
Do the merge, on the merge command use the name of the branch that will be integrated
git merge origin/<branch_source>
<branch> | MERGING
)git mergetool
git merge --abort
git fetch origin
or git fetch
git merge origin/<name_of_branch>
merge your changesgit rebase origin/<name_of_branch>
to put your job on top<branch> | MERGING
)git merge --abort
git branch <my_branch_name_here>
git checkout <my_branch_name_here>
git add <path_to_files_here>
git commit –m "message here"
git push --set-upstream origin <my_branch_name_here>
Alternatively we can manually create the branch on the git server first and then push to it, use:
git branch --set-upstream-to=origin/<my_branch_name_here> <my_branch_name_here>
orgit branch --set-upstream <my_branch_name_here> origin/<my_branch_name_here>
thengit push origin <my_branch_name_here>
git checkout -b <branch_name>
to create the new branch and change to it and then commit and push on that branch normally you will see that your changes are still there but you will be on your new branchTo delete a branch locally and on the git server do:
git branch -D <my_branch_name_here>
git push origin : <my_branch_name_here>
Show all branches information
git branch -vr
Show local branches information
git branch -vv
Create branch and checkout to it
git checkout -b <branch-name>
If we ever make mistakes or the changes we did on our files didn't work out or if for any other reason we want to undo changes, return to a previous commit, return the repo to a previous state/status we can use git reset and git checkout which basically are used when we are in the following situation:
"Damn it, Morty! Looks like we screwed everything up, WE GOTTA GO BACK!"
Checking out changes the current status of your repo to be in a detached temporary branch.
Used to undo local changes that have NOT been committed NOR staged
git checkout <path_to_file>
Use to quickly go back to a previous commit,
git checkout <commit-sha-id>
Reset returns the tracked files of your repo to exactly how they were and the status of your repo is changed as if you were on that commit and haven't update your repo
Use to go back to the state of a certain number of commits (n
)
git reset -–hard HEAD~<n>
Use to go back to the state of a certain commit
git reset -–hard <commit-sha-id>
show the current differences
git diff --cached
git diff <branch>:<path_to_folder> <branch>:<path_to_folder>
If new files are not tracked or not show in git diff you can stage them and use the --cached
option
git add <files_for_diff>
git diff --cached
Diff commits
git diff <prev_commit> <more_recent_commit>
Diff branches
git diff <branch_1> <branch_2>
git diff master dev_branch
Git diff not tracked files
git diff --no-index <path_file_1> <path_file_2>
git diff --color-words --no-index <path_file_1> <path_file_2>
To create a patch you just need to redirect the output of the diff command to a file and use the --patch
option. For example
git diff --patch > <path_to_patch_here>/<name_of_patch>.diff
By default the diff does not include the lines above and below the change to include the full context the --no-prefix -U1000
option needs to be added. For Example:
git diff --patch --no-prefix -U1000 > <path_to_patch_here>/<name_of_patch>.diff
git diff --patch --no-prefix -U1000 master <dev_branch> > <path_to_patch_here>/<name_of_patch>.diff
To apply a patch we usually use the options to ignore white spaces and spaces differences
git apply --ignore-space-change --ignore-whitespace <path_to_patch_here>/<name_of_patch>.diff
The --reject
option is used to apply only the changes that can be applied without conflicts and creates a file with the conflicts found
git apply --ignore-space-change --ignore-whitespace --reject <path_to_patch_here>/<name_of_patch>.diff
This is used to make a little "stash" of file(s); like a bundle of changes that you want to save. You don't want to commit but also you don't want to lose them
git stash
git stash list
git stash apply
To clean a repo means remove/delete files that are not tracked by git
git clean -df
To revert the changes made by a commit that is either just local or has already been pushed to a remote branch
git revert <commit-sha-id>
If there are no conflicts with other commits, this command will automatically create a new commit that reverts the changes made by the commit passed as parameter so you can push this in case you want to revert the changes that are already in the remote branch. If there are conflicts git will enter into reverting state (<branch> | REVERTING
) so you'll have to resolve the conflicts and and create a commit you can use git mergetool
command like in Merge Conflicts [scenario]
Show tags in a branch
git tag -n -l
git tag -l --columns
git show-ref --tags -d
grep
works well with these commands
Creating tags
git tag -a <tag-name-here> -m "<tag message here>"
git tag -a v1.0 -m "Project Version 1.0"
git tag -a <tag-name-here> <sha-commit-here>
git tag <tag-name-here>
Pushing tags
git push origin <tag-name-here>
git push origin v1.0
By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them
This process is used only when you want to create a new repo and NOT work from an already created one, in that case you only need to clone a repo since somebody else already did this
Create a folder with the name you want to give your repo and add files to it
Change to the created directory and execute git init
Create a project on a git host service like Bitbucket to get the origin url and configure it using the git remote add origin <url>
command
Add files to your repo, do a commit and do a push all
mkdir my-repo
cd my-repo
git init
git remote add origin https://jose1791@bitbucket.org/jose1791/my-repo.git
echo "my-repo readme" > readme.md
git add readme.md
git commit -m "My first commit"
git push -u origin --all
git submodule update --init --recursive
git submodule add <git_repo_url>
.gitmodules
file and a local subdirectory are created of the submodule:.gitmodules
stores the mapping between the project’s URLsgit commit -m "Added new module"
git push origin master
git clone <main_repo_url>
git submodule init
git submodule update
To pull the latest from git submodules: git submodule update --remote
or git submodule update --remote <submodule_repo_name>
to update only a specific submodule
git config submodule.<submodule_repo_name>.branch <submodule_branch_name_here>
.gitmodules
): git config -f .gitmodules submodule.<submodule_repo_name>.branch <submodule_branch_name_here>
~/.ssh/config
file (you may need to create one if it doesn't exists): HostkeyAlgorithms +ssh-dss
chmod 600 ~/.ssh/config
git review
/ git-review
Extra command integrated as part of a tool to perform reviews, installation can varu depending of the tool used for reviews like gerrit, confluence, gitlab, etc.
git review -s
git review -R
git review -d <Change-Id>
If you find the information in this page useful and want to show your support, you can make a donation
Use PayPal
This will help me create more stuff and fix the existent content...