Use Git
Initialisation
Clone a repository
# for http-based
git clone https://[email protected]/zephinzer/blog.joeir.net
# for ssh-based
git clone ssh://[email protected]/zephinzer/blog.joeir.net
Create a new repository
git init
Remote management
Add a Git remote
git remote add origin ssh://[email protected]/zephinzer/blog.joeir.net
Update the Git remote
git remote set-url origin ssh://[email protected]/zephinzer/blog.joeir.net
Retrieving changes
Fetch changes
Fetching retrieves the changes but does not merge the changes with your local copy.
git fetch
Merge changes
Merging takes the remote changes that have been fetched from the remote and merges them with your local copy.
git merge HEAD
Pull changes
Pulling basically does a fetch and merge.
git pull
Pull with Rebase
Pulling with rebase does a fetch, but before merging in the remote changes, it rolls back to a state before all remote changes were made, applies the remote changes, and then applies your local changes.
git pull -r
Rebase
# assuming we are on branch feature_x pulling in updates from master
git rebase master
Saving changes
Stashing
# put all unstaged changes into a stash
git stash
# checking stashed changes
git stash list
# popping the last stashed change
git stash pop
Staging
# to stage all changes, run this from project root
git add .
# to stage only one file
git add ./path/to/changed_file
Commiting
git commit -m 'some message'
Commiting without any changes
git commit --allow-empty 'some message'
Modifying changes
Adding a file to a previous commit
# stage the missing file first
git add ./path/to/missed/file;
# this will add the staged file to the previous commit
git commit --amend
Squash commit
# indicate `p` or `pick` for the head commit, and `s` or `squash` for the rest
git rebase -i HEAD~5
Squashing till origin/master/HEAD
git rebase -i HEAD~$(git log --oneline master..HEAD | wc -l);
Uncommit last commit
# this will leave the committed files as staged
git reset --soft HEAD^
# this will also unstage all changes
git reset HEAD^
Reverting a commit
# use `git log` to find the commit hash of the commit you wish to undo the effects of
git revert ${COMMIT_HASH}
Submitting changes
Pushing
git push
Force Pushing
git push -f
Assessing changes
View all current changes
git status
View commit history
# interactive browsing of git commits
git log
# output only the last 5 logs
git log -n 5
View difference between commits
# view file changes from HEAD to ${COMMIT_HASH}
git diff HEAD ${COMMIT_HASH}
Viewing repository information
View the Git configuration
git config -l
Check which branch you're on
git branch
See all remotes
Why
I would like to see which remotes I am pushing to
git remote -v
Checking which .gitignore
is ignoring a file
.gitignore
is ignoring a fileWhy
I would like to know which
.gitignore
is causing a file to be ignored without any obvious reason
cd `./path/to`;
git check-ignore -v *;
Repository adminstration
Creating a new branch from an existing one
git checkout $SOURCE_BRANCH_SLUG;
git checkout -b $NEW_BRANCH_SLUG;
Deleting a local branch
git branch -D $BRANCH_SLUG;
Deleting a remote branch
git push origin --delete $BRANCH_SLUG;
Deleting local remote branches that have been deleted remotely
git remote update origin --prune;
Last updated
Was this helpful?