Git Tips
- Basics
- .gitignore
- Log
- Compare
- Reverting
- Remotes
- Tags
- Branching
- Deleting a repository
- Submodules
- References
Basics
-
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 3git commit -m "Initial commit" git add forgotten_file git commit --amendThen we’ll have one single commit.
.gitignore
-
When we want to ignore certain files or folders, create a .gitignore file and add the file/folder name inside:
1 2 3file.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.
Log
- When we want to get a simpler version of the log, use
git log --pretty=oneline.
Compare
-
Compare the diff between two branches:
1 2 3git diff branch_1..branch_2 # Or if already in branch_1: git diff ..branch_2 -
Compare the commits between two branches:
1 2 3git log master..branch-X # Or if already in branch-X: git log master..
Reverting
-
Use
git checkout .to revert to the last commit before adding the new changes. -
Use
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. Usegit checkout masterto go back the master branch. -
Use
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.
Remotes
-
git remote -vto see all the remotes verbosely with URLs.originis the default name. -
git pull=git fetch+git merge. -
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>andgit remote remove <remote>.
Tags
-
Annotated tags:
git tag -a v1.0 -m "my first tag".git tagto see the tags -
git show v1.0to show the details. -
Lightweight tags:
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 needgit push origin <tag_name>orgit push origin --tagsfor all tags. -
git tag -d <tag_name>andgit push origin --delete <tag_name>for deletion.
Branching
-
git branch <branch_name>creates a new branch.git checkout <branch_name>movesHEADto the branch. The shorter version isgit 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. Thengit 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 --mergedandgit branch --no-mergedshows the branched that are already merged to the current branch or not yet respectively. Orgit 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 fetchandgit checkout -b test remotes/origin/testcreates a localtestbranch 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.
Submodules
-
Update all:
1git submodule foreach git pull