Basic
Clone
Clone a repo.
Clone a specific branch.
Clone a repo over git
Clone only the latest commit (shallow clone). Source
Add to git (so git tracks the file) a new file
Undo add
.
Commit changes with a message
Change the commit message
Show commits and commit id
Revert (undo a previous commit in a new commit) to a previous commit
Go back to the previous commit
Completely remove all staged and unstaged changes to tracked files.
Remove files that are not tracked.
Push the changes to the remote server
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.
Show all the branches
Change back to the master
branch
Revert a file to it's original status
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
Using an external app like meld
. It will ask for every file.
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.
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
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.
Works but other people may have SERIOUS problems to access the repo now.
Credentials storage
Types of storage
- Plain text inside the repo
- Temporal storage
- Temporal storage with timeout
- Use GCM. Uses Windows's own Credential Manager. Can be configured with multiple credential stores
Configure storage
List current values
git config --global --list
git config --system --list
git config --local --list
git config --worktree --list
Remove current values
Add a credential
git credential-manager store
protocol=https
host=gitlab.com
username=yourusernamegoeshere
password=yourpasswordgoeshere
Get a credential
Delete a stored password