Skip to content

Instantly share code, notes, and snippets.

@ahmedhamdy2121
Last active November 25, 2022 11:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ahmedhamdy2121/c7dcf012bbd16b4371e8 to your computer and use it in GitHub Desktop.
Save ahmedhamdy2121/c7dcf012bbd16b4371e8 to your computer and use it in GitHub Desktop.
GIT simple commands
Copyrights (c) 2014 - 2019 Ahmed Hamdy
To pull before push:
> git fetch
> git stash # NOTE: This will backup your local changes
> git pull # git pull origin master
> git stash apply # NOTE: This will restore your local changes
If the remote branch has many changes, or have been deleted and recreated:
> git pull --rebase
To go to a specific commit:
> git checkout 254ccfe
OR
> git checkout HEAD~5
To return to master
> git checkout master
To go to a branch
> git fetch
> git branch
> git checkout <branch name>
To see commit Log:
> git log
OR
> git log --pretty="fuller"
OR
> git log HEAD..origin/master
OR
> git log --graph --oneline --decorate --all
To search in working tree:
> git grep "something"
To know the current branch
> git branch
Remove your changes:
> git reset --hard HEAD
Remove only the unstagged files:
> git checkout -- .
Reset a single file:
> git checkout <file-name>
Edit the commit message (before pushing):
> git commit --amend
Stage the commit (undo the commit):
> git reset --soft HEAD~1
Undo the pushed commit with a new commit:
Locally commit a new commit.
Squash the new and the old one.
> git push -f
Undo the pushed commit:
> git revert <commit_hash>
undo "git add .":
> git reset .
To preserve your changes until fixing a sync problem
> git stash
Then after fixing
> git stash apply
If you made a commit locally and you found a new commit on the origin:
> git fetch
> git status
> git pull --rebase
this will pull the changes from the origin, then rebase your local commits on toop of it
If there are conflicts:
solve them then:
> git rebase --continue
or to abort the pull
> git rebase --abort
If there are local uncommitted changes:
> git fetch
> git status
> git stash
> git pull --rebase
> git stash apply
If I have many commits:
> git rebase -i HEAD~2
I have 2 commits I want to squash
nano will be opened with a list of commits
leave the first one as it is
replace pick with s for all the remaining commits
save and exit
nano will be opened again with list of commits messeges
remove whatever messege you don't want and leave only one (read the instructions)
save and exit
> git status
> git log
> git push
To list all the stashed items:
> git stash list
To preview the stash code without applying it
> git stash show -p
To stash a specific item n:
> git stash apply n
To delete a remote commit:
> git rebase -i HEAD~2
nano will be opened with a list of commits
leave the first one as it is
remove the second commit (the wrong one)
save and exit
> git push -f origin branch-name
==========================================================
List all branches:
> git branch -a
How to create a branch:
> git checkout -b <branch name>
This will keep all your changes.
Switch to a branch:
> git checkout <branch-name>
Push the local branch for the first time:
> git push -u origin branch-name
Working with many branches and having edited files:
commit your changes locally first
go to the master
create a new branch from the master
Sync your branch with another branch:
fetch and check the log of the other branch
go to the new branch then:
> git rebase the-other-branch
this will keep your commits, you might want to do stash first if there is any uncommitted files.
check the log of the new branch
Rename a branch:
> git branch -m new-name
How to delete a branch (locally only):
> git branch -d <branch name>
that way, it won't show in your git fetch output
use -D to force delete
Delete a branch remotely:
> git push origin --delete <branch_name>
you may need to copy and paste the branch name if it is not found.
To merge two branches (locally):
switch to the branch that you want to merg into
> git checkout to-branch
merge all changes from <from-branch> into <to-branch>
- to get the latest commits if you needed:
> git checkout from-branch
> git pull
> git checkout to-branch
> git merge from-branch
OR
> git merge origin/from-branch
then if there is a conflict, resolve it then:
> git add .
> git commit -a
To get the diff between the two merged braches:
> git diff HEAD~1
Merge selected commit:
Go to the to-branch
> git cherry-pick <commit hash>
Where <commit-hash> is unique among branches
==========================================================
Your branch has diverged, and have 1 and 1 different commits each, respectively:
> git pull --rebase
==========================================================
To ignore some files locally only:
> add the files to .git/info/exclude
> if the files already modified, update the index
git update-index --assume-unchanged files-list
==========================================================
Check this link too: https://github.com/joshnh/Git-Commands
Play with commits: https://git-scm.com/docs/git-cherry-pick
git pull vs git pull --rebase: https://stackoverflow.com/questions/18930527/difference-between-git-pull-and-git-pull-rebase
Merge vs Rebase: https://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase/16666418#16666418
Merge two branches using smart git: (squash all commits before rebasing)
Solution 1:
- checkout the from-branch then to-branch
- select from-branch, right click on to-branch and rebase
- select to-branch, right click on from-branch and rebase
Solution 2:
- checkout the from-branch then to-branch
- select the commits you want from the from-branch and cherrypick them
==========================================================
If the local branch is not linked to the local branch:
- git branch --set-upstream-to=origin/<branch-name>
+++++++++++++++++++++++++++++++++++ The remaining of the document is old ++++++++++++++++++++++++++++++++++
How to commit:
1) > git status
2) > git add <file1> <file2> ... (to remove files use rm instead of add)
OR
> git rm <file1> # hint: use -r for deleted folders
3) > git commit -m "write comment here"
4) > git push # origin <branch name, default: master>
==========================================================
How to update:
1) > git pull origin <branch name, default: master> (i.e. origin testing) where testing is the branch name
2) Resolve conflicts then use
> git add <file name>
3) And then commit them and then push them on the server
==========================================================
How to update a branch from the master:
1) Switch to the branch
> git fetch
> git branch
> git checkout <branch name>
2) > git pull origin master
==========================================================
How to revert a change:
1) > git checkout -- db/schema.rb
OR revert all
1) > git checkout -- .
OR reset to master
1) > git reset --hard origin/master
==========================================================
How to pull an already code on the repo:
1) navigate to the folder you want to checkout in
2) the site will give you a command, use it
> git clone <remote_repo>
==========================================================
How to pull a specific commit from a repo:
1) navigate to the folder you want to checkout in
2) init the folder
> git init
3) clone the repo with -n (no checkout done after cloning)
> git remote add origin <url>
4) fetch the specific commit you want
> git fetch origin <sha1>
==========================================================
How to create a repo:
1) create the repo on the site (i.e. bitbucket)
2) from it, it will provide you with commands
3) from terminal, navigate to the project folder
4) use command
> git init .
5) now use the command the site gave to you
> git remote add origin <remote_repo>
6) do these commands
# Note: prepare the .gitignore first!
> git add .gitignore Gemfile Gemfile.lock README.rdoc Rakefile app/ bin/ config.ru config/ db/ log/ lib/ public/ test/ vendor/
> git commit -am "all"
> git push --set-upstream origin master
OR
> git push -u origin --all
==========================================================
How to creat a local project for a branch:
1) make a new directory
> mkdir <branch name>
> cd <branch name>
2) initialize it (you may see a warining message that local copy repo already on this device)
> git init
3) clone the code from the remote repo
> git remote add -t <branch name> -f origin <remote_repo>
4) start to work with that branch
> git checkout <branch name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment