Difference between revisions of "Git Cheat Sheet"

From Gejoreuy
Jump to navigation Jump to search
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
https://www.earthdatascience.org/workshops/intro-version-control-git/basic-git-commands/
+
'''List Branch'''
 +
$ git branch
  
 +
'''List Tag'''
 +
$ git tag
  
 
'''Create New Branch from Master or Other Branch'''
 
'''Create New Branch from Master or Other Branch'''
 +
$ git checkout master                        # first, we work in master branch
 
  $ git pull                                    # pull from original branch
 
  $ git pull                                    # pull from original branch
 
  $ git checkout -b [new_branch_name]          # creating new branch in our local fs
 
  $ git checkout -b [new_branch_name]          # creating new branch in our local fs
  $ git push origin [new_branch_name]          # push our new branch to Github or Gitlab
+
  $ git push origin [new_branch_name]          # push our new branch to remote repository
 
  $ git config core.autocrlf false              # stop replacement warning message
 
  $ git config core.autocrlf false              # stop replacement warning message
  
'''Delete Branch in Our Local Filesystem'''
+
'''Create New Tag from A Commit'''
 +
$ git tag [tag_name] [commit_id]              # create tag from a commit
 +
$ git tag                                    # list tag in our local repository
 +
$ git push origin [tag_name]                  # push our new tag to remote repository
 +
 
 +
'''Create New Tag from A Branch'''
 +
$ git checkout [branch_name]
 +
$ git pull
 +
$ git tag [tag_name]
 +
$ git push origin [tag_name]
 +
 
 +
 
 +
'''Delete Branch in Our Local'''
  
 
  $ git branch -d [name_of_branch]
 
  $ git branch -d [name_of_branch]
Line 16: Line 32:
 
  $ git branch -D [name_of_branch]              # force deletion
 
  $ git branch -D [name_of_branch]              # force deletion
  
'''Delete Branch in Github or Gitlab'''
+
'''Delete Branch in Remote'''
  
 +
$ git -d [name_of_branch]
 
  $ git push origin :[name_of_branch]
 
  $ git push origin :[name_of_branch]
 +
 +
'''Delete Tag in Our Local'''
 +
 +
$ git tag -d [name_of_branch]
 +
 +
or
 +
 +
$ git tag -D [name_of_branch]                # force deletion
 +
 +
'''Delete Tag in Remote'''
 +
 +
$ git -d [tag_name]
 +
$ git push origin :[tag_name]
 +
 +
'''Merge Branch to Master'''
 +
 +
$ git checkout master                        # switched to branch master
 +
$ git merge [branch_name]                    # merge
 +
 +
'''Rebase Master (As Source) to Feature (As Target)'''
 +
 +
$ git checkout master
 +
$ git pull
 +
$ git checkout [target_branch]
 +
$ git rebase master
 +
 +
'''Duplicate Branch to New Branch'''
 +
 +
$ git checkout [old_branch]
 +
$ git branch [new_branch]
 +
 +
or
 +
 +
$ git checkout -b [new_branch] [old_branch]
 +
 +
'''Reset Local Branch Same As Remote Branch '''
 +
 +
$ git checkout [name_of_branch]              # checkout to branch that will be reseted
 +
$ git reset --hard origin/[name_of_branch]  # reset to be same as remote branch
 +
$ git reset --hard origin                    # reset local master to be same with remote master
 +
 +
'''Reset Branch Same As Master or Other Branch'''
 +
 +
$ git checkout [name_of_branch]              # checkout to branch that will be reseted to be same as master
 +
$ git reset --hard master                    # reset to be same as master
 +
 +
'''Copy File in A working Git Branch from Another Branch'''
 +
 +
$ git checkout [name_of_branch]              # checkout to branch where a file need to be placed
 +
$ git checkout [source_branch] path/to/file  # source branch where the file will come from
 +
 +
'''Overwrite File in A Working Git Branch from Another Branch'''
 +
 +
$ git checkout [name_of_branch]              # checkout to branch where a file need to be replaced or overwrited
 +
$ git checkout [source_branch] path/to/file  # source branch where the file will come from
 +
 +
'''Cherry Pick Commit(s) from Another Branch'''
 +
 +
$ git checkout [name_of_branch]              # make sure we are on the branch we want to apply the commit to.
 +
$ git cherry-pick [commit-hash]              # pick the hash commit code, you need to do more than one time if you need to cherry-pick more than one commit.

Latest revision as of 17:04, 20 September 2023

List Branch

$ git branch

List Tag

$ git tag

Create New Branch from Master or Other Branch

$ git checkout master                         # first, we work in master branch
$ git pull                                    # pull from original branch
$ git checkout -b [new_branch_name]           # creating new branch in our local fs
$ git push origin [new_branch_name]           # push our new branch to remote repository
$ git config core.autocrlf false              # stop replacement warning message

Create New Tag from A Commit

$ git tag [tag_name] [commit_id]              # create tag from a commit
$ git tag                                     # list tag in our local repository
$ git push origin [tag_name]                  # push our new tag to remote repository

Create New Tag from A Branch

$ git checkout [branch_name]
$ git pull
$ git tag [tag_name]
$ git push origin [tag_name]


Delete Branch in Our Local

$ git branch -d [name_of_branch]

or

$ git branch -D [name_of_branch]              # force deletion

Delete Branch in Remote

$ git -d [name_of_branch]
$ git push origin :[name_of_branch]

Delete Tag in Our Local

$ git tag -d [name_of_branch]

or

$ git tag -D [name_of_branch]                 # force deletion

Delete Tag in Remote

$ git -d [tag_name]
$ git push origin :[tag_name]

Merge Branch to Master

$ git checkout master                         # switched to branch master
$ git merge [branch_name]                     # merge

Rebase Master (As Source) to Feature (As Target)

$ git checkout master
$ git pull
$ git checkout [target_branch]
$ git rebase master

Duplicate Branch to New Branch

$ git checkout [old_branch]
$ git branch [new_branch]

or

$ git checkout -b [new_branch] [old_branch]

Reset Local Branch Same As Remote Branch

$ git checkout [name_of_branch]              # checkout to branch that will be reseted
$ git reset --hard origin/[name_of_branch]   # reset to be same as remote branch
$ git reset --hard origin                    # reset local master to be same with remote master

Reset Branch Same As Master or Other Branch

$ git checkout [name_of_branch]               # checkout to branch that will be reseted to be same as master
$ git reset --hard master                     # reset to be same as master

Copy File in A working Git Branch from Another Branch

$ git checkout [name_of_branch]               # checkout to branch where a file need to be placed
$ git checkout [source_branch] path/to/file   # source branch where the file will come from

Overwrite File in A Working Git Branch from Another Branch

$ git checkout [name_of_branch]               # checkout to branch where a file need to be replaced or overwrited
$ git checkout [source_branch] path/to/file   # source branch where the file will come from

Cherry Pick Commit(s) from Another Branch

$ git checkout [name_of_branch]               # make sure we are on the branch we want to apply the commit to.
$ git cherry-pick [commit-hash]               # pick the hash commit code, you need to do more than one time if you need to cherry-pick more than one commit.