Skip to content

Instantly share code, notes, and snippets.

@febritecno
Last active June 30, 2023 13:07
Show Gist options
  • Save febritecno/1d845912abbc40417290161c12e55442 to your computer and use it in GitHub Desktop.
Save febritecno/1d845912abbc40417290161c12e55442 to your computer and use it in GitHub Desktop.
catatan belajar git

https://orga.cat/posts/most-useful-git-commands http://guides.beanstalkapp.com/version-control/common-git-commands.html

bahasa indonesia https://www.petanikode.com/tutorial/git/

Perintah lebih aman *********************

git fetch daripada git pull

git rebase daripada git merge

git revert daripada git reset

git show untuk melihat perubahan git branch untuk lihat branch

*NERAPIN FITUR KE BRANCH LAIN DG MUDAH DENGAN GIT STASH

menggunakan git stash sangat portable. cara kerja seperti commit tapi bisa tempel" ke branch lain. atau bisa di panggil kalau dibutuhkan

perintahnya

Git stash save Git stash list Git stash apply Git stash pop Git stash show Git stash branch Git stash clear Git stash drop

git checkout stash@{0} -- // cara cepet apply stash

*save untrack file

git stash save -u or git stash save --include-untracked

Penggunaan:

  1. tambah kode di branch 1
  2. git stash save "fitur nama"
  3. pindah branch lain untuk tempel kode stash tadi. pindah dg checkout ke branch 2
  4. git stash list untuk lihat daftarnya
  5. git stash pop stash@{0} pilih stashnya / git stash apply stash@{0} (lebih aman apply karna file tidak di merge/hilang. kalau pop automatis ngedelete sebelumnya)
  6. git stash show untuk lihat perbedaan perubahan
  7. git stash drop [stash] untuk delete stash | kalau git stash clear untuk delete semua stash -_-

MENGEMBALIKAN FILE -------------

git clean -nfdi == untuk menghapus file Untracked alias seblum di git add . / mengembalikan

-n verbose -f folder -d directory -i pilihan

untracked itu file setelah di ubah dan belum di git add . cek git status untuk cek

pindah ke kondisi commit yg dulu dan membuat branch untuk commit itu,

tidak hanya untuk pindah branch saja checkout ini. bisa jadi mesin waktu

git checkout -b nama_cabang <nomer_commit> atau git checkout 06f735af7724558164c87f6b1ce3ca7778eb1c1b

git reset bahaya karna bisa menghapus riwayat commit

--soft akan mengebalikan dengan kondisi file dalam keadaan staged --mixed akan mengebalikan dengan kondisi file dalam keadaan modified --hard akan mengebalikan dengan kondisi file dalam keadaan commited

git reset --soft 06f735af7724558164c87f6b1ce3ca7778eb1c1b

git revert lebih aman karna tidak menghapus riwayat commit

----------------------------------------END

git pull == fetch + merge

gunakan git margetool == untuk lihat secara visual

git reflog dan git reset , git revert untuk mengembalikan ke commit sebelum

git diff commit1 commit2 atau branch1 branch2 == untuk cek perbedaan

git push --set-upstream origin git checkout -b --track / git branch --set-upstream-to=origin/ git branch -vv git status

git rebase -i HEAD~N git rebase -i ~1

git reset --soft HEAD~1

git rebase -p --onto SHA^ SHA http://sethrobertson.github.io/GitFixUm/fixup.html

rebase memindahkan

git rebase -i HEAD~2 // rebase dg milih history commit dari list git reflog Here '2' is the number of commits you want to rebase.

git rebase -i HEAD if you want to rebase all the commits.

Then you will be able to choose one of these options.

p, pick = use commit

r, reword = use commit, but edit the commit message

e, edit = use commit, but stop for amending

s, squash = use commit, but meld into previous commit

f, fixup = like "squash", but discard this commit's log message

x, exec = run command (the rest of the line) using shell

d, drop = remove commit

reset berdasarkan commit history dari git reflog, git reset --hard HEAD~1

git reset --hard // dari git log git push origin HEAD --force // memaksa semua

git branch -d -r == delete branch local dan online repo

git rebase == untuk meletakan/copy/memindah bukan menimpa/merge. cara letakan status anda di posisi checkout di branch ingin di isi trus pilih branch yg isinya ingin di taruh situ.

cara hapus git repo dan file

git rm file1.txt git commit -m "remove file1.txt" But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

cara hapus hanya git repo saja

git rm --cached file1.txt git commit -m "remove file1.txt" And to push changes to remote repo

git push origin branch_name

cara clone project dengan memilih target branch pada repo

git clone -b [nama target branch remote] [link repository project].git [nama project]

cara copy tanpa rebase

git checkout dev -- path/to/your/file or path/to/your/folder

Normally you don’t need the there, and you can do simply git checkout branchname path/to/file. You only need the before the path in special cases, for example: 1. when path/to/file starts with a “-“, or 2. when you don’t specify a branch (to use the current branch), and you have a branch named “path/to/file”

fix error refuse merge saat pull atau fetch dari repository remote

git pull [remote] [branch target remote] --allow-unrelated-histories

reset sesuai head

show effect resetting

$ git reflog show [branch]

#reset with selecting HEAD

$ git reset 'HEAD@{1}'

bypass asking auth user/pass when git push

$ git config --global credential.helper wincred //for windows

$ git config --global credential.helper cache

menghapus remote branch

$ git push [remote_name] --delete [target branch_name remote]

shorthand

$ git push [remote_name] :[target branch_name remote]

menghapus file untrack/local belum di commit

show

git clean -n

clean with directory

git clean -f -d -x

To remove directories, run git clean -f -d or git clean -fd To remove ignored files, run git clean -f -X or git clean -fX To remove ignored and non-ignored files, run git clean -f -x or git clean -fx

caching your username/pass

$ git config --global credential.helper cache //Set git to use the credential memory cache

git config --global credential.helper 'cache --timeout=3600' //Set the cache to timeout after 1 hour (setting is in seconds)

bermain git flow untuk memudahkan buat fitur

####Initialize a Repository for git-flow

git flow init -d

(Omit -d if you want to select values other than the defaults.)

####Features Start a New Feature This creates a new branch based on develop and switches to it:

git flow feature start FEATURENAME

####Finish a Feature This merges the feature into develop, removes the feature branch, and switches to develop:

git flow feature finish FEATURENAME

####Publish a Feature Push a feature branch to remote repository:

git flow feature publish FEATURENAME

Get a feature published by another user from remote repository:

git flow feature pull origin FEATURENAME

####Releases Start a Release

Create release branch from develop:

git flow release start RELEASENAME

Publish release branch:

git flow release publish RELEASENAME

Create a local tracking branch for a remote release:

git flow release track RELEASENAME

####Finish a Release Merge release branch into master, tag it, merge back into develop, and remove the release branch:

git flow release finish RELEASENAME git push --tags

####Hotfixes

Start a Hotfix

Create hotfix branch from master:

git flow hotfix start VERSIONNAME

Create hotfix branch from some other commit:

git flow hotfix start VERSIONNAME BASENAME

####Finish a Hotfix Merge hotfix back into develop and master, and tag:

git flow hotfix finish VERSIONNAME

Useful Git Commands

You can also read the Portuguese version.

About it

Have you recently started using Git? This should give you the base commands you need to perform the most common actions in Git. If you find a command that is not here, or could be explained better, please don't hesitate in * Contributing. Cheers!

Table of contents

Git

Git is a distributed version control system, very easy to learn and supper fast!

Install Git

There are a few different ways to install git (from source or for Linux) but the purpose of this page is to focus on git commands, so I am going to assume you are installing git on a Mac.

To view other ways of installing it visit the Git official site

Click here to download and install Git

generate ssh keygen
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

trus buka ~/.ssh/id_rsa masukan kodenya pada repo remote anda. supaya bisa push ke repo remote

Setting up git
$ git config --global user.name "User Name"

$ git config --global user.email "email"
Applying colour to git
$ git config --global color.ui true
Initializing a repository in an existing directory

If you’re starting to track an existing project in Git, you need to go to the project’s directory and type:

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet.

To start version-controlling existing files you should start by tracking those files and do an initial commit. To accomplish that you should start with a few $ git add that specifies the files you want to track followed by a commit.

$ git add <file>
$ git add README
$ git commit -m 'Initial project version'

Checking the status of your files

The main tool you use to determine which files are in which state is the $ git status command. If you run this command directly after a clone, you should see something like this:

$ git status
# On branch master
nothing to commit (working directory clean)

If you add a new file to your project, and the file didn't exist before, when you run a $ git status you should see your untracked file like this:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)

Staging files

After initializing a git repository in the chosen directory, all files will now be tracked. Any changes made to any file will be shown after a $ git status as changes not staged for commit.

To stage changes for commit you need to add the file(s) - or in other words, stage file(s).

# Adding a file
$ git add filename

# Adding all files
$ git add -A

# Adding all files changes in a directory
$ git add .

# Choosing what changes to add (this will got through all your changes and you can 'Y' or 'N' the changes)
$ git add -p

Stashing files

Git stash is a very useful command, where git will 'hide' the changes on a dirty directory - but no worries you can re-apply them later. The command will save your local changes away and revert the working directory to match the HEAD commit.

# Stash local changes
$ git stash

# Stash local changes with a custom message
$ git stash save "this is your custom message"

# Re-apply the changes you saved in your latest stash
$ git stash apply

# Re-apply the changes you saved in a given stash number
$ git stash apply stash@{stash_number}

# Drops any stash by its number
$ git stash drop stash@{0}

# Apply the stash and then immediately drop it from your stack
$ git stash pop

# 'Release' a particular stash from your list of stashes
$ git stash pop stash@{stash_number}

# List all stashes
$ git stash list

# Show the latest stash changes
$ git stash show

# See diff details of a given stash number
$ git diff stash@{0}

Committing files

After adding/staging a file, the next step is to commit staged file(s)

# Commit staged file(s)
$ git commit -m 'commit message'

# Add file and commit
$ git commit filename -m 'commit message'

# Add file and commit staged file
$ git commit -am 'insert commit message'

# Amending a commit
$ git commit --amend 'new commit message' or no message to maintain previous message

# Squashing commits together
$ git rebase -i
This will give you an interface on your core editor:
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

# Squashing commits together using reset --soft
$ git reset --soft HEAD~number_of_commits
$ git commit
** WARNING: this will require force pushing commits, which is OK if this is on a branch before you push to master or create a Pull Request.

Branching and merging

# Creating a local branch
$ git checkout -b branchname

# Switching between 2 branches (in fact, this would work on terminal as well to switch between 2 directories - $ cd -)
$ git checkout -

# Pushing local branch to remote
$ git push -u origin branchname

# Deleting a local branch - this won't let you delete a branch that hasn't been merged yet
$ git branch -d branchname

# Deleting a local branch - this WILL delete a branch even if it hasn't been merged yet!
$ git branch -D branchname

# Remove any remote refs you have locally that have been removed from your remote (you can substitute <origin> to any remote branch)
$ git remote prune origin

# Viewing all branches, including local and remote branches
$ git branch -a

# Viewing all branches that have been merged into your current branch, including local and remote
$ git branch -a --merged

# Viewing all branches that haven't been merged into your current branch, including local and remote
$ git branch -a --no-merged

# Viewing local branches
$ git branch

# Viewing remote branches
$ git branch -r

# Rebase master branch into local branch
$ git rebase origin/master

# Pushing local branch after rebasing master into local branch
$ git push origin +branchname

Fetching and checking out remote branches

# This will fetch all the remote branches for you.
$ git fetch origin

# With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:
$ git checkout -b test origin/test

# Deleting a remote branch
$ git branch -rd origin/branchname
$ git push origin --delete branchname  or  $ git push origin:branchname

Merging branch to trunk/master

# First checkout trunk/master
$ git checkout trunk/master

# Now merge branch to trunk/master
$ git merge branchname

# To cancel a merge
$ git merge --abort

Updating a local repository with changes from a Github repository

$ git pull origin master

Tracking existing branch

$ git branch --set-upstream-to=origin/foo foo

Resetting

show effect resetting

$ git reflog show [branch]

#reset with selecting HEAD

$ git reset 'HEAD@{1}'



# Mixes your head with a give sha
# This lets you do things like split a commit
$ git reset --mixed [sha]

# Upstream master
$ git reset HEAD origin/master -- filename

# The version from the most recent commit
$ git reset HEAD -- filename

# The version before the most recent commit
$ git reset HEAD^ -- filename

# Move head to specific commit
$ git reset --hard sha

# Reset the staging area and t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment