DEV Community

loading...
Student Edge

Common Git commands

prayatna profile image Prayatna Bhattarai ・2 min read

Few of the git command that we use at Student Edge:

Pulling from origin (note: master below can be replaced by any branch name)

git pull --rebase origin master

Switching to an existing branch

If you already have the branch locally you can just do

git checkout MyOtherBranch

if you don't have the branch locally you need to do the following

git fetch

git checkout -b MyOtherBranch remotes/origin/MyOtherBranch
Enter fullscreen mode Exit fullscreen mode

Creating a new branch

git checkout -b MyNewBranch

Pushing your branch to origin

git push origin MyBranch

Commit your code to your current branch.

git add --all :/

git commit -m 'WR-123 I changed something'
Enter fullscreen mode Exit fullscreen mode

Make a tweak to your previous commit

git commit --amend

After running the above it will open vim. Just type :wq and it will rebase and commit it

Remove a commit from your branch (note: the hats are how many commits to go back)

git rebase -i HEAD^^

After running this it will open vim with all the commits listed, the number of ^ will indicate how many commits it will show. Type i which will put it in edit mode. Delete the commit lines you want to remove. Then press Esc and then :rw to rebase.

Thanks to Dan, you can add this helper git file in the root of your user account folder (eg. C:\users\'myUser') and create your own git shortcuts (.gitconfig)

[user]name = Your user name
email = your@email.com

[alias]# Pretty text base branch tree
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

# When on master branch, force local master to be the same as origin
refreshmaster = checkout    -B master remotes/origin/master

# pretty diff of changes
diff = diff --word-diff=color

# After Fixing conflicts - OK to proceed with rebase
ok = rebase --continue

# Push the current branch upstream to it's namesake branch on origin
up = push origin HEAD

# Push the current branch upstream to it's namesake branch on origin with FORCE
upf = push -f origin HEAD

# Replace NEW stuff from master to my current branch
new = pull --rebase origin master

# Track all new changes
done = add --all :/

# Amend my last commit - request you to do "git add --all :/" or "done" first and then do `:wq` in vim to save and rebase
tweak = commit --amend

# Give a list of the last branches on my local git
lb = for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
changes = diff-tree --no-commit-id --name-only -r 

[push]
default = upstream
Enter fullscreen mode Exit fullscreen mode

Discussion

pic
Editor guide