mrfdn.com
- Git merupakan aplikasi version control yang hampir digunakan oleh semua developer.
Git dapat membantu kita memonitor source kode yang sedang dibuat. Kita bisa melakukan roollback terhadap perubahan yang sudah pernah dilakukan kemudian melakukan sejumlah modifikasi.
Di git kita juga bisa melakukan kolaborasi dengan orang lain untuk mengerjakan satu atau lebih projek secara bersama-sama tanpa saling terganggu.
Git dicintai oleh semua developer karena semua kemudahan yang dimilikinya sangat memudahkan pengembangan aplikasi.
Mari kita mulai
Create a Repository
- Create a new local repository
- Clone an existing repository
git clone -b [branch] [url]
- Clone a specific branch and create a new directory
git clone -b [branch] [url] [new-directory]
Configuration
- Set the name that will be attached to your commits and tags
git config --global user.name "[name]"
- Set the email address that will be attached to your commits and tags
git config --global user.email "[email address]"
- Edit the global configuration file in a text editor
git config --global --edit
- Set a default branch name other than master
git config --global init.defaultBranch [branch-name]
- Enable some colorization of Git output
git config --global color.ui auto
- Set Git to use the credential memory cache
git config --global credential.helper cache
- Set Git to use credential memory cache for 1 hour
git config --global credential.helper 'cache --timeout=3600'
Make a change
- Show modified files in working directory
- Stages the file, ready for commit to HEAD
- Stage all changed files, ready for commit to HEAD
- Stages all modified and deleted files, ready for commit to HEAD
- Commit all local changes in tracked files
- Commit previously staged changes
git commit -m "[descriptive message]"
- Commit all local changes in tracked files
git commit -a -m "[descriptive message]"
- Unstages file, but preserve its contents
- Discard all local changes in your working directory
- Diff of what is changed but not staged
- Diff of what is staged but not yet commited
- Diff of what is changed between two branches
git diff [first-branch]...[second-branch]
- Apply any commits of current branch a HEAD of specified one
- Discard all history and changes back to the specified commit
git reset --hard [commit]
Bekerja dengan Branch
- List all existing branches
- List all existing and remote branches
- List all branches, local and remote, in long format
- List all branches, local and remote, in long format with commit info
git branch -a -v --abbrev-commit
git checkout [branch-name]
- Create a new branch based on your current HEAD
- Delete the specified branch
git branch -d [branch-name]
- Delete the specified branch, even if it has not been merged
git branch -D [branch-name]
- Rename a branch and its reflog
git branch -m [old-branch] [new-branch]
git checkout [branch-name]
- Switch to the branch and create it if it doesn’t exist
- Create a new tracking branch based on a remote branch
git checkout --track [remote-branch]
- Switch to the branch, and discard all changes in working directory
git checkout -f [branch-name]
- Merge the specified branch’s history into the current one
- Tag the current commit with a version number
Remote Repositories
git remote add [shortname] [url]
- Show the names of the remote repositories you’ve added
- List all currently configured remote repositories
- Show information about a remote repository
- Remove a remote repository
git remote set-url --push [remote] [newUrl]
Synchronize
- Fetch all branches from remote repository
- Fetch all branches from remote repository and prune
- Merge a remote branch into your current branch to bring it up to date
git merge [remote]/[branch]
- Transfers commits, files, and refs from one remote to another
git push [remote] [branch]
- Fetch and merge any commits from the tracking remote branch
- Merge just one specific commit from another branch into your current branch
- Download all history from the repository
Tracking path Changes
- Show all commit logs with indication of any paths that moved
- Change an existing file path and stage the move
git mv [existing-path] [new-path]
- Delete a file from the working tree and stage the deletion
- Remove a file from the working tree and stage the removal
- Add a file as if it was deleted and then re-created
Temporary Commits
- Save modified and staged changes
- List stack-order of stashed file changes
- Write working from top of stash stack
- Discard the changes from top of stash stack
Ignoring Files
Ignoring Files adalah fitur git untuk menandai file / direktory mana yang tidak perlu dimasukken ke git sehingga tidak perlu dilihat oleh orang lain.
echo "file.txt" >> .gitignore
- Add a directory to .gitignore
echo "directory/" >> .gitignore
- Add a file extension to .gitignore
echo "*.extension" >> .gitignore
- Add a file pattern to .gitignore
echo "file*" >> .gitignore
- Add a file extension pattern to .gitignore
echo "*.ext*" >> .gitignore
- Add a exclude pattern to .gitignore
echo "!file.txt" >> .gitignore
- Add a exclude pattern to .gitignore
echo "!directory/" >> .gitignore
- Add a exclude pattern to .gitignore
echo "!*.extension" >> .gitignore
Git Tricks
Rename branch
- Rename your current branch
git branch -m old-name new-name
- Push the new branch and delete the old one
git push origin :old-name new-name
- Reset the upstream branch for the new name
git push origin -u new-name
Commit amend
- Amend the most recent commit
- Amend the most recent commit with a new message
git commit --amend -m "New commit message"
- Amend the most recent commit with the staged changes
git commit --amend --no-edit
- Amend the most recent commit with the staged changes and a new message
git commit --amend -m "New commit message" --no-edit
Log
- Show the commit history for the currently active branch
- Show the commit history for the currently active branch, including all commits from all branches and remotes
git log --all --graph --decorate --oneline
- Show the commit history for the currently active branch, including all commits from all branches and remotes, and the diffs between each commit
git log --all --graph --decorate --oneline --patch
- Show out visual representation of the commit history
git log --graph --oneline --decorate --all
- Search the commit history for the given author
git log --author="[name]"
- Search the commit history for the given commit message
git log --grep="[message]"
- Search the commit history for the given file
- Search the commit history for the given file, including diffs
- Search the change by connected with the given commit
Revert
- Revert a commit by creating a new commit
- Revert a commit by creating a new commit, and edit the commit message
Reset
- Reset the current HEAD to the specified state
- Reset the current HEAD to the specified state, and keep the changes in the working tree
git reset --keep [commit]
Git Aliases
git config --global alias.[alias] [command]
- Create a new alias with arguments
git config --global alias.[alias] "[command] [arguments]"
- Create a new alias with arguments and options
git config --global alias.[alias] "[command] [arguments] --[option]"