Basic
Clone
Clone a repo.
git clone <remote_repo>
Clone a specific branch.
git clone -b <branch> <remote_repo>
Clone a repo over git
git clone git@<server>:<path>
git clone git@gitlab.com:YuriAlek/guides.git
Clone only the latest commit (shallow clone). Source
git clone --depth 1 <repo>
Add to git (so git tracks the file) a new file
git add file
git add -A // Add all files
git add . // Add all files in this directory
Undo add
.
git reset file
Commit changes with a message
git commit -m "Message to commit"
Change the commit message
git commit --amend
Show commits and commit id
git log
Revert (undo a previous commit in a new commit) to a previous commit
git revert <commit id>
Go back to the previous commit
git reset --soft HEAD^
Completely remove all staged and unstaged changes to tracked files.
git reset --hard [HEAD]
Remove files that are not tracked.
git clean [-f]
Push the changes to the remote server
git push
Push the changes from a specific branch (local) to a specific remote branch (remote)
git push local remote
// The most common one
git push origin master
// And to push another branch
git push testing-branch testing-branch
// To push all your branches
git push origin --all
Create a new branch. It will change to this branch.
git checkout -b new_branch
Show all the branches
git branch
Change back to the master
branch
git checkout master
Revert a file to it's original status
git checkout file.txt
Merge the branch hotfix
to master
// First go to the master branch
git checkout master
// Then merge the hotfix branch
git merge hotfix
Merge only certain files from branch to branch
// Change to the destination branch
git checkout master
// Merge files `file1` `file2` `dir/file3` from `source_branch` to `master`
git checkout source_branch file1 file2 dir/file3
Remove
// Remove remote branch
git push --delete <remote_name> <branch_name>
git push --delete origin branch_to_remove
// Remove locally
git branch -d branch_to_remove
// Force
git branch -D branch_to_remove
Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:User/repo.git
git push -u origin --all
git push -u origin --tags
Pull new data and merge: git pull
Pull new data from the remote: git fetch
Merge the data with no history: git rebase
Merge the data keeping the history: git merge
Diff
Show your current changes
git diff origin/master
git diff origin/master path/to/file
Using an external app like meld
. It will ask for every file.
git difftool -t meld origin/master
Conflicts when Merging
Configure git.
git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
Execute the tool when there is a conflict.
git mergetool
Tips.
LOCAL – this is file from the current branch
BASE – common ancestor, how file looked before both changes
REMOTE – file you are merging into your branch
MERGED – merge result, this is what gets saved in the repo
control + w + {h,j,k,l} to move
:diffg RE " get from REMOTE
:diffg BA " get from BASE
:diffg LO " get from LOCAL
:wqa " quit
Updates were rejected because the remote contains work that you do not have locally
git pull --rebase
git add .
git commit -m ""
git push
Delete commits
Show all the commits.
git log --pretty=oneline --abbrev-commit
---
da3b811 (HEAD -> master, origin/master) 2020-08-15 test7
f1394fd 2020-08-15 test6
1b59278 2020-08-15 test5
c89ad76 2020-08-15 test4
b73224f 2020-08-15 test3
8799208 2020-08-15 test2
34013a0 2020-08-15 test
469f570 2020-08-15
6a72547 2020-08-01
db3b637 2020-07-18
08838aa 2020-07-12
Go to the commit that you want. Substitute pick
for drop
.
git rebase -i 469f570
---
drop 34013a0 2020-08-15 test
drop 8799208 2020-08-15 test2
drop b73224f 2020-08-15 test3
drop c89ad76 2020-08-15 test4
drop 1b59278 2020-08-15 test5
drop f1394fd 2020-08-15 test6
drop da3b811 2020-08-15 test7
Check the new log.
git log --pretty=oneline --abbrev-commit
---
469f570 (HEAD -> master) 2020-08-15
6a72547 2020-08-01
db3b637 2020-07-18
08838aa 2020-07-12
Push the changes.
git push origin master
---
To server:git/lists
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'server:git/lists'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Force it.
git push --force origin master
Works but other people may have SERIOUS problems to access the repo now.
Credentials storage
Types of storage
- Plain text inside the repo
git config credential.helper store
- Temporal storage
git config credential.helper cache
- Temporal storage with timeout
git config credential.helper 'cache --timeout=3600'
- Use GCM. Uses Windows's own Credential Manager. Can be configured with multiple credential stores
git config credential.helper manager
Configure storage
List current values
git config --global --list
git config --system --list
git config --local --list
git config --worktree --list
Remove current values
git config --system --unset credential.helper
git config --global --unset credential.helper
Change the Credential Store
git credential-manager store
protocol=https
host=gitlab.com
username=yourusernamegoeshere
password=yourpasswordgoeshere
Delete a stored password
git credential-manager erase
protocol=https
host=gitlab.com