Guide to Use Git: Version Control Made Simple

In the world of software development, version control is an essential aspect of managing codebases, collaborating with teams, and ensuring the integrity of projects. Git, developed by Linus Torvalds in 2005, has become the de facto standard for version control systems due to its speed, flexibility, and distributed nature. This article aims to provide a comprehensive guide to Git, covering fundamental concepts, common workflows, and advanced features.

Git Cheat Sheet

Edit git configuration 

Run command bellow to set your account’s default identity.

git config --global user.email "[email protected]"

git config --global user.name "Your Name"

Omit –global option, to set the identity only in this repository.

Reference: https://help.github.com/articles/setting-your-commit-email-address-in-git/

Git check remote url

git remote show origin

Reference: https://help.github.com/en/articles/renaming-a-remote

Remove or stash all modified data

Undo edited files

git reset --hard

git reset

git rm --cached filename.txt

Reference: https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/

Stash or Save modified files

git stash 

git stash list

git stash clear

git stash drop stash@{1}

git stash apply stash@{0}

Reference: https://medium.freecodecamp.org/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a

Git stash with message

git stash save <message>

git stash -u

git stash --include-untracked

Reference: https://dev.to/neshaz/how-to-git-stash-your-work-the-correct-way-cna

Get differential with branch

git diff --name-status master

git diff --name-status firstbranch..yourBranchName

git diff --stat --color master..branchName

git diff --stat --color branchName..master

git diff --stat --color master..development

git diff --stat --color production..development

Reference: https://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-two-revisions

Merging git

git checkout ba 

git checkout -b ba-merge

git merge master 

.... review new code and fix conflicts.... 

git commit 

git checkout ba 

git merge ba-merge 

git branch -d ba-merge

git push origin --delete login

git merge master

## Menambahkan remote repository kalau belum pernah

git remote add origin [email protected]:doavers/example-code.git

https://help.github.com/articles/adding-a-remote/

## Ubah remote repository URL

https://help.github.com/articles/changing-a-remote-s-url/

git remote -v

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

## Delete remote repo

git remote rm bitbucket

## Menggunakan git tag untuk release version

https://dzone.com/articles/git-tags-version-control-made-easy

https://git-scm.com/book/en/v2/Git-Basics-Tagging

git tag -a v0.1.0 -m "Releasing version v0.1.0"

git push origin v0.1.0

## Git pull error => fatal: refusing to merge unrelated histories

git pull origin branchname --allow-unrelated-histories

git pull origin master --allow-unrelated-histories

## Melihat log

git log --pretty=oneline

## Adding SSH key to git

https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

Harus buka git bash

clip < ~/.ssh/id_rsa.pub

## Get all remote brances

https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches

git fetch --all

git pull --all

git pull -u origin --all

## Git ignone not working

https://stackoverflow.com/questions/11451535/gitignore-is-not-working

git rm -r --cached . 

git add . 

git commit -m "fixed untracked files"

## Git fatal: The current branch master has no upstream branch

git push -u origin master 

git push -u origin --all

Git delete last commit push

git reset --hard <sha1-commit-id>

git push origin HEAD --force

## Updates were rejected because the tip of your current branch is behind

## Updates were rejected because a pushed branch tip is behind its remote

# rebase in progress; onto bc631e1

You are currently rebasing branch 'dev' on 'bc631e1'

Step 1: Keep going git rebase --continue

Step 2: fix CONFLICTS then git add .

Back to step 1, now if it says no changes .. then run git rebase --skip then go back to step 1

If you just want to quit rebase run git rebase --abort

Once all changes are done run git commit -m "rebase complete" and you are done.

Other examples

Remove branch

Deleting local branches in Git

git branch -d feature/login

Deleting remote branches in Git

git push origin --delete feature/login

Reference: https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-remotely

You May Also Like

More From Author

+ There are no comments

Add yours