Coder Social home page Coder Social logo

git-commands's Introduction

Using Git

Basics
Adding and Changing Things
Undo Changes and Recover Files
Viewing Commits
Branch and Merge
Commands for Remotes
Favorites
Resources

Note on Paths

In this file, directory paths are written with a forward slash as on MacOS, Linux, and the Windows-Bash shell: /dir1/dir2/somefile.

Basics

  1. When using Git locally, what are these? Define each one in a sentence

    • Staging area - file that added and ready to commit in next commit.
    • Working copy - a cloned repository from git clone command then working at it.
    • master - default branch name of repository that come from cloning any repository
    • HEAD - pointer that reference the parent of next commit that going to created
  2. When you install git on a new machine (or in a new user account) you should perform these 2 git commands to tell git your name and email. These values are used in commits that you make:

    # Git configuration commands for a new account
    git config --global user.name = "Tezigudo"
    git config --glabal user.email = "[email protected]"
    
  3. There are 2 ways to create a local Git repository. What are they?

    • first way
    git init #creating .git in current directory
    git add filename
    git commit -m "commit message"
    
    • second way
    git clone URL local_dir_name
    
  4. When you create a git repository by entering git init, Git will create a "hidden" directory for the local repository. Where is the directory for this local repository (relative to the directory where you typed "git init")?

    .git
    

Adding and Changing Things

Suppose your working copy of a repository contains these files and directories:

README.md
out/
    a.exe
src/a.py
    b.py
    c.py
test/
    test_a.py
    ...
  1. Add README.md and everything in the src directory to the git staging area.

    git add README.md src/ 
    
  2. Add test/test_a.py to the staging area (but not any other files).

    git add test/test_a.py
    
  3. List the files in the staging area.

    git diff --name-only --cached
    
  4. Remove README.md from the staging area. (Useful if you accidentally add something you don't want to commit.)

    git restore --staged README.md
    
  5. Commit everything in the staging area to the repository.

    git commit -m "commit message"
    
  6. Describe 2 steps to configure the repository so git will ignore all files in the out/ directory:

    • step one create file name .gitignore all name in this file will be ignore to commit.
    • step two in the .gitignore file write a out/ in it
  7. Command to move all the .py files from src to the top-level directory of this repository, so they are also moved in the Git repo.

    git mv src/*.py .
    
  8. Commit this change with the message "moved src directory":

    git commit -m "moved src directory"
    
  9. Command to add all changed files (but not untracked files) to the staging area using a single command.

    git add -u *
    
  10. Delete the file c.py from your working copy and the repository:

    git rm src/c.py
    

Undo Changes and Recover Files

  1. Display the differences between your working copy of a.py and the a.py in the local repository (HEAD revision):

    git diff a.py
    
  2. Display the differences between your working copy of a.py and the version in the staging area. (But, if a.py is not in the staging area this will compare working copy to HEAD revision):

    git diff --cached a.py
    
  3. View changes to be committed: Display the differences between files in the staging area and the versions in the repository. (You can also specify a file name to compare just one file.)

    git diff --staged filename(optional)
    
  4. Undo "git add": If main.py has been added to the staging area (git add main.py), remove it from the staging area:

    git restore --staged main.py
    
  5. Recover a file: Command to replace your working copy of a.py with the most recent (HEAD) version in the repository. This also works if you have deleted your working copy of this file.

    git restore a.py
    
  6. Undo a commit: Suppose you want to discard some commit(s) and move both HEAD and "master" to an earlier revision (an earlier commit) Suppose the git commit graph looks like this (aaaa, etc, are the commit ids)

    aaaa ---> bbbb ---> cccc ---> dddd [HEAD -> master]
    

    The command to reset HEAD and master to the commit id bbbb:

    git reset --hard bbbb
    
  7. Checkout old code: Using the above example, the command to replace your working copy with the files from commit with id aaaa:

    git checkout aaaa
    

    Note:

    • Git won't let you do this if you have uncommitted changes to any "tracked" files.
    • Untracked files are ignored, so after doing this command they will still be in your working copy.

Viewing Commits

  1. Show the history of commits, using one line per commit:

    git log --oneline
    

    Some versions of git have an alias "log1" for this (git log1).

  2. Show the history (as above) including all branches in the repository and include a graph connecting the commits:

    git log --graph
    
  3. List all the files in the current branch of the repository:

    git ls-files
    

    example output:

    .gitignore
    README.md
    a.py
    b.py
    test/test_a.py
    test/test_b.py
    

Branch and Merge

  1. Create a new branch named dev-foo:

    git branch dev-foo
    
  2. Display the name of your current branch:

    git status -b
    
  3. List the names of all branches, including remote branches:

    git branch -r
    
  4. Switch your working copy to the branch named dev-foo:

  • Soln1
    git checkout dev-foo 
    
  • Soln2
    git switch dev-foo
    
  1. Merge: To merge the work from dev-foo into the master branch, perform these steps:

    1. step one
      git switch master
      
    2. step two
      git merge dev-foo
      
  2. Describe under what conditions a merge may fail.

    merge fail come from merge conflict that mean same file between 2 branches
    have been edited the same part, So git won't merge cleanly. 
    

Favorites

My favourites command is git checkout or git switch in new version. this command will switch all file in the choosen branch to working copy and switching head to choosing branch.


Resources

Favourite git resourcess

Pro Git Online Book Chapters 2 & 3 contain the essentials. Downloadable PDF is also available.
Visual Git Reference one page with illustrations of git commands.

Try Git:

Learn Git Interactive Tutorial excellent visual tutorial.
Git Visualizer execute Git commands in a web browser and see the results as a graph.

git-commands's People

Contributors

tezigudo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.