- Deleting a repository
Git - more like a mini filesystem - thinks about its data more like a stream of snapshots, unlike other delta-based version control systems (Subversion.)
Unlike CVCS, nearly every operation in Git is local.
Git has integrity. Everything is checksummed. It uses SHA-1 to store everything in it’s DB.
Git generally only adds data.
Git has three stages: modified, staged, and committed. And it has three main sections:
- The working tree: a single checkout of one version of the project. Files are pulled out of the compressed DB in the Git directory and placed on the disk for modifications.
- The staging area: - aka index - a file that stores info about what will go into the next commit.
- The Git directory: - .git - stores the metadata and object DB.
git commit --amend:
1 2 3
git commit -m "Initial commit" git add forgotten_file git commit --amend
Then we’ll have one single commit.
When we want to ignore certain files or folders, create a .gitignore file and add the file/folder name inside:
1 2 3
file.txt # a single file folder/ # a folder named `folder` *.txt # all txt files
If a file is already tracked, use
git rm --cached foo.txtto remove it first from the git repo.
- When we want to get a simpler version of the log, use
git log --pretty=oneline.
Compare the diff between two branches:
1 2 3
git diff branch_1..branch_2 # Or if already in branch_1: git diff ..branch_2
Compare the commits between two branches:
1 2 3
git log master..branch-X # Or if already in branch-X: git log master..
git checkout .to revert to the last commit before adding the new changes.
git checkoutplus the first 6 characters of the reference ID to check out the old commits. This enters the detached HEAD state. And it’s best not to make changes when checking out old commits. Use
git checkout masterto go back the master branch.
git reset --hardplus the first 6 character of the reference ID to reset the project to the old commit.
git reset HEAD README.mdto unstage the file.
git checkout -- README.mdto discard the changes for the file.
git checkout ffffff -- foo.txt bar.txtreverts the two files back to commit ffffff.
git remote -vto see all the remotes verbosely with URLs.
originis the default name.
git push <remote> <branch>:
git push origin master.
git remote show <remote>to see details of a remote.
git remote rename <old_remote> <new_remote>and
git remote remove <remote>.
git tag -a v1.0 -m "my first tag".
git tagto see the tags
git show v1.0to show the details.
git tag v1.0-lw(just provide a tag name only). It’s a commit checksum - no other info is kept.
Add tags for previous commits:
git tag -a v1.0 <log_number>.
git pushdoesn’t push tags to the remote servers. We need
git push origin <tag_name>or
git push origin --tagsfor all tags.
git tag -d <tag_name>and
git push origin --delete <tag_name>for deletion.
git branch <branch_name>creates a new branch.
git checkout <branch_name>moves
HEADto the branch. The shorter version is
git checkout -b <branch_name>to do both at the same time.
git log --oneline --decorateshows the branch pointers.
git log --oneline --decorate --graph --allshows the divergence.
Branches are cheap since they are essentially a file that has the 40-character checksum of the commit pointed to.
git checkout master+
git merge hotfixwill fast-forward (or use the recursive strategy) the master branch to the match the hotfix branch. Then
git branch -d hotfixdeletes it.
In case of merge conflict, we need to choose one side or merge the contents ourselves. Then add and commit the file again.
git branch -vshows the last commit of each branch.
git branch --mergedand
git branch --no-mergedshows the branched that are already merged to the current branch or not yet respectively. Or
git branch --no-merged master.
git checkout experiment+
git rebase master+
git checkout master+
git merge experimentgives a fast-forward merge.
git stashis a convenience tool. But since branching is cheap, we can always branch and then delete it.
git branch -ato see all the branches.
git checkout -b test remotes/origin/testcreates a local
testbranch and connects it with the remote branch.
Deleting a repository
- We can either delete the .git directory in a file browser or use
rm -rf .git.
git submodule foreach git pull