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 3
git commit -m "Initial commit" git add forgotten_file git commit --amend
Then 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 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.txt
to 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 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..
Reverting
-
Use
git checkout .
to revert to the last commit before adding the new changes. -
Use
git checkout
plus 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 master
to go back the master branch. -
Use
git reset --hard
plus the first 6 character of the reference ID to reset the project to the old commit. -
git reset HEAD README.md
to unstage the file. -
git checkout -- README.md
to discard the changes for the file. -
git checkout ffffff -- foo.txt bar.txt
reverts the two files back to commit ffffff.
Remotes
-
git remote -v
to see all the remotes verbosely with URLs.origin
is 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 tag
to see the tags -
git show v1.0
to 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 push
doesn’t push tags to the remote servers. We needgit push origin <tag_name>
orgit push origin --tags
for 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>
movesHEAD
to the branch. The shorter version isgit checkout -b <branch_name>
to do both at the same time. -
git log --oneline --decorate
shows the branch pointers. -
git log --oneline --decorate --graph --all
shows 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 hotfix
will fast-forward (or use the recursive strategy) the master branch to the match the hotfix branch. Thengit branch -d hotfix
deletes 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 -v
shows the last commit of each branch.git branch --merged
andgit branch --no-merged
shows 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 experiment
gives a fast-forward merge. -
git stash
is a convenience tool. But since branching is cheap, we can always branch and then delete it. -
git branch -a
to see all the branches. -
git fetch
andgit checkout -b test remotes/origin/test
creates a localtest
branch 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:
1
git submodule foreach git pull